|
-
Aug 4th, 2010, 04:55 PM
#7
Re: Thread safety
I'd still like to know though: is it possible for a thread that only raises events to cause another thread responding to these events and not interfering in any other way with the other thread (function arguments are byval), to be non-thread-safe? Even with all the university teachings (granted it was a long time ago), I can't wrap my head around how that would ever cause a conflict.
Its not the threads interacting with each other specifically that causes the problems, its the threads both interacting with the same objects. If you had a List(Of String) for example that both threads could access, if one thread was looping through that list whilst the other thread was removing items from that list then it will cause problems because the thread that is looping through the list will suddenly find that the list is not in the state that it was expecting it to be in (this usually generates an exception stating that "the collection has been modified").
Also an important point to note - passing arguments ByVal does not make any difference if the object you are passing is a Reference type (a Class). When you pass a reference type ByVal it just passes a copy of the reference, rather than a copy of the actual object, so the method accepting the argument does not get its own copy of whatever object you passed in, it just gets a reference to the same object that the calling method was using. This is especially important to understand in a multi-threaded application.
You say you are getting a cross thread exception - can you show us the code where you actually get this exception and explain which thread is executing that code etc, and then we can probably give you more specific advice.
The programming ought to be thread-safe as is in my opinion, since one thread is raising events only (no manipulation of objects) and the other thread listens for events and reacts to them through function parameters
I dont quite understand this sentence, how can one thread just be raising events without manipulating any objects - how does it know when to raise an event and where does it get the values from that it passes in as arguments?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|