Skip to content

AsyncActions

Ca5an0va edited this page Dec 15, 2017 · 6 revisions

AsyncActions is a SignalGo class that helps you to run a method in another thread. This is a typical usage:

 public void YourMethod()
        {
            string result;
            AsyncActions.Run(() =>
            {
                result = //your long running code here
                label1.InvokeIfRequired(() => label1.Text = result);  //See extension method below
            }, (ex) =>
            {
                //when your code has exception
            });
            // other code here... but it will be executed in parallel with AsyncActions block!
        }                

Please note that since code will run in another thread be carefull when refreshing UI controls present in this Action!! You must use BeginInvoke/EndInvoke patterns for UI controls or use an extension method like this:

        public static void InvokeIfRequired(this Control control, Action code)
            {
            if (control.InvokeRequired)
                {
                control.BeginInvoke(code);
                }
            else
                {
                code.Invoke();
                }
            }

AsyncActions IS NOT awaitable. So your code runs in parallel with other code eventually placed after the AsyncActions block itself.