I had a question about threading and access to private class
variables. I am developing a windows service which has a class inside
of it which will receive various asynchronous calls to it via
delegates.
Inside one method of the class (which is a private variable in my
windows service), an ArrayList variable is modified by adding a new
item to its list. Each time the method is called via an asynchronous
delegate (from numerous sources), a number is added to this ArrayList.
It is possible that multiple delegates may be trying to call the
method at the same time.
Now, I understand the concept of using Monitor.Enter/Exit when needing
access to specific values or something, but is that needed here? I am
simply adding items to the list. I don't care about the order they
are added in, I simply want to be sure they are added.
Do I need to use Monitor.Enter/Exit, or can I simply add the item as
usual?
Thanks in advance.
--
Mike mi*********@hotmail.com 14 2459
Mike,
you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same
element of the array to set. That would be the case, if thread one hasn't
yet increased the element counter, and thread 2 is just about to assign the
object to the array.
Klaus
"Mike" <mi*********@hotmail.com> schrieb im Newsbeitrag
news:cb**************************@posting.google.c om... I had a question about threading and access to private class variables. I am developing a windows service which has a class inside of it which will receive various asynchronous calls to it via delegates.
Inside one method of the class (which is a private variable in my windows service), an ArrayList variable is modified by adding a new item to its list. Each time the method is called via an asynchronous delegate (from numerous sources), a number is added to this ArrayList. It is possible that multiple delegates may be trying to call the method at the same time.
Now, I understand the concept of using Monitor.Enter/Exit when needing access to specific values or something, but is that needed here? I am simply adding items to the list. I don't care about the order they are added in, I simply want to be sure they are added.
Do I need to use Monitor.Enter/Exit, or can I simply add the item as usual?
Thanks in advance.
-- Mike mi*********@hotmail.com
Mike,
you definitely should use SyncLock or the Monitor class to sync the add
process, otherwise you could end up with two threads overwriting the same
element of the array to set. That would be the case, if thread one hasn't
yet increased the element counter, and thread 2 is just about to assign the
object to the array.
Klaus
"Mike" <mi*********@hotmail.com> schrieb im Newsbeitrag
news:cb**************************@posting.google.c om... I had a question about threading and access to private class variables. I am developing a windows service which has a class inside of it which will receive various asynchronous calls to it via delegates.
Inside one method of the class (which is a private variable in my windows service), an ArrayList variable is modified by adding a new item to its list. Each time the method is called via an asynchronous delegate (from numerous sources), a number is added to this ArrayList. It is possible that multiple delegates may be trying to call the method at the same time.
Now, I understand the concept of using Monitor.Enter/Exit when needing access to specific values or something, but is that needed here? I am simply adding items to the list. I don't care about the order they are added in, I simply want to be sure they are added.
Do I need to use Monitor.Enter/Exit, or can I simply add the item as usual?
Thanks in advance.
-- Mike mi*********@hotmail.com
"Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message news:<ON*************@TK2MSFTNGP09.phx.gbl>... Mike,
you definitely should use SyncLock or the Monitor class to sync the add process, otherwise you could end up with two threads overwriting the same element of the array to set. That would be the case, if thread one hasn't yet increased the element counter, and thread 2 is just about to assign the object to the array.
Thanks for the reply.
Well, I will deifnitely SyncLock things to make the class thread safe.
But it appears I am not as knowledgable on SyncLock as I thought I
was..
If I have code like this:
Public Class Foo
Private _someList As New ArrayList
Public Sub FooTest()
SyncLock _someList
_someList.Add("foo test")
End SyncLock
End Sub
Public Sub FooTest2()
_someList.Clear()
End Sub
End Class
If I have multiple threads call Foo.FooTest(), does the synclock also
protect against other ArrayList methods occuring while inside the
SyncLock?
If a thread had locked _someList in FooTest, and some other thread had
called Foo.FooTest2(), would the thread calling FooTest2 be blocked
until the first thread released _someList via End SyncLock?
Thanks.
--
Mike mi*********@hotmail.com
"Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message news:<ON*************@TK2MSFTNGP09.phx.gbl>... Mike,
you definitely should use SyncLock or the Monitor class to sync the add process, otherwise you could end up with two threads overwriting the same element of the array to set. That would be the case, if thread one hasn't yet increased the element counter, and thread 2 is just about to assign the object to the array.
Thanks for the reply.
Well, I will deifnitely SyncLock things to make the class thread safe.
But it appears I am not as knowledgable on SyncLock as I thought I
was..
If I have code like this:
Public Class Foo
Private _someList As New ArrayList
Public Sub FooTest()
SyncLock _someList
_someList.Add("foo test")
End SyncLock
End Sub
Public Sub FooTest2()
_someList.Clear()
End Sub
End Class
If I have multiple threads call Foo.FooTest(), does the synclock also
protect against other ArrayList methods occuring while inside the
SyncLock?
If a thread had locked _someList in FooTest, and some other thread had
called Foo.FooTest2(), would the thread calling FooTest2 be blocked
until the first thread released _someList via End SyncLock?
Thanks.
--
Mike mi*********@hotmail.com
Mike,
see inline
"Mike" <mi*********@hotmail.com> schrieb im Newsbeitrag
news:cb**************************@posting.google.c om... "Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message
news:<ON*************@TK2MSFTNGP09.phx.gbl>... Mike,
you definitely should use SyncLock or the Monitor class to sync the add process, otherwise you could end up with two threads overwriting the
same element of the array to set. That would be the case, if thread one
hasn't yet increased the element counter, and thread 2 is just about to assign
the object to the array.
Thanks for the reply.
Well, I will deifnitely SyncLock things to make the class thread safe. But it appears I am not as knowledgable on SyncLock as I thought I was..
If I have code like this:
Public Class Foo Private _someList As New ArrayList
Public Sub FooTest() SyncLock _someList _someList.Add("foo test") End SyncLock End Sub
Public Sub FooTest2() _someList.Clear() End Sub End Class
If I have multiple threads call Foo.FooTest(), does the synclock also protect against other ArrayList methods occuring while inside the SyncLock?
Not at all! Well, except, if those methods you are talking about are also
SyncLocked with the same object reference, and that object hasn't been
altered in a way (which is therefore exactly what you must take care
of/implement). In your case adding or deleting elements wouldn't alter
_someList in the means of synclock-synchronisation, but if you would define
_someList=new ArrayList(blbalbal) in SubFooTest2, the object would become
useless for synchronisation. To avoid that, simply use
SyncLock me
in a class instance, because it's of course not very likely, that the
instance of Me changes while its code is being executed.
If a thread had locked _someList in FooTest, and some other thread had called Foo.FooTest2(), would the thread calling FooTest2 be blocked until the first thread released _someList via End SyncLock?
No. See explanation above.
So, implement a SyncLock Me everytime you access your array, and you're on
the safe side.
However, if you have more then one element to synchonize which are supposed
to not exclude their manipulating code from being executed at the same time,
then you'd choose another object for syncronisation (a string, for example,
with a class wide scope that isn't empty and NEVER altered while the threads
are running).
Hope that helps,
Klaus
Mike,
see inline
"Mike" <mi*********@hotmail.com> schrieb im Newsbeitrag
news:cb**************************@posting.google.c om... "Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message
news:<ON*************@TK2MSFTNGP09.phx.gbl>... Mike,
you definitely should use SyncLock or the Monitor class to sync the add process, otherwise you could end up with two threads overwriting the
same element of the array to set. That would be the case, if thread one
hasn't yet increased the element counter, and thread 2 is just about to assign
the object to the array.
Thanks for the reply.
Well, I will deifnitely SyncLock things to make the class thread safe. But it appears I am not as knowledgable on SyncLock as I thought I was..
If I have code like this:
Public Class Foo Private _someList As New ArrayList
Public Sub FooTest() SyncLock _someList _someList.Add("foo test") End SyncLock End Sub
Public Sub FooTest2() _someList.Clear() End Sub End Class
If I have multiple threads call Foo.FooTest(), does the synclock also protect against other ArrayList methods occuring while inside the SyncLock?
Not at all! Well, except, if those methods you are talking about are also
SyncLocked with the same object reference, and that object hasn't been
altered in a way (which is therefore exactly what you must take care
of/implement). In your case adding or deleting elements wouldn't alter
_someList in the means of synclock-synchronisation, but if you would define
_someList=new ArrayList(blbalbal) in SubFooTest2, the object would become
useless for synchronisation. To avoid that, simply use
SyncLock me
in a class instance, because it's of course not very likely, that the
instance of Me changes while its code is being executed.
If a thread had locked _someList in FooTest, and some other thread had called Foo.FooTest2(), would the thread calling FooTest2 be blocked until the first thread released _someList via End SyncLock?
No. See explanation above.
So, implement a SyncLock Me everytime you access your array, and you're on
the safe side.
However, if you have more then one element to synchonize which are supposed
to not exclude their manipulating code from being executed at the same time,
then you'd choose another object for syncronisation (a string, for example,
with a class wide scope that isn't empty and NEVER altered while the threads
are running).
Hope that helps,
Klaus
Thanks for the response. See my questions below.. <snip> If I have code like this:
Public Class Foo Private _someList As New ArrayList
Public Sub FooTest() SyncLock _someList _someList.Add("foo test") End SyncLock End Sub
Public Sub FooTest2() _someList.Clear() End Sub End Class
If I have multiple threads call Foo.FooTest(), does the synclock also protect against other ArrayList methods occuring while inside the SyncLock? Not at all! Well, except, if those methods you are talking about are also SyncLocked with the same object reference, and that object hasn't been altered in a way (which is therefore exactly what you must take care of/implement). In your case adding or deleting elements wouldn't alter _someList in the means of synclock-synchronisation, but if you would
define _someList=new ArrayList(blbalbal) in SubFooTest2, the object would become useless for synchronisation. To avoid that, simply use SyncLock me
All of my SyncLock calls within class Foo would use _someList as the locking
object, so would it be valid to say that any ArrayList method call would be
blocked until the first SyncLock on _someList is released?
If I SyncLock on 'Me', will that block any other method of the class from
executing on another thread? Even if the other method does not touch
_someList? I need to allow for other methods of the class to execute, but
still keep _someList protected.
<snip> So, implement a SyncLock Me everytime you access your array, and you're on the safe side. However, if you have more then one element to synchonize which are
supposed to not exclude their manipulating code from being executed at the same
time, then you'd choose another object for syncronisation (a string, for
example, with a class wide scope that isn't empty and NEVER altered while the
threads are running).
I'm a bit confused on this part. Could you elaborate?
Say I have the following code:
Public Class DataClass
Private _someList As ArrayList
Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock
End Sub
Public Sub EmptyList()
SyncLock Me
_someList.Clear()
End SyncLock
End Sub
Public Sub DoNothingToList()
Console.WriteLine("Hello, World!")
End Sub
End Class
If I have an instance of DataClass, foo, and I have 3 threads:
Thread #1: foo.AddToList()
Thread #2: foo.EmptyList()
Thread #3: foo.DoNothingToList()
If all three threads execute simultaneously (I know they are not exactly
simultaneous), what happens? Does AddToList() block EmptyList() until it
has finished? Does AddToList() block DoNothingToList()?
That is the part I am confused about.
Also, is it acceptable to use a SyncLock block in a method, then release the
lock, do some processing, acquire a lock again, do something with the locked
code, and then release the lock? Or is it better to have on lock
encompassing the entire method? Like this:
Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock
' Do some other stuff...
SyncLock Me
_someList.Add("Extra data goes here...")
End SyncLock
End Sub
Is this ok?
Thanks for all your help!
--
Mike Loll mi*********@hotmail.com
Thanks for the response. See my questions below.. <snip> If I have code like this:
Public Class Foo Private _someList As New ArrayList
Public Sub FooTest() SyncLock _someList _someList.Add("foo test") End SyncLock End Sub
Public Sub FooTest2() _someList.Clear() End Sub End Class
If I have multiple threads call Foo.FooTest(), does the synclock also protect against other ArrayList methods occuring while inside the SyncLock? Not at all! Well, except, if those methods you are talking about are also SyncLocked with the same object reference, and that object hasn't been altered in a way (which is therefore exactly what you must take care of/implement). In your case adding or deleting elements wouldn't alter _someList in the means of synclock-synchronisation, but if you would
define _someList=new ArrayList(blbalbal) in SubFooTest2, the object would become useless for synchronisation. To avoid that, simply use SyncLock me
All of my SyncLock calls within class Foo would use _someList as the locking
object, so would it be valid to say that any ArrayList method call would be
blocked until the first SyncLock on _someList is released?
If I SyncLock on 'Me', will that block any other method of the class from
executing on another thread? Even if the other method does not touch
_someList? I need to allow for other methods of the class to execute, but
still keep _someList protected.
<snip> So, implement a SyncLock Me everytime you access your array, and you're on the safe side. However, if you have more then one element to synchonize which are
supposed to not exclude their manipulating code from being executed at the same
time, then you'd choose another object for syncronisation (a string, for
example, with a class wide scope that isn't empty and NEVER altered while the
threads are running).
I'm a bit confused on this part. Could you elaborate?
Say I have the following code:
Public Class DataClass
Private _someList As ArrayList
Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock
End Sub
Public Sub EmptyList()
SyncLock Me
_someList.Clear()
End SyncLock
End Sub
Public Sub DoNothingToList()
Console.WriteLine("Hello, World!")
End Sub
End Class
If I have an instance of DataClass, foo, and I have 3 threads:
Thread #1: foo.AddToList()
Thread #2: foo.EmptyList()
Thread #3: foo.DoNothingToList()
If all three threads execute simultaneously (I know they are not exactly
simultaneous), what happens? Does AddToList() block EmptyList() until it
has finished? Does AddToList() block DoNothingToList()?
That is the part I am confused about.
Also, is it acceptable to use a SyncLock block in a method, then release the
lock, do some processing, acquire a lock again, do something with the locked
code, and then release the lock? Or is it better to have on lock
encompassing the entire method? Like this:
Public Sub AddToList()
SyncLock Me
_someList.Add("Some data goes here...")
End SyncLock
' Do some other stuff...
SyncLock Me
_someList.Add("Extra data goes here...")
End SyncLock
End Sub
Is this ok?
Thanks for all your help!
--
Mike Loll mi*********@hotmail.com
Michael,
see inline again
"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag
news:ev**************@TK2MSFTNGP11.phx.gbl... Thanks for the response. See my questions below..
<snip> All of my SyncLock calls within class Foo would use _someList as the
locking object, so would it be valid to say that any ArrayList method call would
be blocked until the first SyncLock on _someList is released?
No, only if that method is SyncLocked with the ArrayList, too. If I SyncLock on 'Me', will that block any other method of the class from executing on another thread? Even if the other method does not touch _someList? I need to allow for other methods of the class to execute, but still keep _someList protected.
<snip> So, implement a SyncLock Me everytime you access your array, and you're
on the safe side. However, if you have more then one element to synchonize which are supposed to not exclude their manipulating code from being executed at the same time, then you'd choose another object for syncronisation (a string, for example, with a class wide scope that isn't empty and NEVER altered while the threads are running).
I'm a bit confused on this part. Could you elaborate?
Say I have the following code:
Public Class DataClass Private _someList As ArrayList
Public Sub AddToList() SyncLock Me _someList.Add("Some data goes here...") End SyncLock End Sub
Public Sub EmptyList() SyncLock Me _someList.Clear() End SyncLock End Sub
Public Sub DoNothingToList() Console.WriteLine("Hello, World!") End Sub
End Class
If I have an instance of DataClass, foo, and I have 3 threads:
Thread #1: foo.AddToList()
Would have to wait - it's SyncLocked with Me. Thread #2: foo.EmptyList()
Would have to wait - it's SyncLocked with Me. Thread #3: foo.DoNothingToList()
Would not have to wait. It's using the object which is used for SyncLocking
in the other methods alright, but it's actually not embedded in a SyncLock
code block.
That's actually the strange thing about SyncLock and the object it's using
as a parameter. SyncLock *does not lock the object*. It's locking *the code*
and using the object only as a kind of tool to do its job - keep that in
mind! If all three threads execute simultaneously (I know they are not exactly simultaneous), what happens? Does AddToList() block EmptyList() until it has finished? Does AddToList() block DoNothingToList()?
That is the part I am confused about.
Also, is it acceptable to use a SyncLock block in a method, then release
the lock, do some processing, acquire a lock again, do something with the
locked code, and then release the lock? Or is it better to have on lock encompassing the entire method? Like this:
Public Sub AddToList() SyncLock Me _someList.Add("Some data goes here...") End SyncLock
' Do some other stuff...
SyncLock Me _someList.Add("Extra data goes here...") End SyncLock End Sub
Is this ok?
Yes, that's OK.
BTW: Just in case: Keep in mind, that you musn't access controls on a form
from a thread in a direct way (by changing their properties directly, e.g.).
Only use the "diversion" over control.invoke (see vs.help on that topic for
more info).
Thanks for all your help!
You're welcome! -- Mike Loll mi*********@hotmail.com
Michael,
see inline again
"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag
news:ev**************@TK2MSFTNGP11.phx.gbl... Thanks for the response. See my questions below..
<snip> All of my SyncLock calls within class Foo would use _someList as the
locking object, so would it be valid to say that any ArrayList method call would
be blocked until the first SyncLock on _someList is released?
No, only if that method is SyncLocked with the ArrayList, too. If I SyncLock on 'Me', will that block any other method of the class from executing on another thread? Even if the other method does not touch _someList? I need to allow for other methods of the class to execute, but still keep _someList protected.
<snip> So, implement a SyncLock Me everytime you access your array, and you're
on the safe side. However, if you have more then one element to synchonize which are supposed to not exclude their manipulating code from being executed at the same time, then you'd choose another object for syncronisation (a string, for example, with a class wide scope that isn't empty and NEVER altered while the threads are running).
I'm a bit confused on this part. Could you elaborate?
Say I have the following code:
Public Class DataClass Private _someList As ArrayList
Public Sub AddToList() SyncLock Me _someList.Add("Some data goes here...") End SyncLock End Sub
Public Sub EmptyList() SyncLock Me _someList.Clear() End SyncLock End Sub
Public Sub DoNothingToList() Console.WriteLine("Hello, World!") End Sub
End Class
If I have an instance of DataClass, foo, and I have 3 threads:
Thread #1: foo.AddToList()
Would have to wait - it's SyncLocked with Me. Thread #2: foo.EmptyList()
Would have to wait - it's SyncLocked with Me. Thread #3: foo.DoNothingToList()
Would not have to wait. It's using the object which is used for SyncLocking
in the other methods alright, but it's actually not embedded in a SyncLock
code block.
That's actually the strange thing about SyncLock and the object it's using
as a parameter. SyncLock *does not lock the object*. It's locking *the code*
and using the object only as a kind of tool to do its job - keep that in
mind! If all three threads execute simultaneously (I know they are not exactly simultaneous), what happens? Does AddToList() block EmptyList() until it has finished? Does AddToList() block DoNothingToList()?
That is the part I am confused about.
Also, is it acceptable to use a SyncLock block in a method, then release
the lock, do some processing, acquire a lock again, do something with the
locked code, and then release the lock? Or is it better to have on lock encompassing the entire method? Like this:
Public Sub AddToList() SyncLock Me _someList.Add("Some data goes here...") End SyncLock
' Do some other stuff...
SyncLock Me _someList.Add("Extra data goes here...") End SyncLock End Sub
Is this ok?
Yes, that's OK.
BTW: Just in case: Keep in mind, that you musn't access controls on a form
from a thread in a direct way (by changing their properties directly, e.g.).
Only use the "diversion" over control.invoke (see vs.help on that topic for
more info).
Thanks for all your help!
You're welcome! -- Mike Loll mi*********@hotmail.com
Thanks for your reply. Assuming Thread #1 in my example always executed
first, would the outcome be as follows:
Thread #1 executes and releases SyncLock.
Thread #2 acquires synclock, executes, and releases SyncLock.
Thread #3 executes whenever it can, since it is not using SyncLock.
"Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message
news:OU**************@TK2MSFTNGP11.phx.gbl... Michael,
see inline again
"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag news:ev**************@TK2MSFTNGP11.phx.gbl... Thanks for the response. See my questions below..
> <snip> All of my SyncLock calls within class Foo would use _someList as the locking object, so would it be valid to say that any ArrayList method call would be blocked until the first SyncLock on _someList is released?
No, only if that method is SyncLocked with the ArrayList, too.
If I SyncLock on 'Me', will that block any other method of the class
from executing on another thread? Even if the other method does not touch _someList? I need to allow for other methods of the class to execute,
but still keep _someList protected.
<snip> So, implement a SyncLock Me everytime you access your array, and
you're on the safe side. However, if you have more then one element to synchonize which are supposed to not exclude their manipulating code from being executed at the same time, then you'd choose another object for syncronisation (a string, for example, with a class wide scope that isn't empty and NEVER altered while the threads are running).
I'm a bit confused on this part. Could you elaborate?
Say I have the following code:
Public Class DataClass Private _someList As ArrayList
Public Sub AddToList() SyncLock Me _someList.Add("Some data goes here...") End SyncLock End Sub
Public Sub EmptyList() SyncLock Me _someList.Clear() End SyncLock End Sub
Public Sub DoNothingToList() Console.WriteLine("Hello, World!") End Sub
End Class
If I have an instance of DataClass, foo, and I have 3 threads:
Thread #1: foo.AddToList() Would have to wait - it's SyncLocked with Me. Thread #2: foo.EmptyList() Would have to wait - it's SyncLocked with Me. Thread #3: foo.DoNothingToList() Would not have to wait. It's using the object which is used for
SyncLocking in the other methods alright, but it's actually not embedded in a SyncLock code block. That's actually the strange thing about SyncLock and the object it's using as a parameter. SyncLock *does not lock the object*. It's locking *the
code* and using the object only as a kind of tool to do its job - keep that in mind! If all three threads execute simultaneously (I know they are not exactly simultaneous), what happens? Does AddToList() block EmptyList() until
it has finished? Does AddToList() block DoNothingToList()?
That is the part I am confused about.
Also, is it acceptable to use a SyncLock block in a method, then release
the lock, do some processing, acquire a lock again, do something with the locked code, and then release the lock? Or is it better to have on lock encompassing the entire method? Like this:
Public Sub AddToList() SyncLock Me _someList.Add("Some data goes here...") End SyncLock
' Do some other stuff...
SyncLock Me _someList.Add("Extra data goes here...") End SyncLock End Sub
Is this ok? Yes, that's OK.
BTW: Just in case: Keep in mind, that you musn't access controls on a form from a thread in a direct way (by changing their properties directly,
e.g.). Only use the "diversion" over control.invoke (see vs.help on that topic
for more info).
Thanks for all your help! You're welcome! -- Mike Loll mi*********@hotmail.com
Thanks for your reply. Assuming Thread #1 in my example always executed
first, would the outcome be as follows:
Thread #1 executes and releases SyncLock.
Thread #2 acquires synclock, executes, and releases SyncLock.
Thread #3 executes whenever it can, since it is not using SyncLock.
"Klaus Löffelmann" <fo***********@loeffelmann.de> wrote in message
news:OU**************@TK2MSFTNGP11.phx.gbl... Michael,
see inline again
"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag news:ev**************@TK2MSFTNGP11.phx.gbl... Thanks for the response. See my questions below..
> <snip> All of my SyncLock calls within class Foo would use _someList as the locking object, so would it be valid to say that any ArrayList method call would be blocked until the first SyncLock on _someList is released?
No, only if that method is SyncLocked with the ArrayList, too.
If I SyncLock on 'Me', will that block any other method of the class
from executing on another thread? Even if the other method does not touch _someList? I need to allow for other methods of the class to execute,
but still keep _someList protected.
<snip> So, implement a SyncLock Me everytime you access your array, and
you're on the safe side. However, if you have more then one element to synchonize which are supposed to not exclude their manipulating code from being executed at the same time, then you'd choose another object for syncronisation (a string, for example, with a class wide scope that isn't empty and NEVER altered while the threads are running).
I'm a bit confused on this part. Could you elaborate?
Say I have the following code:
Public Class DataClass Private _someList As ArrayList
Public Sub AddToList() SyncLock Me _someList.Add("Some data goes here...") End SyncLock End Sub
Public Sub EmptyList() SyncLock Me _someList.Clear() End SyncLock End Sub
Public Sub DoNothingToList() Console.WriteLine("Hello, World!") End Sub
End Class
If I have an instance of DataClass, foo, and I have 3 threads:
Thread #1: foo.AddToList() Would have to wait - it's SyncLocked with Me. Thread #2: foo.EmptyList() Would have to wait - it's SyncLocked with Me. Thread #3: foo.DoNothingToList() Would not have to wait. It's using the object which is used for
SyncLocking in the other methods alright, but it's actually not embedded in a SyncLock code block. That's actually the strange thing about SyncLock and the object it's using as a parameter. SyncLock *does not lock the object*. It's locking *the
code* and using the object only as a kind of tool to do its job - keep that in mind! If all three threads execute simultaneously (I know they are not exactly simultaneous), what happens? Does AddToList() block EmptyList() until
it has finished? Does AddToList() block DoNothingToList()?
That is the part I am confused about.
Also, is it acceptable to use a SyncLock block in a method, then release
the lock, do some processing, acquire a lock again, do something with the locked code, and then release the lock? Or is it better to have on lock encompassing the entire method? Like this:
Public Sub AddToList() SyncLock Me _someList.Add("Some data goes here...") End SyncLock
' Do some other stuff...
SyncLock Me _someList.Add("Extra data goes here...") End SyncLock End Sub
Is this ok? Yes, that's OK.
BTW: Just in case: Keep in mind, that you musn't access controls on a form from a thread in a direct way (by changing their properties directly,
e.g.). Only use the "diversion" over control.invoke (see vs.help on that topic
for more info).
Thanks for all your help! You're welcome! -- Mike Loll mi*********@hotmail.com
"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag
news:Oq**************@tk2msftngp13.phx.gbl... Thanks for your reply. Assuming Thread #1 in my example always executed first, would the outcome be as follows:
Thread #1 executes and releases SyncLock. Thread #2 acquires synclock, executes, and releases SyncLock. Thread #3 executes whenever it can, since it is not using SyncLock.
Yes.
"Michael Loll" <mi*********@NOSPAM.hotmail.com> schrieb im Newsbeitrag
news:Oq**************@tk2msftngp13.phx.gbl... Thanks for your reply. Assuming Thread #1 in my example always executed first, would the outcome be as follows:
Thread #1 executes and releases SyncLock. Thread #2 acquires synclock, executes, and releases SyncLock. Thread #3 executes whenever it can, since it is not using SyncLock.
Yes. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ste |
last post by:
this code not work: it is no possible Exit from Monitor
Why ?
thank
ste
////////
using System;
using System.Collections;
|
by: Eric |
last post by:
I'm looking at this page in the MSDN right here:
ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist...
|
by: vbportal |
last post by:
Hi,
I would like to add BitArrays to an ArrayList and then remove any duplicates
- can someone please help me forward. I seem to have (at leaset...
|
by: MattB |
last post by:
Hello
I am starting a new thread in a button click event.
This thread calls an method which sends emails, I don't want the page
to wait for...
|
by: Mike |
last post by:
I had a question about threading and access to private class
variables. I am developing a windows service which has a class inside
of it which...
|
by: AdrianJMartin |
last post by:
Hi all,
I have a need for a STA thread from asp.net. I can create the thread and it
runs fine. But when it is finished, the thread still 'hangs'...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
| |