Question about thread safety...

D

Dennis

I have a question about thread safety in a VB application written using Visual
Studio.Net 2003.

Here is the situation...

I am running a process thread in a modal dialog. I have written it so the user
cannot exit the dialog until the thread completes (unless they click a button
to explicitly interrupt and destroy the thread).

My question...

How concerned should I be about accessing the dialog's member variables from
within the process thread? These are variables that cannot themselves be
changed anywhere on the modal dialog window.

Thanks,

Dennis
 
M

Mattias Sjögren

How concerned should I be about accessing the dialog's member variables from
within the process thread? These are variables that cannot themselves be
changed anywhere on the modal dialog window.

If you mean members for non-UI objects it shouldn't be a problem, as
long as you use appropriate synchronization.


Mattias
 
D

Dennis

If you mean members for non-UI objects it shouldn't be a problem, as
long as you use appropriate synchronization.

Yes, that is what I mean.

1> What would I have to synchronize against if the user can't do anything to
update these member variables?

2> How would you synchronize them? I saw an example that provided access thru
a Property with surrounding SyncLock Me statements. Is that the best method?

kowallek iglou com
@ .
 
B

Brian Gideon

Dennis said:
What if I were talking about UI objects?

You cannot safely access any UI objects from a thread other than the
main UI thread. That means you'll have to use the
ISynchronizeInvoke.Invoke method, which is implemented by Form and
Control, to marshal the execution of a method from your worker thread
to the main UI thread if UI objects need to be updated.

Brian
 
D

Dennis

You cannot safely access any UI objects from a thread other than the
main UI thread. That means you'll have to use the
ISynchronizeInvoke.Invoke method, which is implemented by Form and
Control, to marshal the execution of a method from your worker thread
to the main UI thread if UI objects need to be updated.

OK. But what if I don't want to update a UI object from the worker thread? If
I only want to get a value from the UI object, can I access it? This
particular UI object happens to be disabled while the worker thread is
running.
 
B

Brian Gideon

Dennis said:
OK. But what if I don't want to update a UI object from the worker thread? If
I only want to get a value from the UI object, can I access it? This
particular UI object happens to be disabled while the worker thread is
running.

No. The read might be inconsistent since it could happen when the
control is modifying its internal state. Unless it's documented
somewhere I would not assume that a disabled control isn't responding
to windows messages and thus could be changing its internal state. I
suspect in most cases it would actually be okay though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top