views:

45

answers:

3

My code calls a Web service method which takes a few minutes to perform the operation. During that time my window becomes non responsive and it shows a complete white screen.

I don't want to the call method from a different thread.

It it the best way to handle it?

Environment: C#, web service

+1  A: 

The BackgroundWorker is your friend.

Here's an example of how I use a BackgroundWorker with a WebService. Basically, there's no way to do intensive operations on the UI side without using a separate thread. The BackgroundWorker is the nicest way of running on a separate thread.

GenericTypeTea
A: 

You can make the request on a separate thread, which will leave the UI thread responsive. You'll need to synchronise the response back to the UI thread once you've finished.

Sohnee
I have used different thread but it is still unresponsive
Buzz
this normally happens when i switch between applications
Buzz
+1  A: 

To have a responsive UI, you must use another thread.

But if you use visual studio, the generated client class have asynchronous method signatures wich would do it for you. If your method is "GetData", then you should have a method called "GetDataAsync" wich would not freeze your window.

Here is an example :

WsClient client;
protected override void Load() {
    base.Onload();
    client = new WsClient();
    client.GetDataCompleted += new GetDataCompletedEventHandler(client_GetDataCompleted);
}

//here is the call
protected void Invoke()
{
    client.GetDataAsync(txtSearch.Text);
}

//here is the result
void client_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
    //display the result
    txtResult.Text = e.Result;
}
Manitra Andriamitondra
I have used different thread but it is still unresponsive.
Buzz
Then, could you post some code to help us understand the problem ?
Manitra Andriamitondra