Notify Async operation in C#




Threading is a nice thing until we know how it is being executed. Sometime it can be a pain in the arse and sometimes asset you gain from. Today came across a question on how to get to know a async operation is completed thought of spending some time to explain it in my way.
How do you get to know a Async operation is completed, well one way is IAsyncResult,this is how we go,
Lets create a delegate that will be used call our time consuming or background task.
delegate string CallFunctionDelegate(string arg1, string arg2);
say the time consuming function

private string DoSomeThingBig(string arg1, string arg2)

just to show how to pass argument, i have used some dummy arguments.

Once the time consuming function is completed lets call the notification function that will called after the asynchronous operation let that be

private void AfterDoingSomethingBig(IAsyncResult result)


Now for this kind of function signature we have a delegate class called AsyncCallback
next will create an instance for that delegate,

AsyncCallback CallBackAfterAsynOperation = new AsyncCallback(AfterDoingSomethingBig);


combining all sample will look like this


        delegate string CallFunctionDelegate(string arg1, string arg2);
 
        private void btnStart_Click(object sender, EventArgs e)
        {
            CallFunctionDelegate delegRunApps = new CallFunctionDelegate(DoSomeThingBig);
 
            AsyncCallback CallBackAfterAsynOperation = new AsyncCallback(AfterDoingSomethingBig);
 
            delegRunApps.BeginInvoke("""", CallBackAfterAsynOperation, null);
        }
 
        private string DoSomeThingBig(string arg1, string arg2)
        {
            #region Implemetation of time consuming function
            //Implemetation of time consuming function
 
            for (int i = 0; i < 5; i++)
            {
                Thread.Sleep(1000);
 
                if (btnStart.InvokeRequired)
                {
                    btnStart.Invoke((new MethodInvoker(delegate { btnStart.Text = i.ToString(); })));
                }
                else
                {
                    btnStart.Text = i.ToString();
                }
            } 
            #endregion
 
            return arg1.Replace("freetime", arg2);
        }
 
        private void AfterDoingSomethingBig(IAsyncResult result)
        {
            MessageBox.Show("Finaly Done!! ;) ");
 
            btnStart.Invoke((new MethodInvoker(delegate { btnStart.Text = "Start"; })));
        }


the blow line was used just demo the threading in the user interface so had to set the text of the UI element in a separate delegate, if tried to set UI element property in current thread it would throw a cross threading blah blah error.

btnStart.Invoke((new MethodInvoker(delegate { btnStart.Text = i.ToString(); })));

Binary Files
Source files

Comments

Popular posts from this blog

Launch .Net Core with VSCode for starters

Chrome in a nut shell

Developing SMS Services in C#