C# Threading

G

Guest

Under my button click event, I have code that will call another method that takes 3-4 seconds. I want to be call that method and be free to do something else. I know this can be done with threading, but I just can't seem to get it right. Below is what I have so far. The Join statement is what is holding up the code.

Any suggesstions

Thanks

private void btnSend_Click(object sender, System.EventArgs e

this.Cursor = System.Windows.Forms.Cursors.WaitCursor;

Thread thread1 = new Thread(new ThreadStart(sendMsg))
thread1.Name= "JMS"
thread1.Start();

int count=0
thread1.Join();

while (this.msgReceived == false

count++
Console.WriteLine(thread1.ThreadState.ToString())
Console.WriteLine("sendMsg: " + count);
 
J

Jon Skeet [C# MVP]

Jason said:
Under my button click event, I have code that will call another
method that takes 3-4 seconds. I want to be call that method and be
free to do something else. I know this can be done with threading,
but I just can't seem to get it right. Below is what I have so far.
The Join statement is what is holding up the code.

Yes - the Join statement waits for the other thread to die. Without the
call to Join, your thread is free to do other stuff. Why did you have
the call to Join in there?
 
J

Jon Skeet [C# MVP]

[It helps if you quote some of what you're replying to]
Because that was the only way that the method that I was calling
would work.

Well, the method you were calling would execute either way. Thread.Join
is just going to make the thread wait until that other thread has
finished. What were you actually trying to do? In what way didn't it
work without the call to Thread.Join?
 
J

Jon Skeet [C# MVP]

Jason said:
OK, here is what I want to do. It is real simple. I want my button
click event to call the sendMsg method, which takes about 2-3 seconds
to execute and then still be able to execute other code in the button
click event such as the looping code listed below. Is this possible
to do?

The problem is with the "execute other code in the button click event".
While your button click event is executing, no other UI events can
occur. The correct way round this is to make the button click event
just start the other thread, and make the other thread itself tell the
UI that it's finished by invoking a delegate (using Control.Invoke or
Control.BeginInvoke).
 
G

Guest

Sounds good. Can you point me into the right direction in order to setup the delegates for the other thread to use?
 
J

Jon Skeet [C# MVP]

Jason said:
Sounds good. Can you point me into the right direction in order to
setup the delegates for the other thread to use?

You can create any delegate you like, although you may want to make it
follow the EventHandler pattern (object sender, EventArgs e). (I
believe EventHandlers are more efficient for Control.Invoke, although I
wouldn't put too much stock in that.)

I'm writing an article about multithreading which will soon cover
Windows Forms and give an example of this kind of thing - hopefully
over the weekend it'll be finished. It's at
http://www.pobox.com/~skeet/csharp/multithreading.html and is gradually
getting there...
 

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