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.