2

Alright so I am having a hard time figuring out how to wait for the thread to finish before continuing along with the code without freezing up the windows form..

example code:

var ct = new System.Threading.Thread(solve);
ct.IsBackground = true;
ct.Name = "Solving";
//start our thread
ct.Start();
ct.Join();
label33.Invoke((MethodInvoker)delegate { label33.Text = "Submitting..."; });
clickPost("name", textBox27.Text);
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user372671
  • 35
  • 1
  • 2
  • 7

2 Answers2

1

The call to ct.Join() only has one purpose - to block until the thread in question completes.

If you don't want to block, you need to handle notification of the completion of the background "work" via some other mechanism, such as an event.

This is greatly simplified for Windows Forms by using a BackgroundWorker, which does the notification for you (on the correct user interface thread).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

If I understand you correctly all you need is to create callback for some async operation. You can use delegate's BeginInvoke callback parameter. Keep in mind the delegate and its callback will be executed in own thread:

Action backgroundAction = new Action(DoAction);
backgroundAction.BeginInvoke(ActionCallback, null);

private void DoAction()
{
    //some background task
}

private void ActionCallback(IAsyncResult result)
{
   //will be executed after
}