mhdi
asked on
c# Tasks - How to wait for events
Hi,
I have some code which initiates a download. This results in one of the following async events being triggered. I need the application to wait for one of these events before continuing.
Is this a job for tasks? Can I create a task with my download code which returns 'task complete' when one of the 3 events below fire?
I have some code which initiates a download. This results in one of the following async events being triggered. I need the application to wait for one of these events before continuing.
Is this a job for tasks? Can I create a task with my download code which returns 'task complete' when one of the 3 events below fire?
(stdFile_.saver as _IStreamCollectorEvents_Event).stopped += (collector) =>
{
Console.WriteLine("Download stopped");
};
(stdFile_.saver as _IStreamCollectorEvents_Event).failed += (sender, error) =>
{
Console.WriteLine("Download failed");
};
(video as _IStream_Events_Event).stopped += (s) =>
{
Console.WriteLine("Download Complete!");
};
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You just need the AutoResetEvent, and set it
autoEvent.Set();
autoEvent.Set();
ASKER
@chaau - I need the ability to signal the main thread whether the download completed successfully or not. Can I do this with the "AutoResetEvent"? Or do I keep a separate variable which holds the download status. eg "bool downloadComplete = false" which gets changed to "true" when the download complete event is fired.
@Tchuki - I like your proposed solution, but how do I tie in the events mentioned in my top post?