Nerseus
Danner
In .NET 1.x, I got back an IAsynchResult (I think) object when I called BeginMethodName. I could hold onto the handle until I really needed the data, then call a Wait method to make sure the webmethod returned (or errored out).
In .NET 2.0, they've implemented the Event-based Asynchronous Pattern. In this pattern, I hook up an event handler and then call MethodNameAsync. My trouble is, I can't figure out how to wait for method to finish from within WinForms.
The scenario is this:
Form1 is a search form that uses a static "Lookups" class. The "Lookups" class exposes a method that makes an asynchronous webmethod call to get some standard lookup tables from the DB.
Form1 launches Form2 some time later. Form2 MUST have the lookups loaded so it needs to wait (or block) until the asynchronouse Lookups class has completed it's webmethod.
I tried adding another static method in my "Lookups" class that goes into a "while(...) Sleep" loop, to wait for the Lookups to complete but that seems to block forever.
What's the best way to use the asynch method and wait until it's done or at least safely check that it's done?
Here's the relevent code:
From above, Form1 would call StartLookupsAsync(). Later, Form2 calls GetLookups. Since the asynch call was made, gettingLookupsAsync is true and GetLookups enters the while loop. The while loop appears to block forever. This is my failed attempt at getting it to work.
If the event handler fires before GetLookups is called (which is most of the time), then everything works fine.
I need help! (please!)
-ner
In .NET 2.0, they've implemented the Event-based Asynchronous Pattern. In this pattern, I hook up an event handler and then call MethodNameAsync. My trouble is, I can't figure out how to wait for method to finish from within WinForms.
The scenario is this:
Form1 is a search form that uses a static "Lookups" class. The "Lookups" class exposes a method that makes an asynchronous webmethod call to get some standard lookup tables from the DB.
Form1 launches Form2 some time later. Form2 MUST have the lookups loaded so it needs to wait (or block) until the asynchronouse Lookups class has completed it's webmethod.
I tried adding another static method in my "Lookups" class that goes into a "while(...) Sleep" loop, to wait for the Lookups to complete but that seems to block forever.
What's the best way to use the asynch method and wait until it's done or at least safely check that it's done?
Here's the relevent code:
Code:
partial class Lookups
{
private static Lookups lookups;
private static bool gettingLookupsAsynch = false;
public static void StartLookupsAsync(string wsUrl)
{
// If lookups are already in memory, get out
if (lookups != null) return;
gettingLookupsAsynch = true;
WebService webService = new WebService();
webService.Url = wsUrl;
webService.GetLookupsCompleted +=
new GetLookupsCompletedEventHandler(Lookups_GetLookupsCompleted);
webService.GetLookupsAsync();
}
public static Lookups GetLookups(string wsUrl)
{
// If we already have the lookups, just return them
if (lookups != null) return lookups;
if (gettingLookupsAsynch)
{
// This is the problem code
[b]
while (lookups == null)
{
System.Threading.Thread.Sleep(1);
}
[/b]
}
else
{
// Not getting async, get them and wait
WebService webService = new WebService();
webService.Url = wsUrl;
lookups = webService.GetLookups();
}
return lookups;
}
static void Lookups_GetLookupsCompleted(object sender, WebService.GetLookupsCompletedEventArgs e)
{
lookups = e.Result;
}
}
From above, Form1 would call StartLookupsAsync(). Later, Form2 calls GetLookups. Since the asynch call was made, gettingLookupsAsync is true and GetLookups enters the while loop. The while loop appears to block forever. This is my failed attempt at getting it to work.
If the event handler fires before GetLookups is called (which is most of the time), then everything works fine.
I need help! (please!)
-ner