Correct Approach to Multi-Threading Issue

G

Guest

I've done a lot of reading about multi-threading (I come from a VB6
background so these things are new to me) but I can't find an example of my
exact scenario.

It's very simple, I've got a worker thread which needs to read the Checked
property of a checkbox on a form. I gather that I can't read this property
from the worker thread (only the UI thread) so is the best approach to use a
(VB.NET) function to read the checkbox property and use Me.Invoke to invoke
the function in the UI thread?

Thanks in advance,

Colin
 
S

Stephany Young

All you need is a variable with sufficient scope to be be visible to both
the UI thread and the worker thread.

In the CheckChanged event handler for the Checkbox, set the variable to the
value of the Checkbox's Checked property.

The next time the worker thread reads the variable, it will 'notice' any
change.
 
G

Guest

Thanks for the reply.

If I use this approach won't I need to use SyncLock to make sure the
variable isn't being accessed by the UI and worker threads at the same time?
 
S

Stephany Young

Quite possibly. Have a read up on it.


Colmeister said:
Thanks for the reply.

If I use this approach won't I need to use SyncLock to make sure the
variable isn't being accessed by the UI and worker threads at the same
time?
 

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