コード例 #1
0
        public ActionResult _Response_PartialView(string threadType)
        {
            #region Initialization

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            ProcessingResult result = new ProcessingResult();

            Debug.WriteLine("About to run process....");
            Debug.WriteLine("Controller is on Thread: " + Thread.CurrentThread.ManagedThreadId);

            #endregion

            result.Description = "Task will still be running in the background. View trace statements in your 'Output' window. You may also fire additional tasks once control returns to the view.";

            switch (threadType)
            {

                case "basic-thread":

                    //BEST method for ASP.NET:
                    //Fire & Forget on a background thread
                    System.Threading.ThreadStart threadStart = delegate
                    {
                        Methods.ParallelMethods.VoidWithParallelProcessing();

                    };
                    System.Threading.Thread thread = new System.Threading.Thread(threadStart);
                    thread.IsBackground = true;
                    thread.Start(); //<-- Preferred option fpr Fire/Forget when considering not disturbing the ThreadPool in ASP.NET.

                    //Simple method:
                    //Also, don't forget to use EXTENSIVE ERROR HANDLING in your routine because any unhandled exceptions outside of a debugger will abruptly crash your application:
                    //ThreadPool.QueueUserWorkItem(o => Methods.ParallelMethods.VoidWithParallelProcessing()); //<-- Preferred option fpr Fire/Forget when considering performance (but not for ASP.NET).

                    //Alternate option:
                    //Task.Run(() => Methods.ParallelMethods.AsyncVoidWithParallelProcessing());  // <--- Task is preferred Option when not handling fire/forget scenarios: http://blogs.msdn.com/b/pfxteam/archive/2009/10/06/9903475.aspx

                    result.Message = "Parallel Call Complete!";
                    result.AlertType = "alert-success";;

                    break;

                case "detailed-thread":

                    //w/ Thread Details
                    //By using this method over ThreadPool.QueueUserWorkItem you can name your new thread to make it easier for debugging.
                    //Also, don't forget to use EXTENSIVE ERROR HANDLING in your routine because any unhandled exceptions outside of a debugger will abruptly crash your application:
                    (new Thread(() =>
                    {
                        Methods.ParallelMethods.VoidWithParallelProcessing();
                    })

                    {
                        Name = "Long Running Work Thread (AsyncVoidWithParallelProcessing)",
                        Priority = ThreadPriority.AboveNormal
                    }).Start();

                    result.Message = "Parallel Call Complete!";
                    result.AlertType = "alert-success";

                    break;

                case "exception":

                    throw new System.Exception("Exception message");

                    break;

                case "exception-catch":

                    try
                    {

                        throw new System.Exception("Exception message");
                    }
                    catch(Exception e)
                    {
                        result.Message = "Exception Caught!";
                        result.AlertType = "alert-error";
                    }

                    break;

            }

            #region Response

            Debug.WriteLine("Control is back origin thread.");
            Debug.WriteLine("Controller is on Thread: " + Thread.CurrentThread.ManagedThreadId);

            stopWatch.Stop();
            result.TimeElapsed = stopWatch.ElapsedMilliseconds;
            result.MethodType = "Thread.Sleep()";

            return PartialView(result);

            #endregion
        }
コード例 #2
0
        [HandleError(ExceptionType=typeof(TimeoutException), View="_TimeoutError")] //<-- Handle the Timeout Error
        public async Task<ActionResult> _Response_Timeout(string type, CancellationToken cancellationToken) //<-- Cancellation Token
        {
            #region Initialization

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            ProcessingResult result = new ProcessingResult();

            Debug.WriteLine("About to run process....");
            Debug.WriteLine("Controller is on Thread: " + Thread.CurrentThread.ManagedThreadId);

            #endregion

            switch(type)
            {
                case "timeout":

                    // call is awaited, so Timeout Exception will not stop this thread, it will only disconnect the callback:
                    result.Result = await (new Methods.BasicMethods()).AsyncStringResultProcessingTask();

                    result.Description = "Result = '" + result.Result + "'";
                    result.Message = "Timeout Results Task Complete!";
                    result.AlertType = "alert-error";
                    result.MethodType = "Timeout: ";

                    break;

                case "timeout-handle-exception":

                    //Tell cancellation token to throw if exception occurs:
                    cancellationToken.ThrowIfCancellationRequested();

                    try
                    {
                        result.Result = await (new Methods.BasicMethods()).AsyncStringResultProcessingTask();

                        result.Description = "Result = '" + result.Result + "'";
                        result.Message = "Timeout Results Task Complete!";
                        result.AlertType = "alert-error";
                        result.MethodType = "Timeout: ";
                    }
                    catch(Exception e)
                    {

                    }

                    break;
            }

            #region Response

            Debug.WriteLine("Control is back origin thread.");
            Debug.WriteLine("Controller is on Thread: " + Thread.CurrentThread.ManagedThreadId);

            stopWatch.Stop();
            result.TimeElapsed = stopWatch.ElapsedMilliseconds;


            return PartialView(result);

            #endregion 
        }
コード例 #3
0
        public async Task<ActionResult> _Response_PartialView(string type)
        {
            #region Initialization

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            ProcessingResult result = new ProcessingResult();

            Debug.WriteLine("About to run process....");
            Debug.WriteLine("Controller is on Thread: " + Thread.CurrentThread.ManagedThreadId);

            #endregion

            result.Description = "Task will still be running in the background. View trace statements in your 'Output' window. You may also fire additional tasks once control returns to the view.";

            switch (type)
            {

                case "basic-parallel":

                    //Simple method:
                    await Methods.ParallelMethods.AsyncVoidWithParallelProcessingTask();  // <--- Task is preferred Option when not handling fire/forget scenarios: http://blogs.msdn.com/b/pfxteam/archive/2009/10/06/9903475.aspx

                    result.Description = "You'll notice this call is EXACTLY the same as the 'Parallel Tasks (Standard)' button in the 'Standard' section, with the exception that the Controller ActionResult is async and this method is awaited.";
                    result.Message = "Basic Async Task Complete!";
                    result.AlertType = "alert-success";
                    result.MethodType = "AsyncVoidWithParallelProcessingTask";

                    

                    break;


                case "void":

                    await Methods.BasicMethods.AsyncVoidProcessingTask();

                    result.Description = "";
                    result.Message = "Async Void Task Complete!";
                    result.AlertType = "alert-success";
                    result.MethodType = "AsyncVoidProcessingTask";

                    break;

                case "string-response":

                    result.Result = await (new Methods.BasicMethods()).AsyncStringResultProcessingTask();

                    result.Description = "Result = '" + result.Result + "'";
                    result.Message = "Await Results Task Complete!";
                    result.AlertType = "alert-success";
                    result.MethodType = "AsyncStringResultProcessingTask";


                    break;

                case "multiple-tasks-awaited":

                    //Run Tasks in order using await:
                    string str = await (new Methods.BasicMethods()).AsyncStringResultProcessingTask();
                    await Methods.BasicMethods.AsyncVoidProcessingTask();
                    await Methods.BasicMethods.AsyncVoidProcessingTask(); 
                    await Methods.ParallelMethods.AsyncVoidWithParallelProcessingTask(); 

                    result.Description = "Result = '" + result.Result + "'";
                    result.Message = "Multiple Tasks Complete!";
                    result.AlertType = "alert-success";
                    result.MethodType = "AsyncStringResultProcessingTask, AsyncVoidProcessingTask & AsyncVoidWithParallelProcessingTask";


                    break;

                case "multiple-tasks-when-all":


                    //Set up our same Tasks in order used in Synchronous version:
                    var task1 =  (new Methods.BasicMethods()).AsyncStringResultProcessingTask();
                    var task2 = Methods.BasicMethods.AsyncVoidProcessingTask();
                    var task3 = Methods.BasicMethods.AsyncVoidProcessingTask();
                    var task4 = Methods.ParallelMethods.AsyncVoidWithParallelProcessingTask();

                    //Run them in parallel and continue:
                    await Task.WhenAll(task1, task2, task3);


                    result.Description = "Result = '" + result.Result + "'";
                    result.Message = "Multiple Tasks Complete!";
                    result.AlertType = "alert-success";
                    result.MethodType = "AsyncStringResultProcessingTask, AsyncVoidProcessingTask & AsyncVoidWithParallelProcessingTask";


                    break;
            }



            #region Response

            Debug.WriteLine("Control is back origin thread.");
            Debug.WriteLine("Controller is on Thread: " + Thread.CurrentThread.ManagedThreadId);

            stopWatch.Stop();
            result.TimeElapsed = stopWatch.ElapsedMilliseconds;


            return PartialView(result);

            #endregion 


        }
コード例 #4
0
        public ActionResult _Response_PartialView(string type)
        {
            #region Initialization

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            ProcessingResult result = new ProcessingResult();

            Debug.WriteLine("About to run process....");
            Debug.WriteLine("Controller is on Thread: " + Thread.CurrentThread.ManagedThreadId);

            #endregion

            switch (type)
            {

                case "basic-parallel":

                    //Simple method:
                    Methods.ParallelMethods.VoidWithParallelProcessing();  // <--- Task is preferred Option when not handling fire/forget scenarios: http://blogs.msdn.com/b/pfxteam/archive/2009/10/06/9903475.aspx

                    result.Description = "You'll notice this call is EXACTLY the same as the 'Parallel Tasks (Await)' button in the 'Await' section, with the exception that the Controller ActionResult is not async and the method is not awaited. ";
                    result.Message = "Basic Task Complete!";
                    result.AlertType = "alert-success";
                    result.MethodType = "VoidWithParallelProcessing";

                    break;

                case "void":

                    Methods.BasicMethods.VoidProcessing();

                    result.Description = "";
                    result.Message = "Standard Void Task Complete!";
                    result.AlertType = "alert-success";
                    result.MethodType = "VoidProcessing";

                    break;

                case "string-response":

                    result.Result = (new Methods.BasicMethods()).StringResultProcessing();

                    result.Description = "Result ='" + result.Result + "'";
                    result.Message = "Standard Results Task Complete!";
                    result.AlertType = "alert-success";
                    result.MethodType = "StringResultProcessing";

                    break;

            }

            #region Response

            Debug.WriteLine("Control is back origin thread.");
            Debug.WriteLine("Controller is on Thread: " + Thread.CurrentThread.ManagedThreadId);

            stopWatch.Stop();
            result.TimeElapsed = stopWatch.ElapsedMilliseconds;

            return PartialView(result);

            #endregion
        }