Don't Believe Everything They'Ve Told You. Threads in C # Are Actually Pretty Easy.
Don't believe that others tell you all things. In fact, the thread in C # is very simple.
A thread is an encapsulation of the flow of control in a program you might be used to writing single-threaded programs -. That is, programs that only execute one path through their code "at a time" If you have more than one thread. , The Code path run "simultaneously".
The thread is the package of the control flow in the program. You may have become accustomed to writing a single-threaded program, that is, the program is executed in a road in their code. If you get more threads, the code run may be more "synchronized".
Why are some phrases above in quotes? In a typical process in which multiple threads exist, zero or more threads may actually be running at any one time. However on a machine that got n CPU's only one thread (actually) can run at any given Time on Each CPU, BECAUSE EACH THREAD IS A CODE PATH, EACH CPU CAN Only Run ONE CODE-AT A Time. The Appearance of Running Many More Than N "Simultaneously" is done by Sharing The cpus among threads.
In a typical process with multi-thread, zero or more threads are running at the same time. However, on a machine with N CPUs, a thread can only run on a CPU at a given time because each thread is a code segment, and each CPU can only run a piece of code at a time. It seems that it is true like n simultaneously is the effect of sharing CPU time films between threads.
In this example we will create another thread, we will try to implement a way to demonstrate the multithreaded way of working between the two threads we have, and at the end, we will sync the two threads (the main and the new one) for Letting The New Thread "Wait" for a message Before Continuing.
In this example, we will create another thread, we will use two threads to demonstrate multithreading mode of work, and finally, we implement two threads (main threads and new threads) synchronize, and must wait for the message before the new thread work.
To create a thread we need to add the System.Threading namespace. After that we need to understand that a thread has GOT to have a start point for its flow of control. The start point is a function, which should be in the same call Or in a DiffERENT ONE. We must introduce the system.threaming namespace before establishing the thread. Then I need to know is that threads have established a starting point for the control process. The starting point is a function that allows one to call or other.
Here You Can See a Function in The Same Class That Is Defined As The Start Point.
Here you can see the starting point function defined in the same class.
Using system;
Using system.threading;
Namespace ThreadingTester
{
Class threadClass
{
Public static void trmain ()
{
For (int x = 0; x <10; x )
{
Thread.sleep (1000);
Console.writeline (x);
}
}
Static void
Main
(String [] ARGS)
{
Thread thrd1 = new thread (New Threadstart (TRMAIN);
THRD1.START ();
For (int x = 0; x <10; x )
{
Thread.sleep (900);
Console.writeline ("main:" x);
}
}
}
}
Thread.Sleep (n) method puts the * this * thread into sleep for n milliseconds. You can see in this example, that in main we define a new thread, which its start point is the function trmain (), we then invoke the START () Method to Begin The Execution.
Thread.sleep (n) method The "THIS" thread is placed in the sleep state of the N milliseconds. You can take a look at this example, in the main function we define a new thread, where its starting point is the function TRMAIN (), and we then contain the start () method to start execution.
If you run this example, you will know that the context switch between the threads (the process of letting a thread run in the CPU and then switching to another thread) lets the threads run almost together, I have placed the main thread to sleep 100 Milliseconds Less Than New Thread in Order To See Which One Thread Runs "Faster".
If you are running this example, you will learn about the switch between the thread (let the CPU go to another thread from running a thread) to make the thread run almost simultaneously. In order to see which thread runs faster, I will set the main thread to the new thread. 100 milliseconds.
Now, A Thread Could Be Assigned with a name, Before Starting The Thread We Could: Now, first naming threads before starting the thread:
Thread thrd1 = new thread (New Threadstart (TRMAIN);
THRD1.NAME = "thread1";
THRD1.START ();
In The Thread Itself, We can take the name inteage by:
Thread Tr = Thread.currentthread;
Console.writeLine (Tr.Name);
After we made that, imagine that we do not want the new thread to run to the end immediately when we start it, say we want to start the new thread, let it run, in a certain point the new thread will pause and will Wait for a message from the main thread (or from another thread).
After completing the above program, I envision We don't want to run at the beginning, let it run immediately, that is, we open a new thread, let it run, on a specific time point, new thread pause and wait Message from the main thread (or other thread).
We can do this by defining:
We can define this: public static manualReveTevent MRE = New ManualReveTevent (false);
The ManualResetEvent is being created with false as start state, this class is being used to signal another thread, and to wait for one or more threads. Note that all threads should have access to that class in order to signal or listen on the same one .
When ManualReset is established, the FALSE is used as the initial state of Start, which is used to inform another thread and let it wait for one or more threads. Note that all other threads can access that class in order to notify or listen to the same thread.
The Waiting Thread SHOULD:
Waiting for the thread to write:
Mre.waitone ();
This Will Cause The Waiting Thread to Pause Indefinitely And Wait for the Class To Be Signaled.
This will cause the waiting thread to unlimited blocking and wait for the class to notify.
The signaling thread shop:
The thread of the signal should be like this:
MRE.SET ();
That Will Cause The Class To Be Signaled As True, and The Waiting. After Signaling An Event We Can Reset It To The Base State By:
Such a category will be notified that the value becomes True, waiting for the thread to stop waiting. After the notification event occurs, we can use the following statement to place the thread in the base state:
MRE.RESET ();
Now Lets Implement All That In a Single Application:
Let us now execute:
Using system;
Using system.threading;
Namespace ThreadingTester
{
Class threadClass
{
Public Static ManualReveTevent MRE = New ManualReveTevent (false);
Public static void trmain ()
{
Thread Tr = Thread.currentthread;
Console.writeline ("Thread: Waiting for An Event);
Mre.waitone ();
Console.writeline ("Thread: got an evenet";
For (int x = 0; x <10; x )
{
Thread.sleep (1000);
Console.writeline (Tr.Name ":" x);
}
}
Static void
Main
(String [] ARGS)
{
Thread thrd1 = new thread (New Threadstart (TRMAIN);
THRD1.NAME = "thread1";
THRD1.START ();
For (int x = 0; x <10; x )
{
Thread.sleep (900);
Console.writeline ("main:" x);
IF (5 == x) mre.set ();
}
While (THRD1.IISALIVE)
{
Thread.sleep (1000);
Console.WriteLine ("Main: waiting for thread to stop ...");
}
}
}
}