Workflow Foundation: Asynchronous operations (lengthy network I/O)
I have to create an application that will be started a few times per day (it's non - interactive).
To operate, it needs LARGE amounts of data from the Internet (megabytes) via a rather slow con开发者_开发问答nection, so the WCF service calls take quite some time.
At the same time, it needs to perform local calculations and has a sophisticated initialization process.
So, what I want to do is to create a workflow that asynchronously fetches the data (takes a few minutes) while already initializing / calculating locally.
Is there a way to accomplish this?
You could use the BackgroundWorker component for each async operation - one for the calculations and one for the data download.
My suggestion would be to use one thread for each operation. After you start each thread, call thread.Join() one each thread to wait for each thread to finish.
e.g.
Thread[] workers = new Thread[2];
workers[0] = new Thread(dataDownloader.Fetch);
workers[1] = new Thread(calculator.DoComplexCalculations);
foreach(Thread t in workers)
{
t.Start();
}
foreach(Thread t in workers)
{
t.Join();
}
You can generate the async classes of your wcf service (using the commandlinetool). If you run these async methods, they return immediatly and call a callback delegate when they have finished. This way you are free to do all your calculations while you wait
精彩评论