Reputation: 1621
I need to run a SSIS package from code behind asyncronously so that it does not block the UI thread. Currently I am using a creating a simple thread to handle this without blocking the UI thread. However, after reading some blog posts I wanted to try the async/await functionality as creating a new thread is resource intensive when compared to async/await.
The code I have written is this.
protected void Page_Load(object sender, EventArgs e)
{
RegisterAsyncTask(new PageAsyncTask(SSISAsync));
}
public async void Button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Before calling task");
await SSISAsync();
Debug.WriteLine("After calling task");
}
public Task<int> SSISAsync()
{
//Sample task to keep the task doing some work.
return Task.Run(() =>
{
for (int i = 0; i < 100000; i++)
{
for (int j = 0; j < 10000; j++)
{
int k = i * j;
//Nothing LOL
}
}
Debug.WriteLine("from thread");
return 1;
});
}
What I expect to happen is that when the Button1_Click
method will execute synchronously up until it encounters a await
keyword and then it call the SSISAsync
on a separate thread from the thread pool and not on the UI thread. So, ideally this not be blocking the UI thread when the Task is being executed.
But my UI is blocking i.e) The loading gif on the tab in IE keeps on going until the Task is completed.
I have seen a few similar questions in SO. But most of them were related to WPF and none of the solutions really worked out.
Upvotes: 3
Views: 1288
Reputation: 456527
There's some misconceptions here.
So, ideally this not be blocking the UI thread when the Task is being executed.
First, there's no such thing as a UI thread on ASP.NET.
But my UI is blocking i.e) The loading gif on the tab in IE keeps on going until the Task is completed.
What you're really looking for is a way to return early from an ASP.NET request. As I describe on my blog, async
has nothing to do with returning early.
In fact, since you're loading it into a browser, I think returning early is not going to work at all. What you really want to do is return a page that then uses AJAX to load whatever else it needs. AJAX is naturally asynchronous (on the client side), so that kind of approach should allow you to quickly load a "loading..." page and then keep the UI (browser) responsive while loading the rest.
Upvotes: 3