Basic. NET Framework knowledge (2)

Source: Internet
Author: User
Tags getstream what array

1. Out-of-class: Why can an object array be assigned the object type, but an int array cannot be assigned the int type?
A: No matter what Array, It inherits from Array, while Array inherits from object.
2. Thread: it is the basic unit for the operating system to allocate the processor time.
3. An operating system that supports preemptive multi-task processing can create multiple threads in multiple processes for simultaneous execution.
Implementation: divide the available processor time between threads that require processing time, and allocate processor time slices to each thread in turn.

The currently executed thread is suspended at the end of its time slice, while the other thread continues to run. When the system switches from one thread to another,

It will save the context of the preemptive thread and reload the saved thread context of the next thread in the thread queue.
4... NET uses Thread to create and control threads. Note: using
Example: class Program
{
Static void Main (string [] args)
{
ThreadStart ts = new ThreadStart (F1 );
Thread th1 = new Thread (ts );
ThreadStart ts1 = new ThreadStart (F2 );
Thread Th1 = new Thread (ts1 );
Th1.Start ();
Th2.Start ();
Th1.Abort ();
}
Static void F1 ()
{
While (true)
{
Console. WriteLine ("00000 ");
Thread. Sleep (200 );
}
}
Static void F2 ()
{
While (true)
{
Console. WriteLine ("11111 ");
Thread. Sleep (200 );
}
}
}
5. Several Methods of the thread: Abort terminates the thread execution and Sleep the thread to Sleep for a period of time. The parameter unit of the method is milliseconds,

Join can block threads
Taking join as an example to implement thread blocking:
Class Program
{
Static Thread ThrTest1, ThrTest2;
Static void Main (string [] args)
{
ThreadStart TS1 = new ThreadStart (F1 );
ThrTest1 = new Thread (TS1 );
ThreadStart TS2 = new ThreadStart (F2 );
ThrTest2 = new Thread (TS2 );
ThrTest1.Start ();
ThrTest2.Start ();
}
Public static void F1 ()
{
For (int I = 0; I <20; I ++)
{
If (I = 10)
{
ThrTest2.Join ();
}
Console. ForegroundColor = ConsoleColor. Blue;
Console. WriteLine (I. ToString () + "F1 ");
Thread. Sleep (500 );
}
}
Public static void F2 ()
{
For (int I = 0; I <20; I ++)
{
Console. ForegroundColor = ConsoleColor. Red;
Console. WriteLine (I. ToString () + "F2 ");
Thread. Sleep (500 );
}
}
}
6. lock is used to implement synchronous control. It can be used to sell train tickets or sales personnel at the same time in several windows.
Take selling books as an example:
Class Program
{
Static void Main (string [] args)
{
BookShop a = new BookShop ();
Thread t2 = new Thread (new ThreadStart (a. Sale ));
T2.Name = "Li Si ";
Thread t1 = new Thread (new ThreadStart (a. Sale ));
T1.Name = "James ";
T2.Start ();
T1.Start ();

}
}
Public class BookShop
{
Int num = 50;
Int I = 0;
Int j = 0;
Public void Sale ()
{
While (num> 0)
{
Lock (this)
{
If (num> 0)
{
Thread. Sleep (5 );
Num = num-1;
Console. WriteLine (Thread. CurrentThread. Name + "sold one! ");
If (Thread. CurrentThread. Name = "James ")
{
I ++;
}
Else
{
J ++;
}
}
Else
{
Console. WriteLine (Thread. CurrentThread. Name + "No! ");
Console. WriteLine ("Li Si sold {0} books, Zhang San sold {1} books! ", J, I );
If (I> j)
{
Console. ForegroundColor = ConsoleColor. Magenta;
Console. WriteLine ("Li Si is stupid! ");
Console. ResetColor ();
}
Else
{
Console. WriteLine ("James is stupid! ");
}

}
}
}
}
}
7. TCP/IP: network communication protocol, which consists of the IP protocol at the network layer and the TCP protocol at the transmission layer. It is a communication protocol for computers connected to the Internet.
8. In. net, we use TCPClient and TCPListener classes to implement point-to-point communication. The namespace is located in System. Net. Sockets.
The TcpListener class provides some simple methods for listening to and receiving incoming connection requests in the blocking synchronous mode. You can use TcpClient and Socket to connect TcpListener. You can use IPEndPoint, local IP address, and port number, or only the port number to create a TcpListener.
The TcpClient class provides some simple methods for connecting, sending, and receiving stream data through the network in synchronous blocking mode.
Take a simple console chatbot as an example:
Server:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Net. Sockets;
Using System. Net;
Using System. IO;
Using System. Threading;

Namespace Server
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("server ");
TcpListener server = new TcpListener (IPAddress. Parse ("***. ***"), 9999 );

// Ip address of the server
Server. Start ();
TcpClient clinet = server. AcceptTcpClient ();
NetworkStream netstream = clinet. GetStream ();
Thread th = new Thread (ReadText );
Th. Start (netstream );
StreamWriter swriter = new StreamWriter (netstream );
While (true)
{
Console. WriteLine ("---------------------------");
String con = Console. ReadLine ();
Swriter. WriteLine (con );
Swriter. Flush ();
Console. WriteLine ("service:" + DateTime. Now );
Console. WriteLine ("---------------------------");
}
}
Static void ReadText (object o)
{
NetworkStream netstream = o as NetworkStream;
StreamReader sr = new StreamReader (netstream );
While (true)
{
Console. WriteLine ("---------------------------");
String con = sr. ReadLine ();
Console. WriteLine (con );
Console. WriteLine ("Customer:" + DateTime. Now );
Console. WriteLine ("---------------------------");
}

}
}
}
Client:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Net;
Using System. Net. Sockets;
Using System. IO;
Using System. Threading;

Namespace Client
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("client ");
TcpClient client = new TcpClient ("***. ***", 9999); // ip address of the server
NetworkStream netstream = client. GetStream ();
StreamWriter swriter = new StreamWriter (netstream );
Thread th = new Thread (ReadText );
Th. Start (netstream );
While (true)
{
Console. WriteLine ("---------------------------");
String con = Console. ReadLine ();
Swriter. WriteLine (con );
Swriter. Flush ();
Console. WriteLine ("Customer:" + DateTime. Now );
Console. WriteLine ("---------------------------");
}

}
Static void ReadText (object o)
{
NetworkStream netstream = o as NetworkStream;
StreamReader sr = new StreamReader (netstream );
While (true)
{
String con = sr. ReadLine ();
Console. WriteLine ("---------------------------");
Console. WriteLine (con );
Console. WriteLine ("service:" + DateTime. Now );
Console. WriteLine ("---------------------------");
}

}
}
}
This is a one-to-one chat program. You can use the previous knowledge to implement many-to-many chat programs.

This article from the "Big lazy girl" blog, please be sure to keep this source http://lanyatou.blog.51cto.com/3306130/624863

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.