Threading question

T

Tom B

I've written the code below to try and figure out how threading works.
My assumption is that when starting a new thread it would run at the same
time as the original thread.
Am I wrong?

I've made a simple form with two text labels and a button. When I click the
button, it creates a thread that updates one label, and the main code
updates the other label.

It appears that the created thread doesn't run until the main thread is
done?!?

Is my code wrong? Is my logic wrong? Am I an idiot?

Thanks for taking a look


private void button1_Click(object sender, System.EventArgs e)
{

ThreadStart ts=new ThreadStart(addText);
Thread t = new Thread(ts);
t.Start();


//for (int iLoop=0;iLoop<10;iLoop++)
//{
//lblWorking.Text="";
lblWorking.Text=DateTime.Now.ToString();
lblWorking.Refresh();
//Thread.Sleep(1000);
//}
}

private void addText()
{
lblText.Text+= "Starting: " + DateTime.Now.ToString() + "\n";
//MessageBox.Show("In the thread");
for (int iLoop=0;iLoop<10;iLoop++)
{
Thread.Sleep(300);
lblText.Refresh();
}

lblText.Text+="Finishing: " + DateTime.Now.ToString() + "\n";
lblText.Refresh();
}
 
M

Michael Mayer

L

Lee Alexander

Well for one thing you shouldn't be calling methods or properties on a
control directly from another thread. You need to call them via
Control.Invoke. Apart from that it may *seem* that the thread isn't starting
until the main thread has finished its bit of code but in reality it's a
toss up as to what code executes first. On a SMP machine the spawned thread
may get there first. Your example though will always probably show the main
thread updating it's label first since the main thread has direct access to
the control and the message queue whilst the other threads UI update will
only happen when the main thread processes the message queue which will be
after 'button1_Click' has executed.

Regards
Lee
 
T

Tom B

Thanks, I'll read those two articles. The samples I'd seen were Console
apps, I just figured they did that to minimize the code, didn't occur to me
that it would actually make a difference.

Again, thanks for the articles.

Tom B
 
1

100

Hi Tom,
When you start a new thread the new thread and the main thread do execute at
the same time. How ever you have a couple of problems in the code:
1. The main thread owns the form and all controls on it. Updating the form
or its controls form outside the owning thred thread is wrong and has to be
avoided. So the code like

lblWorking.Text=DateTime.Now.ToString();
lblWorking.Refresh();

in the worker thread is wrong.

Insted, you should use Control.Invoke to start a method in the main thread's
contex that can update the UI

2. Looping in the main thread stops the message loop and no win messages are
processed. So I'm not sure that calling Refresh method will get through
until resuming the message loop. So the worker thread works, but the UI gets
updated when the loop in the main thread is done.

HTH
B\rgds
100
 
T

Tom B

I've been reading the articles suggested by Michael Mayer, and now see the
errors in my ways.
Thanks for taking the time to answer.
 
T

Tom B

I've been reading more stuff and have found the problems you mention.
Thanks for taking the time to respond.
 
A

Alvin Bruney

you sir, need to be commended for bravely venturing into the threaded world.
threading is easy to do but difficult to master. good luck. post here often
the community is very knowledgeable on advanced issues
 

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

Similar Threads


Top