Tutorial by Examples

When ASP.NET handles a request, a thread is assigned from the thread pool and a request context is created. The request context contains information about the current request which can be accessed through the static HttpContext.Current property. The request context for the request is then assigned t...
See below for a simple example of how to use async/await to do some time intensive stuff in a background process while maintaining the option of doing some other stuff that do not need to wait on the time intensive stuff to complete. However, if you need to work with the result of the time intensiv...
See below for a simple example of how to use a BackgroundWorker object to perform time-intensive operations in a background thread. You need to: Define a worker method that does the time-intensive work and call it from an event handler for the DoWork event of a BackgroundWorker. Start the execu...
See below for a simple example of how to use a Task to do some time intensive stuff in a background process. All you need to do is wrap your time intensive method in a Task.Run() call. public void ProcessDataAsync() { // Start the time intensive method Task<int> t = Task.Run(() =&...
See below for a simple example of how to use a Thread to do some time intensive stuff in a background process. public async void ProcessDataAsync() { // Start the time intensive method Thread t = new Thread(TimeintensiveMethod); // Control returns here before TimeintensiveMethod r...
In certain cases (e.g. logging) it might be useful to run task and do not await for the result. The following extension allows to run task and continue execution of the rest code: public static class TaskExtensions { public static async void RunAndForget( this Task task, Action<E...

Page 1 of 1