-3

I have build one windows application where in I have one button name submit on Click on this button it takes 3 min to execute the sql command mean while I need to perform other actions on the form as the previous thread should run at back end Process.

how can I achieve this scenerio..

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
Kapil
  • 1,823
  • 6
  • 25
  • 47
  • 1
    Which programming language/platform you are using? – rkosegi Jun 28 '13 at 07:14
  • sry I am using c#.Net – Kapil Jun 28 '13 at 07:31
  • Look at this: http://msdn.microsoft.com/en-us/library/jj152938.aspx. Recommendation would be to use TAP as it is very easy to learn and a little more difficult to get wrong. That said it is very easy to get multithreading wrong. No one here will just provide you a full solution, you will need to build it yourself. I suggest you find some good guides and start learning then come back when you have a more specific question. A good start for TAP is here: http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx – Dave Williams Jun 28 '13 at 07:45
  • The title of your question does not match the actual question you have posted, so it is very unclear what is being asked here – Matt Wilko Jun 28 '13 at 10:07

2 Answers2

2

Use the BackgroundWorker control, you can read the tutorial on the topic on MSDN

Basically, on the button's click event you execute:

if(!bw.IsBusy)
    bw.RunWorkerAsync();

This will run any code you put into backgroundworker's DoWork event on a background thread.

After your code finishes, it will execute RunWorkerCompleted event that will run on the main thread, and that's where you can proceed to update stuff that you couldn't before due to cross-threading.

Note that you can pass parameters to the DoWork event and results from DoWork to RunWorkerCompleted event.

Vedran
  • 10,369
  • 5
  • 50
  • 57
2

Addressing the title of your question,
Look at the following code from this post:

public delegate void InvokeDelegate();

private void Invoke_Click(object sender, EventArgs e)
{
   myTextBox.BeginInvoke(new InvokeDelegate(InvokeMethod));
}
public void InvokeMethod()
{
   myTextBox.Text = "Executed the given delegate";
}

Use the above to have a different thread access UI elements on the form.

For some collection of more advanced approaches to this, take a look at this SO post.

Community
  • 1
  • 1
Liel
  • 2,407
  • 4
  • 20
  • 39