Threading-2 in C#

Posted on

Threading in C#

A thread is characterized as the execution way of a project. Every string characterizes a one of a kind stream of control. On the off chance that your application includes muddled and tedious operations, then it is regularly useful to set diverse execution ways or threads, with every string performing a specific occupation. Threads are lightweight procedures. One basic illustration of utilization of string is execution of simultaneous programming by present day working frameworks. Utilization of threads spares wastage of CPU cycle and builds proficiency of an application. So far we composed the projects where a solitary string keeps running as a solitary procedure which is the running occasion of the application. Be that as it may, along these lines the application can perform one employment at once. To make it execute more than one undertaking at once, it could be isolated into littler threads.

Thread Life Cycle

The thread state goes through various states while its life cycle:

1. New: During this stage, a new thread has been created.

2. Wait: This is a stage where number of processes gets together, and waits for CPU to get executed.

3. Running: This is a stage where one from these processes gets executed.

4. Interrupt: Whenever there occur an interrupt the process will wait in a waiting queue.

5. Terminate: This stage is the last stage in thread life cycle gets ends and process gets terminate.

The Main Thread

In C#, the System.Threading.Thread class is utilized for working with strings. It permits making and getting to individual strings in a multithreaded application. The primary string to be executed in a procedure is known as the fundamental string. At the point when a C# program begins execution, the fundamental string is naturally made. The strings made utilizing the Thread class are known as the tyke strings of the principle string. You can get to a string utilizing the CurrentThread property of the Thread class.

using System;

using System.Threading;

namespace MultithreadingApplication

{

class MainThreadProgram

{

static void Main(string[] args)

{

Thread th = Thread.CurrentThread;

th.Name = “MainThread”;

Console.WriteLine(“This is {0}”, th.Name);

Console.ReadKey();

}

}

}

With Visual Basic and C#, you can compose applications that perform different undertakings in the meantime. Errands with the capability of holding up different undertakings can execute on independent strings, a procedure known as multithreading or free threading.

Applications that utilization multithreading are more receptive to client info in light of the fact that the client interface stays dynamic as processor-serious errands execute on particular strings. Multithreading is likewise valuable when you make adaptable applications, on the grounds that you can include strings as the workload increments.

The most dependable approach to make a multithreaded application is to utilize the BackgroundWorker part. This class deals with a different string devoted to handling the strategy that you indicate.

For a case, see Walkthrough:

Multithreading with the BackgroundWorker Component (C# and Visual Basic). To begin an operation out of sight, make a BackgroundWorker and listen for occasions that report the advancement of your operation and signal when your operation is done. You can make the BackgroundWorker question automatically, or you can drag it onto a structure from the Components tab of the Toolbox. On the off chance that you make the BackgroundWorker in the Forms Designer, it shows up in the Component Tray, and its properties are shown in the Properties window.

To set up for a foundation operation, include an occasion handler for the DoWork occasion. Call your tedious operation in this occasion handler. To begin the operation, call RunWorkerAsync. To get notices of advancement redesigns,handle the ProgressChanged occasion. To get a warning when the operation is finished, handle the RunWorkerCompleted occasion. The strategies that handle the ProgressChanged and RunWorkerCompleted occasions can get to the application’s client interface, on the grounds that those occasions are raised on the string that called the RunWorkerAsync technique. In any case, the DoWork occasion handler can’t work with any client interface objects in light of the fact that it keeps running on the foundation string.

Creating and Using Threads

On the off chance that you require more control over the conduct of your application’s strings, you can deal with the strings yourself. Then again, understand that composition right multithreaded applications can be troublesome: Your application might quit reacting or encounter transient blunders created by race conditions. For more data, see Thread-Safe Components.

The greater parts of these routines are simple, yet the idea of safe focuses might be different to you. Safe focuses are areas in code where it is ok for the regular dialect runtime to perform programmed trash accumulation, the procedure of discharging unused variables and recovering memory. When you call the Abort or Suspend technique for a string, the basic dialect runtime dissects the code and decides the area of a proper area for the string to quit running.

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s