Author - StudySection Post Views - 145 views
Multithreading

Multithreading in .Net

A thread is a separate stream of instructions in a program and is analogous to a sequential program.
The true use of a thread is not a single sequential thread, but rather the use of several threads in a single application. Multithreading refers to multiple threads running at the same time and doing various activities.

process

Namespace System.Threading

System.Threading is the namespace that gives different sorts to help in the development of multithreaded applications.

process1

System.Threading.Thread class

In .net allows you to manage the execution of managed threads in your program using thread class also known as managed threads.

process2

Multithreading Implementation

using System;
using System.Threading;
namespace threading
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("******Current Threads Information******\n");
Thread t = Thread.CurrentThread;
t.Name = "Primary_Thread";
Console.WriteLine("Thread Name: {0}", t.Name);
Console.WriteLine("Thread Status: {0}", t.IsAlive);
}

Thread Creation
using System;
using System.Threading;
namespace threading
{
class Program
{
static void Main(string[] args)
{
Thread t = new Thread(myTest);
t.Start();
Console.WriteLine("Main thread is Running");
Console.ReadKey();
}
static void myTest()
{
Console.WriteLine("Running other Thread");
}
}
}

Background Thread
The application’s process continues to run as long as at least one foreground thread is active. If more than one foreground thread is operating when the Main() method is called, the application’s process remains active until all foreground threads have completed their tasks, at which point all background threads will be terminated.

using System;
using System.Threading;
namespace threading
{
class Program
{
static void Main(string[] args)
{
Thread t = new Thread(myFun);
t.Name = "Thread1";
t.IsBackground = false; //can define it as being either a foreground or background thread by setting the property IsBackground
t.Start();
Console.WriteLine("Main thread Running");
Console.ReadKey();
}
static void myFun()
{
Console.WriteLine("Thread {0} started", Thread.CurrentThread.Name);
Thread.Sleep(2000);
Console.WriteLine("Thread {0} completed", Thread.CurrentThread.Name);
}
}
}

The English language is the most widely used language as a medium of communication around the world. Having a certification for the English language can be an advantage. StudySection provides an English Certification Exam that tests English language proficiency in English grammar, reading, and writing.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri