windows operating system works under the hood.Windows is a preemptive multitasking operating system. The system CPU can do only one thing at a time, but to give the illusion that multiple processes are running simultaneously the operating system splits the CPU time between the various running processes. Preemptive means that the operating system determines how long task will when executes.
.net there is System.Threading.Thread name space with use of this name space we can achive multithreading.
public class TestClass{ public static void MyTest(){ ... } }
ThreadStart start = new ThreadStart( TestClass.MyTest ) ; Thread thread1 = new Thread(start) ; thread1.Start() ;
C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads.
Pls refer the attached document for more detials.
Thanks.
If this post is useful then mark it as "Accepted Answer"
Multithreading: C# allows to run more than one thread parallel in an operating system. Which effectively uses the operating system to execute more threads concurrently.
For example, the operating system allows to work multitasking. The word document, notepad, calculator and etc.. can run concurrently. At the same time the multithreading ensures the multiple word document can be opened concurrently. It means it can work as separate thread.
NET has been designed from the start to support multi-threaded operation. There are two main ways of multi-threading which .NET encourages: starting your own threads with ThreadStart delegates, and using the ThreadPool class either directly (using ThreadPool.QueueUserWorkItem) or indirectly using asynchronous methods (such as Stream.BeginRead, or calling BeginInvoke on any delegate).
In general, you should create a new thread "manually" for long-running tasks, and use the thread pool only for brief jobs. The thread pool can only run so many jobs at once, and some framework classes use it internally, so you don't want to block it with a lot of tasks which need to block for other things. The examples in this article mostly use manual thread creation. On the other hand, for short-running tasks, particularly those created often, the thread pool is an excellent choice.
For example,
using System;
using System.Threading;
public class ThreadTest {
public class MyJob {
public int repetitions=1;
public void runme() {
for(int i=0;i
Thread.Sleep(1000);
Console.WriteLine(Thread.CurrentThread.Name);
}
}
}
public static void Main(string[] args) {
MyJob myJob = new MyJob();
myJob.repetitions=3;
Thread thread = new Thread(new ThreadStart(myJob.runme));
thread.Name = "first";
thread.Start();
MyJob myJob2 = new MyJob();
myJob2.repetitions=5;
Thread thread2 = new Thread(new ThreadStart(myJob2.runme));
thread2.Name = "second";
thread2.Start();
}
}
The thread first and second can execute concurrently. Both threads executes the same method and at the same time it can be locked.
Jignesh TrivediRank #16 · 50.3K reputationPosted Apr 5, 2012, 9:04 PM EDT
windows operating system works under the hood.Windows is a preemptive multitasking operating system. The system CPU can do only one thing at a time, but to give the illusion that multiple processes are running simultaneously the operating system splits the CPU time between the various running processes.
Preemptive means that the operating system determines how long task will when executes.
.net there is System.Threading.Thread name space with use of this name space we can achive multithreading.
public class TestClass{
public static void MyTest(){
...
}
}
ThreadStart start = new ThreadStart( TestClass.MyTest ) ;
Thread thread1 = new Thread(start) ;
thread1.Start() ;
hope this help.
MuthuMari MRank #5,738 · 288 reputationPosted Apr 5, 2012, 9:19 PM EDT
C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run
simultaneously with other threads.
Pls refer the attached document for more detials.
Thanks.
Kunal Naik163 reputationPosted Apr 5, 2012, 9:39 PM EDT
Refer below article from this site,
http://www.c-sharpcorner.com/UploadFile/mgold/MultithreadingIntro10062005000439AM/MultithreadingIntro.aspx
Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 5, 2012, 11:20 PM EDT
Please refer Our recommended articles
http://www.c-sharpcorner.com/uploadfile/rohatash/multithreading-in-C-Sharp/default.aspx
Thanks
SenthilkumarRank #387 · 6.5K reputationPosted Apr 6, 2012, 10:26 PM EDT
Multithreading:
C# allows to run more than one thread parallel in an operating system. Which effectively uses the operating system to execute more threads concurrently.
For example, the operating system allows to work multitasking. The word document, notepad, calculator and etc.. can run concurrently. At the same time the multithreading ensures the multiple word document can be opened concurrently. It means it can work as separate thread.
For example,
The thread first and second can execute concurrently. Both threads executes the same method and at the same time it can be locked.
You can go through this MSDN for know about threading, semephore, mutex, synchronization:
http://msdn.microsoft.com/en-us/library/system.threading.aspx
Lot of example program in
http://www.fincher.org/tips/Languages/csharpThreads.shtml
There are some other links about multithreading:
http://www.yoda.arachsys.com/csharp/threads/
http://www.c-sharpcorner.com/UploadFile/mgold/MultithreadingIntro10062005000439AM/MultithreadingIntro.aspx
http://www.codeproject.com/Articles/8694/Multithreading-Concepts-in-C
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts