Multimobile Development: Building Applications for any Smartphone
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
Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
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?
Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
    
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]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
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]

Reply to this message...
Vote that this is a GOOD answer...
 
First chapters of Multimobile Development book now available on Apress Alpha program
iPad
 
    
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]

Reply to this message...
Vote that this is a GOOD answer...
 
New book project – Multimobile Development: Building Applications for any Smartphone
Dive into HTML5
 
 
System.Threading.Thread
System.Threading.ThreadStart




Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734