.NET Framework基础知识(二)(转载)

1、课外:为什么object数组可以赋值给object类型,int数组却不能赋值给int类型?
答:因为不管是什么什么数组都继承自Array,而Array又继承自object。
2、线程:是操作系统分配处理器时间的基本单元。
3、支持抢先多任务处理的操作系统可以创建多个进程中的多个线程同时执行的效果。
     实现:在需要处理器时间的线程之间分割可用处理器时间,并轮流为每个线程分配处理器时间片。

当前执行的线程在其时间片结束时被挂起,而另一个线程继续运行。当系统从一个线程切换到另一个线程时,

它将保存被抢先的线程的上下文,并重新加载线程队列中下一个线程的已保存线程上下文。
4、.NET 用Thread创建并控制线程。注意:引入using
例:class Program
    {
        static void Main(string[] args)
        {
            ThreadStart ts = new ThreadStart(F1);
            Thread th1 = new Thread(ts);
            ThreadStart ts1 = new ThreadStart(F2);
            Thread th2 = 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、线程的几个方法:Abort终止线程执行,Sleep使线程睡眠一段时间,方法的参数单位为毫秒,

Join可以使线程阻塞
以join为例实现线程阻塞:
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用来实现同步控制,在几个窗口同时在卖火车票或几个售货员同时在卖东西等可以用到lock。
以卖书为例:
class Program
    {
        static void Main(string[] args)
        {
            BookShop a = new BookShop();
            Thread t2 = new Thread(new ThreadStart(a.Sale));
            t2.Name = "李四";
            Thread t1 = new Thread(new ThreadStart(a.Sale));
            t1.Name = "张三";
            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 + "售出一本!");
                        if (Thread.CurrentThread.Name=="张三")
                        {
                            i++;
                        }
                        else
                        {
                            j++;
                        }
                    }
                    else
                    {
                        Console.WriteLine(Thread.CurrentThread.Name + "没有了!");
                        Console.WriteLine("李四售出{0}本,张三售出{1}本!",j,i);
                        if (i > j)
                        {
                        Console.ForegroundColor = ConsoleColor.Magenta;
                            Console.WriteLine("李四是笨笨!");
                            Console.ResetColor();
                        }
                        else
                        {
                            Console.WriteLine("张三是笨笨!");
                        }

                    }
                }
            }
        }
    }
7、TCP/IP:网络通讯协议,由网络层的IP协议和传输层的TCP协议组成的,是供已连接因特网的计算机进行通信的通信协议。
8、在.net中,我们用TCPClient和TCPListener类来实现点对点通讯,命名空间位于System.Net.Sockets
      TcpListener类提供一些简单方法,用于在阻止同步模式下侦听和接受传入连接请求。可以使用TcpClient和Socket来连接TcpListener。可使用IPEndPoint、本地IP地址及端口号或者仅使用端口号,来创建TcpListener。
      TcpClient类提供的一些简单方法,用于在同步阻止模式下通过网络来连接、发送、和接收流数据。
以一个简单控制台聊天机器人为例:
服务端:

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地址
            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("服务:" + 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("客户:" + DateTime.Now);
                Console.WriteLine("---------------------------");
            }

        }
    }
}
客户端:

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地址
            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("客户:" + 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("服务:" + DateTime.Now);
                Console.WriteLine("---------------------------");
            }

        }
    }
}
     这是一个一对一的聊天程序,大家可以结合以前学过的知识实现多对多的聊天程序。

本文出自 “大懒丫头” 博客,请务必保留此出处http://lanyatou.blog.51cto.com/3306130/624863

posted @ 2011-08-13 15:18  酷小孩  阅读(514)  评论(0编辑  收藏  举报