Daniel Szalay
Daniel Szalay

Reputation: 4101

How to call web service without blocking execution of client?

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.

Upvotes: 0

Views: 1827

Answers (4)

Martin Risell Lilja
Martin Risell Lilja

Reputation: 652

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!

Upvotes: 2

silvino.barreiros
silvino.barreiros

Reputation: 99

I would recommend looking into BackgroundWorkers..

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

proxyWorker.RunWorkerAsync();

Upvotes: 0

L.B
L.B

Reputation: 116108

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

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

Upvotes: 1

Marco
Marco

Reputation: 57573

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();
}

Upvotes: 3

Related Questions