0

I have a Windows Forms application which makes calls to web services via proxies generated with SvcUtil from WSDL descriptors. These calls can last for minutes, and during this time I don't want the client app to 'freeze out'. What do I have to do to achieve this? I guess something Threading related, but I'm not sure how to manage return values and parameters in that case.

Daniel Szalay
  • 4,041
  • 12
  • 57
  • 103

4 Answers4

3

You could use a BackgroundWorker.

private void wrk_DoWork(object sender, DoWorkEventArgs e)
{
    // Do your work here
}

private void wrk_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Executed when worker completed its execution
}

private void StartIt()
{
    BackgroundWorker wrk1 = new BackgroundWorker();
    wrk1.DoWork += wrk_DoWork;
    wrk1.RunWorkerCompleted += wrk_RunWorkerCompleted;
    wrk1.RunWorkerAsync();
}
Marco
  • 56,740
  • 14
  • 129
  • 152
2

I'd go for a background worker.

Set the RunWorkerCompleted event and DoWork, run it and when you get your result in DoWork, set the event argument to your result (e.Result).

BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    // Do your processing
    e.Result = result;
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
     ResultLabel.Text = (string)e.Result;
}

The examples aren't tested, but your IDE should help you out. Also you will have to resolve the BackgroundWorker, or just add

using System.ComponentModel;

More information here: http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

Hope it helps!

1

You can use methods that start with Begin......

e.g, use BeginAbc() instead of Abc()

L.B
  • 114,136
  • 19
  • 178
  • 224
0

I would recommend looking into BackgroundWorkers..

BackgroundWorker proxyWorker = new BackgroundWorker();
proxyWorker.DoWork +=
   (sender, args) =>
   {
     //make proxy call here
   };

proxyWorker.RunWorkerAsync();