3

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.

1
  • 4
    There is no notion of UI thread in asp.net at the server side, UI is rendered at the client side by the web browser. You possibly misunderstand then how async/await in asp.net differs from wpf. If this is so, read this msdn entry by Stephen Cleary msdn.microsoft.com/en-us/magazine/dn802603.aspx Commented Jun 6, 2016 at 12:52

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.