时间:2015-1-13来源:不详作者:佚名
收藏    我要投稿
理论:       在学习多线程之前,首先要了解一下什么是进程?   进程:(关键字Process)进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动。它是操作系统动态执行的基本单元,在传统的操作系统中,进程既是基本的分配单元,           也是基本的执行单元。简单来说,就是计算机开启的一个正在运行的软件,是一段程序的执行过程。操作系统上包含N个进程。一个进程包含多个线程.   线程:(关键字Thread)线程是程序中一个单一的顺序控制流程,在一个进程里面开辟多个功能来同时执行多个任务。每一个程序都至少有一个线程,若程序只有一个线程,那就是程序本身。   前台线程:UI界面使用的是系统给我们默认的前台线程        前台线程终止后,后台线程不会终止。   后台线程:指的是我们自定义的线程对象                           后台线程终止后,前台线程将会结束。   多线程:   在单个程序中同时运行多个线程完成不同的工作成为多线程。   GDI:   图形设备接口(GDI:Graphics Device Interface)是Windows的子系统,它负责在视讯显示器和打印机上显示图形。     实操:   进程(在控制台下)   <strong><span style="font-size: 18pt;">class Program     {         static void Main(string[] args)         {               Process[] proce = Process.GetProcesses();  //获取操作系统上的所有进程               foreach (var item in proce)             {                  item.Kill();                        //停止关联的进程(慎用)                   Console.WriteLine(item);               }               Process.Start("devenv");          //打开一个进程               Process p1 = new Process();      //实例化一个进程               ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\system32\calc.exe"); //打开计算器进程的路径               p1.StartInfo = info;                      p1.Start();                 //打开           }     } } </span></strong>      线程:在Form窗体下   设计界面       主要代码:     <strong><span style="font-size: 18pt;">public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }                 bool b = false;         Thread th = null;   //引入命名空间           private void button1_Click(object sender, EventArgs e)         {             Control.CheckForIllegalCrossThreadCalls = false;  //是否跨线程                         if (!b)               {                 this.button1.Text = "终止";                                   b = true;                   th = new Thread(Bgian);                   th.Start();    //开始                              }               else             {                    this.button1.Text = "抽奖";                    b = false;                    th.Abort();    //终止线程             }               }                   public void Bgian()           { //让三个label控件循环获取1--10的随机数             Random r = new Random();             while (true)          {                 label1.Text = r.Next(1,10). ToString();                  label2.Text = r.Next(1, 10).ToString();            label3.Text = r.Next(1, 10).ToString();            }                         }     } </span></strong>     运行结果   单击”抽奖“按钮后出现的界面           单击”终止“按钮出现的界面:           GDI画图(直线)     <strong><span style="font-size: 18pt;">public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }           private void button1_Click(object sender, EventArgs e)         {             Graphics g = this.CreateGraphics(); //引进命名空间using System.Drawing;               Pen p = new Pen(Brushes.Red);      //Pen定义用于绘制直线和曲线的对象   Brushes所有标准颜色的画笔               g.DrawLine(p,new Point(100,100),new Point (300,100)); //只能画一条线                        Point[] points = { new Point(150, 200), new Point(250, 200), new Point(100, 300), new Point(300, 300) };//定义个Point数组来画多条线             g.DrawLines(p, points);                   } </span></strong>         简单提一下序列化:序列化就是将类以二进制的形式保存在硬盘上。关键字是:BinaryFormater。定义一个序列化是: BinaryFormatter binary = new BinaryFormatter();

转载请注明原文网址:http://www.helimiaopu.com/bbqb/bbqb/34.html
------分隔线----------------------------