Reputation: 865
I have a class A, which has two functions ("Download" and "query"). When the function "Download" is finished, it will fire the "finish" event. Since the function "query" will use the return info from the finished "Download" function, I want when function "query" is called from a class outside A (e.g., Class B), the function will wait to execute until the "Download" is finished. Class B won't recognize "query" function in A is waiting on the "Download" finish. In other words, I don't want in class B's download finish event handler to call the "query" function.
Class A
{
public Event finish;
public A()
{
Download();
}
private void Download ()
{
finish(this, new EventArgs());
}
public void query ()
{
}
}
Class B
{
A a = new A();
a.query();
}
Thanks,
Wei
Upvotes: 0
Views: 1850
Reputation: 11760
You have to synchronize the methods Download and query. Therefore you can use AutoResetEvents:
class A
{
AutoResetEvent are = new AutoResetEvent(false);
public void Download()
{
are.Reset();
try
{
//Do your work here
}
finally
{
are.Set();
}
}
public void Query()
{
are.WaitOne();
}
}
This will hold on every thread that enters the method Query until the reset event is set.
But if Download is run synchronously, you don't have to synchronize them. As the constructor of A is called from B, B cannot call query before Download has finished.
Upvotes: 1
Reputation: 19
in query use Backgroundworker with RunWorkerAsync() method. Execute download in bw.RunWorkerCompleted and fire event in RunWorkerCompleted handler.
Upvotes: 2