<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      C#如何進行多線程編程

      由于多線程編程非常復(fù)雜,這個小例子只能算是一個入門線的知識點吧

      首先建一個應(yīng)用程序項目,命名為ThreadExample,在窗體上放一個文本框(textBox1) ,一個標(biāo)簽(lblResult),再放兩個按鈕,分別命名為btnStart、btnStop。

      窗體代碼:

      namespace ThreadExample
      {
          partial 
      class ThreadExample
          {
              
      /**//// <summary>
              
      /// Required designer variable.
              
      /// </summary>
              private System.ComponentModel.IContainer components = null;

              
      /**//// <summary>
              
      /// Clean up any resources being used.
              
      /// </summary>
              
      /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
              protected override void Dispose(bool disposing)
              {
                  
      if (disposing && (components != null))
                  {
                      components.Dispose();
                  }
                  
      base.Dispose(disposing);
              }

              Windows Form Designer generated code
      #region Windows Form Designer generated code

              
      /**//// <summary>
              
      /// Required method for Designer support - do not modify
              
      /// the contents of this method with the code editor.
              
      /// </summary>
              private void InitializeComponent()
              {
                  
      this.btnStart = new System.Windows.Forms.Button();
                  
      this.btnStop = new System.Windows.Forms.Button();
                  
      this.button1 = new System.Windows.Forms.Button();
                  
      this.textBox1 = new System.Windows.Forms.TextBox();
                  
      this.lblResult = new System.Windows.Forms.Label();
                  
      this.SuspendLayout();
                  
      // 
                  
      // btnStart
                  
      // 
                  this.btnStart.Location = new System.Drawing.Point(1438);
                  
      this.btnStart.Name = "btnStart";
                  
      this.btnStart.Size = new System.Drawing.Size(7523);
                  
      this.btnStart.TabIndex = 0;
                  
      this.btnStart.Text = "啟動";
                  
      this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
                  
      // 
                  
      // btnStop
                  
      // 
                  this.btnStop.Location = new System.Drawing.Point(1468);
                  
      this.btnStop.Name = "btnStop";
                  
      this.btnStop.Size = new System.Drawing.Size(7523);
                  
      this.btnStop.TabIndex = 1;
                  
      this.btnStop.Text = "停止";
                  
      this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
                  
      // 
                  
      // button1
                  
      // 
                  this.button1.Location = new System.Drawing.Point(1497);
                  
      this.button1.Name = "button1";
                  
      this.button1.Size = new System.Drawing.Size(7523);
                  
      this.button1.TabIndex = 3;
                  
      this.button1.Text = "關(guān)閉";
                  
      this.button1.Click += new System.EventHandler(this.button1_Click);
                  
      // 
                  
      // textBox1
                  
      // 
                  this.textBox1.Location = new System.Drawing.Point(1411);
                  
      this.textBox1.Name = "textBox1";
                  
      this.textBox1.Size = new System.Drawing.Size(7521);
                  
      this.textBox1.TabIndex = 4;
                  
      this.textBox1.Text = "200";
                  
      // 
                  
      // lblResult
                  
      // 
                  this.lblResult.AutoSize = true;
                  
      this.lblResult.Location = new System.Drawing.Point(12139);
                  
      this.lblResult.Name = "lblResult";
                  
      this.lblResult.Size = new System.Drawing.Size(2312);
                  
      this.lblResult.TabIndex = 5;
                  
      this.lblResult.Text = "0/0";
                  
      // 
                  
      // ThreadExample
                  
      // 
                  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                  
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                  
      this.ClientSize = new System.Drawing.Size(104164);
                  
      this.Controls.Add(this.lblResult);
                  
      this.Controls.Add(this.textBox1);
                  
      this.Controls.Add(this.button1);
                  
      this.Controls.Add(this.btnStop);
                  
      this.Controls.Add(this.btnStart);
                  
      this.Name = "ThreadExample";
                  
      this.Text = "Form1";
                  
      this.ResumeLayout(false);
                  
      this.PerformLayout();

              }

              
      #endregion

              
      private System.Windows.Forms.Button btnStart;
              
      private System.Windows.Forms.Button btnStop;
              
      private System.Windows.Forms.Button button1;
              
      private System.Windows.Forms.TextBox textBox1;
              
      private System.Windows.Forms.Label lblResult;
          }
      }

      程序代碼:
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
      using System.Threading;

      namespace ThreadExample
      {
          
      public partial class ThreadExample : Form
          {
              
      //聲明一個線程
              private Thread timerThread;
              
      //聲明一個變量,用來存儲label值
              int count, i = 0;

              
      public ThreadExample()
              {
                  InitializeComponent();
              }

              
      //把label的值加1;
              public void AddData()
              {
                  
      //顯示lable的值
                  if (i == count)
                      i 
      = 0;
                  
      this.lblResult.Text = i.ToString() + "/" + count.ToString();
                  i
      ++;
              }

              
      //更新線程
              public void UpdataThread()
              {
                  
      try
                  {
                      
      //在對控件的調(diào)用方法進行調(diào)用時,或需要一個簡單委托又不想自己定義時可以使用該委托。
                      MethodInvoker mi = new MethodInvoker(this.AddData);
                      
      while (true)
                      {
                          
      //在創(chuàng)建控件的基礎(chǔ)句柄所在線程上異步執(zhí)行指定的委托
                          this.BeginInvoke(mi);
                          Thread.Sleep(
      50);
                      }
                  }
                  
      catch (ThreadInterruptedException)
                  {
                      
      //針對具體問題定制異常拋出顯示
                  }
                  
      finally
                  {
                      
      //做一些處理
                  }
              }

              
      //啟動線程
              public void StartThread()
              {
                  StopThread();
                  timerThread 
      = new Thread(new ThreadStart(UpdataThread));
                  
      //獲取或設(shè)置一個值,該值指示某個線程是否為后臺線程。
                  timerThread.IsBackground = true;
                  timerThread.Start();
              }

              
      //停止線程
              public void StopThread()
              {
                  
      if (timerThread != null)
                  {
                      
      //中斷線程
                      timerThread.Interrupt();
                      timerThread 
      = null;
                  }
              }

              
      //啟動線程,顯示結(jié)果
              private void btnStart_Click(object sender, EventArgs e)
              {
                  
      //調(diào)用線程啟動函數(shù)
                  count = int.Parse(textBox1.Text);
                  
      this.StartThread();
              }

              
      //停止線程
              private void btnStop_Click(object sender, EventArgs e)
              {
                  
      //調(diào)用線程停止函數(shù)
                  this.StopThread();
              }       
          }
      }

      編譯后,運行,在文本框中輸入200,點擊開始按鈕,標(biāo)簽為動態(tài)增長,點擊停止可以暫停程序的執(zhí)行。
      posted @ 2009-06-23 10:39  .NET快速開發(fā)框架  閱讀(352)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 国产h视频在线观看| 成年女人午夜毛片免费视频| 亚洲AV成人片不卡无码| 麻豆国产va免费精品高清在线| 国产亚洲精品VA片在线播放| 麻豆一区二区中文字幕| 4虎四虎永久在线精品免费| 4399理论片午午伦夜理片| 精品无码一区二区三区电影| 国产99青青成人A在线| 亚洲一级特黄大片在线播放| www亚洲精品| 99久久精品一区二区国产| 麻豆精产国品一二三区区| 日韩午夜福利片段在线观看| 国产高清亚洲一区亚洲二区| 亚洲另类激情专区小说图片| 影视先锋av资源噜噜| 国产精品视频午夜福利| 南木林县| 国产精品国产三级国产试看| 一区二区三区在线色视频| 免费无码一区无码东京热| 国产中文三级全黄| 亚洲精品乱码久久久久久蜜桃不卡| 亚洲毛片多多影院| 无码国产偷倩在线播放| 免费无码一区二区三区蜜桃大| 中文字幕国产精品二区| 国产极品丝尤物在线观看| 欧美日韩一区二区综合| 精品九九人人做人人爱| 三级黄色片一区二区三区| 亚洲伊人久久综合成人| 午夜成人精品福利网站在线观看| 波多野结衣乳喷高潮视频| 性色av一区二区三区精品| 人人干人人噪人人摸| 日韩精品中文字幕无码一区| 亚洲综合av永久无码精品一区二区| 自偷自拍亚洲综合精品|