Free Web Development Online Tutorials, Learn to Code
Learn C# programming Language, Free C# Tutorial for Beginners, C# Development Guide

Learn Threading in C# Thread Example

What we learn Threads in C#?
We create thread, call LongTask function within thread, start the thread

In .Net Framework all thread-associated classes are under System.Threading namespace

Just think, when we need threading in development! When we have some long time taking task to be completed, and at the same time user should be able to perform other task without being interrupted, in that situation we use threading.

Now let's look at below threading code sample

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;     
namespace WTRConsoleApplication

Now we write a function for long time taking task that will Fetch 100000 records from database and send customized email to each user

class ThreadingSample
public static void MyLongTask1()
List< object> objCollection = null; 
        foreach (object o in objCollection)
        // here we can do any database task or sending email.
        // fetching 100000 records from database and send them customized email
        System.Console.WriteLine("Processing... my thread running");

Now we create a thread object and call that function

void ProcessNow()
    Thread MyThread = new Thread(new ThreadStart(MyLongTask1));

A collection of configured Threads ready to serve incoming asynchronous task is called "ThreadPool". ThreadPool manages a group of threads, ThreadPool technique improves the responsiveness of the application.

Now we learn how to use ThreadPool in realtime application

Learn Threading in C# Thread, Thread safety, Learn Join, Sleep and Abort methods in C# Threading, Learn thread pool


Threading in C#
in C# Tutorials
Other Online Tutorial

C# .net Interview Questions Answers