Connecting Tech Pros Worldwide Help | Site Map

Thread sync problem

Gordon Knote
Guest
 
Posts: n/a
#1: Nov 20 '05
Hi

I'm currently writing a VB.NET service that uses external
Dlls, like this:

Private Declare Function ReadSettings Lib "mydll.dll" ()

Now, how can I protect this function (ReadSettings) from
being called more then once a time?
I tried to use

System.Threading.Monitor.Enter(ReadSettings)
ReadSettings()
System.Threading.Monitor.Exit(ReadSettings)

but this caused a reference null exception. So, can
anyone tell me how I can protect external functions?


Thanks
Gordon
Tom Shelton
Guest
 
Posts: n/a
#2: Nov 20 '05

re: Thread sync problem


Gordon Knote wrote:[color=blue]
> Hi
>
> I'm currently writing a VB.NET service that uses external
> Dlls, like this:
>
> Private Declare Function ReadSettings Lib "mydll.dll" ()
>
> Now, how can I protect this function (ReadSettings) from
> being called more then once a time?
> I tried to use
>
> System.Threading.Monitor.Enter(ReadSettings)
> ReadSettings()
> System.Threading.Monitor.Exit(ReadSettings)
>
> but this caused a reference null exception. So, can
> anyone tell me how I can protect external functions?
>
>
> Thanks
> Gordon[/color]

You need to use an object in your Monitor.Enter. So you would do
something like this...

Public Class Resource

Private Object lockObject = New Object()


Public Function ThisIsTheFunction() As Integer
Try
Monitor.Enter(lockObject)
ReadSettings()
Catch (Exception ex)
' Handle your exceptions
Finally
Monitor.Exit(lockObject)
End Try
End Function

End Class

You can also use SyncLock as well. It is really just a shorthand for
using the Monitor object and the Try/Catch/Finally stuff ;)

HTH,
Tom Shelton

Tom Spink
Guest
 
Posts: n/a
#3: Nov 20 '05

re: Thread sync problem


> You can also use SyncLock as well. It is really just a shorthand for[color=blue]
> using the Monitor object and the Try/Catch/Finally stuff ;)[/color]

Which would become...

' ///
Public Class Resource

Private lockObject As New Object

Public Function ThisIsTheFunction() As Integer

SyncLock lockObject
ReadSettings()
End SyncLock

End Function

End Class
' ///

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"


"Tom Shelton" <tom@mtogden.com> wrote in message
news:uj10BXGcDHA.1492@TK2MSFTNGP12.phx.gbl...[color=blue]
> Gordon Knote wrote:[color=green]
> > Hi
> >
> > I'm currently writing a VB.NET service that uses external
> > Dlls, like this:
> >
> > Private Declare Function ReadSettings Lib "mydll.dll" ()
> >
> > Now, how can I protect this function (ReadSettings) from
> > being called more then once a time?
> > I tried to use
> >
> > System.Threading.Monitor.Enter(ReadSettings)
> > ReadSettings()
> > System.Threading.Monitor.Exit(ReadSettings)
> >
> > but this caused a reference null exception. So, can
> > anyone tell me how I can protect external functions?
> >
> >
> > Thanks
> > Gordon[/color]
>
> You need to use an object in your Monitor.Enter. So you would do
> something like this...
>
> Public Class Resource
>
> Private Object lockObject = New Object()
>
>
> Public Function ThisIsTheFunction() As Integer
> Try
> Monitor.Enter(lockObject)
> ReadSettings()
> Catch (Exception ex)
> ' Handle your exceptions
> Finally
> Monitor.Exit(lockObject)
> End Try
> End Function
>
> End Class
>
> You can also use SyncLock as well. It is really just a shorthand for
> using the Monitor object and the Try/Catch/Finally stuff ;)
>
> HTH,
> Tom Shelton
>[/color]


Closed Thread