The Web Service FREEZE the App
-
I have a program that connects to a Web Service to authenticate a user. When the user clicks ok the program access the Web Service through the internet. This actually seems to FREEZE the program until it has finished, how can I solve this problem? It's really not a good user interface like now! I mean maybe I could make like when you are loggin in in MSN Messenger, but how to do it? Thankx! "Nelle cose del mondo non e' il sapere ma il volere che puo'."
-
I have a program that connects to a Web Service to authenticate a user. When the user clicks ok the program access the Web Service through the internet. This actually seems to FREEZE the program until it has finished, how can I solve this problem? It's really not a good user interface like now! I mean maybe I could make like when you are loggin in in MSN Messenger, but how to do it? Thankx! "Nelle cose del mondo non e' il sapere ma il volere che puo'."
What you are seeing is that all the work is being done on one thread, including all the waiting that you have to do while you wait for the webservice to respond. The solution is to use an Async method or a separate thread. The Async method will use a separate thread but it takes care of all the work for you; while you have to do all the work to use a thread yourself. If I remember correctly the code that the "Add Web Reference" option generates includes some Async methods. The Async methods begin with
Begin
andEnd
followed by the name of the method. Using a method from the CodeProject webserviceArticleBrief[] GetLatestArticleBrief(int NumArticles);
The Async versions of this method are:IAsyncResult BeginGetLatestArticleBrief(int NumArticles, AsyncCallback callback, object asyncState);
andArticleBrief[] EndGetLatestArticleBrief(IAsyncResult asyncResult);
Typical usage is as suchIAsyncResult iar = BeginGetLatestArticleBrief(numArticles, null, null);
// Later on
if( iar.IsCompleted )
{
articleBriefs = EndGetLatestArticleBrief(iar);
}
else
{
// do something else
}Since you wouldn't be doing anything else while the user is logging in I would do this:
IAsyncResult iar = BeginLoginUser(username, password, null, null);
while( !iar.IsCompleted )
{
Application.DoEvents(); // Let the message pump do some work
}// Continue as before
This is only a brief intro and a limited way of doing things, MSDN contains an entire section on Asynchronous programming which you should read before going much further. If you use the MSDN that came with VS.NET I found the topic by going to the following nodes: Visual Studio.NET -> .NET Framework -> Programming with the .NET Framework -> Including Asynchronous Calls HTH, James "Java is free - and worth every penny." - Christian Graus