Peer-to-peer Technology in C # to achieve point-to-point chat

Source: Internet
Author: User
Tags count join sleep thread thread class
Peer-to-peer previously in the use of VB to achieve multithreading, found that there is a certain degree of difficulty. Although there are also such methods, but not satisfactory, but in C #, to write multithreaded applications is quite simple. This article will give a brief introduction, to play a role in the study!
. NET defines the function of multithreading in the System.Threading namespace. Therefore, to use multiple threads, you must first declare a reference to this namespace (using System.Threading;).
Even if you do not have the experience of writing multithreaded applications, you may have heard the words "Start thread" "Kill thread", in addition to these two, there are also multithreading aspects such as "pause thread" "Priority" "Suspend thread" "Recovery thread" and so on. Below will be an explanation of one.
A. Start a thread
As the name suggests, "Start a thread" is to create a new and start a thread meaning, the following code can be implemented:
Thread thread1 = new Thread (new ThreadStart (Count));
The Count is the function that will be executed by the new thread.
B. Killing threads
"Kill a Thread" is to destroy the first thread, in order not to waste effort, before killing one of the threads, it is best to determine whether it is still alive (through the IsAlive property), and then can call the Abort method to kill the thread.
C. Pausing a thread
It means to keep a running thread dormant for a period of time. such as thread. Sleep (1000); is to keep the thread dormant for 1 seconds.
D. Priority
There's no need to explain this. The thread class has a ThreadPriority property that is used to set the priority, but does not guarantee that the operating system will accept the priority. The priority of a thread can be divided into 5 kinds: Normal, AboveNormal, BelowNormal, highest, Lowest. The specific implementation examples are as follows:
Thread. Priority = Threadpriority.highest;
E. Suspending Threads
The suspend method of the thread class is used to suspend the thread, knowing that the resume is invoked before the thread can continue. If the thread is already suspended, it will not work.
if (thread. ThreadState = threadstate.running)
{
Thread. Suspend ();
}
F. Recovery threads
Used to recover a thread that has been suspended so that it continues to execute, and it will not work if the thread is not suspended.
if (thread. ThreadState = threadstate.suspended)
{
Thread. Resume ();
}
An example is listed below to illustrate the simple threading functionality. This example comes from the Help document.
Using System;
Using System.Threading;

Simple threading Scenario:start A static method running
On a second thread.
public class Threadexample {
The ThreadProc method was called when the thread starts.
It Loops ten times, writing to the console and yielding
The rest of its time slice each time, and then ends.
public static void ThreadProc () {
for (int i = 0; i < i++) {
Console.WriteLine ("ThreadProc: {0}", i);
Yield the rest of the time slice.
Thread.Sleep (0);
}
}

public static void Main () {
Console.WriteLine ("Main Thread:start a second thread.");
The constructor for the Thread class requires a ThreadStart
Delegate that represents the "method" to is executed on the
Thread. C # simplifies the creation of this delegate.
Thread t = new Thread (new ThreadStart (ThreadProc));
Start ThreadProc. On a uniprocessor, the thread does not get
Any processor time until the main thread yields. Uncomment
The thread.sleep that follows T.start () to the difference.
T.start ();
Thread.Sleep (0);

for (int i = 0; i < 4; i++) {
Console.WriteLine ("Main thread:do some work.");
Thread.Sleep (0);
}

Console.WriteLine ("Main Thread:call Join (), to wait until ThreadProc ends.");
T.join ();
Console.WriteLine ("Main Thread:ThreadProc.Join has returned. Press Enter to end program. ");
Console.ReadLine ();
}
}

The output generated by this code is similar to the following:

Main Thread:start a second thread.
Main Thread:do some work.
threadproc:0
Main Thread:do some work.
Threadproc:1
Main Thread:do some work.
Threadproc:2
Main Thread:do some work.
Threadproc:3
Main Thread:call Join (), to the wait until ThreadProc ends.
Threadproc:4
Threadproc:5
Threadproc:6
Threadproc:7
Threadproc:8
Threadproc:9
Main Thread:ThreadProc.Join has returned. Press Enter to end program.






[1]



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.