|
| Can threads share objects? |
|
|
|
|
| Messages |
|
Related Types |
This message was discovered on microsoft.public.dotnet.languages.csharp.
| John |
Hi,
I have a worker thread that fills in data fields in a class. It takes a few minutes to fill in all the fields because of slow database queries.
I want to display the filled out fields in a GUI in the main thread.
I give the worker thread a queue full of the objects that I want "filled out". When the worker fills out an object I use a callback to let the main thread know that a particular object is ready to be displayed.
The problem is that my main thread hangs when trying to read data out of the object that was put there by the worker thread until the worker thread stops being busy. Once it's done all it's work everything is cool. But while it's still processing I can get at any of the data.
Does this make sense? What am I missing?
Thanks, John
|
|
|
| |
|
| |
| |
| Eliyahu \(Vyacheslav\) Biktagirov |
Strange.. I never got such problem. Anyway, why you trying read object before it filled if you are using some kind of synchronization?
|
|
|
| |
|
| |
|
| |
| Vyas Bharghava |
You have to synchronize on the object that's being filled & read for display. But if you're locking the whole object when you're filling it, the result you're getting is right i.e, the GUI thread / the thread that displays data, will get blocked till the worker thread releases it. Rather, synchronize the methods and don't lock the whole class.
Vyas
"John" <Click here to reveal e-mail address> wrote in message news:Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Nick |
I think you are getting many terms mixed up but this is what you are looking for and it has worked for me.
class myClass () { public myClass() { Thread loadThread = new Thread(new ThreadStart(myMethod)); loadThread.Name = "Type Loader"; loadThread.IsBackground = true; loadThread.Start(); }
private void myMethod () { // do processing here progressBar.Value++; } }
"Vyas Bharghava" <Click here to reveal e-mail address> wrote in message news:e413bW57BHA.2256@tkmsftngp02... [Original message clipped]
|
|
|
| |
|
| |
| |
| John |
I have been doing exactly what you show here. However, I don't use:
loadThread.IsBackground = true;
Could this be causing my problems? What happens if you don't set IsBackground to true?
John
"Nick" <Click here to reveal e-mail address> wrote in message news:<OJLG#G67BHA.2288@tkmsftngp03>... [Original message clipped]
|
|
|
| |
|
| |
|
|
|
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|