Reputation: 966
I have a requirement where a client makes a request to a single web page, this web page needs to retrieve the results from two other web pages.
The two web pages that we are requesting info from carry out a process before returning the result, this process can take a few minutes so rather than starting a web request and waiting for it to return and then starting another I want to fire off both requests in background workers and have the results being generated in parallel.
So my code looks something like this (Pseudo code):
var worker1 = new BackgroundWorker();
var worker2 = new BackgroundWorker();
worker1.DoWork += (s, e) => {
//Web Request done here
};
worker2.DoWork += (s, e) => {
//Web Request done here
};
worker1.RunWorkerAsync();
worker2.RunWorkerAsync();
//Some logic here that basically waits for both to return.
Now I noticed that the results were still taking longer to return than I expected so I did a bit of a test and changed the contents of the Dowork to
using (var sw = new StreamWriter(@"c:\temp\TEST\Worker1.txt"))
{
for (int i = 0; i < 1000000; i++)
{
sw.WriteLine($"{i} - {DateTime.Now.ToLongTimeString()}");
}
}
For brevity I wont copy this back out but assume that the file name is changed to worker2.txt in the second workers DoWork()
Its crude I know, but it works, its takes about 3 seconds to write out the time 1 million times so in the first file I see times ranging from Execution Time to Execution Time + 3 seconds In the second file I can see times ranging from Execution time + 3 Seconds to Execution Time + 6 seconds.
So it’s clear at this point that one worker is being executed first and then the other, but I cannot see why.
More info about the project - Web forms (This is a requirement I have been given and have to stick to) .net 4.5
**Edited to add : **
After coming back to this problem I have had a look at the pages that I am calling and notice that they have aspcompat=true in the page declaration, which I assume is what is causing the issue (They require this, I cant remove it).
What are my options now? Ideally I want to be able to call these pages to run in parallel, is there any way to still achieve this with the aspcompat set to true?
Editing for future readers
Note that the problem that I described above and the code that was provided were correct, the issue that I had was that the pages that I was calling were set to use ASPCOMPAT which forces webforms to work in STA mode which is what was throwing me off.
In the end the code above did work unchanged, the solution was to put the page with the background workers in a separate web application with its own app pool, that page the n used the background workers to call off to the pages running in aspcompat mode in their application pool - its not ideal, but its what we wanted for the live deployment anyway.
Upvotes: 0
Views: 176
Reputation: 897
You could skip the BackgroundWorker
and use Task.WaitAll()
instead.
void Main()
{
Task.WaitAll(FirstRequest(), SecondRequest());
}
// Define other methods and classes here
private async static Task FirstRequest()
{
// Do your work for one of the WebRequests here
await Task.Delay(3000);
Console.WriteLine("Done with 1");
}
private async static Task SecondRequest()
{
// Do your work for the second WebRequest here
await Task.Delay(3000);
Console.WriteLine("Done with 2");
}
Upvotes: 1