国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#如何進行多線程編程

2024-07-21 02:26:38
字體:
來源:轉載
供稿:網友

初次接觸c#時做的一個小例子,先貼到這吧。由于多線程編程非常復雜,這個小例子只能算是一個入門線的知識點吧

首先建一個應用程序項目,命名為threadexample,在窗體上放一個文本框(textbox1) ,一個標簽(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(14, 38);
            this.btnstart.name = "btnstart";
            this.btnstart.size = new system.drawing.size(75, 23);
            this.btnstart.tabindex = 0;
            this.btnstart.text = "啟動";
            this.btnstart.click += new system.eventhandler(this.btnstart_click);
            //
            // btnstop
            //
            this.btnstop.location = new system.drawing.point(14, 68);
            this.btnstop.name = "btnstop";
            this.btnstop.size = new system.drawing.size(75, 23);
            this.btnstop.tabindex = 1;
            this.btnstop.text = "停止";
            this.btnstop.click += new system.eventhandler(this.btnstop_click);
            //
            // button1
            //
            this.button1.location = new system.drawing.point(14, 97);
            this.button1.name = "button1";
            this.button1.size = new system.drawing.size(75, 23);
            this.button1.tabindex = 3;
            this.button1.text = "關閉";
            this.button1.click += new system.eventhandler(this.button1_click);
            //
            // textbox1
            //
            this.textbox1.location = new system.drawing.point(14, 11);
            this.textbox1.name = "textbox1";
            this.textbox1.size = new system.drawing.size(75, 21);
            this.textbox1.tabindex = 4;
            this.textbox1.text = "200";
            //
            // lblresult
            //
            this.lblresult.autosize = true;
            this.lblresult.location = new system.drawing.point(12, 139);
            this.lblresult.name = "lblresult";
            this.lblresult.size = new system.drawing.size(23, 12);
            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(104, 164);
            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
            ...{
                //在對控件的調用方法進行調用時,或需要一個簡單委托又不想自己定義時可以使用該委托。
                methodinvoker mi = new methodinvoker(this.adddata);
                while (true)
                ...{
                    //在創建控件的基礎句柄所在線程上異步執行指定的委托
                    this.begininvoke(mi);
                    thread.sleep(50);
                }
            }
            catch (threadinterruptedexception)
            ...{
                //針對具體問題定制異常拋出顯示
            }
            finally
            ...{
                //做一些處理
            }
        }

        //啟動線程
        public void startthread()
        ...{
            stopthread();
            timerthread = new thread(new threadstart(updatathread));
            //獲取或設置一個值,該值指示某個線程是否為后臺線程。
            timerthread.isbackground = true;
            timerthread.start();
        }

        //停止線程
        public void stopthread()
        ...{
            if (timerthread != null)
            ...{
                //中斷線程
                timerthread.interrupt();
                timerthread = null;
            }
        }

        //啟動線程,顯示結果
        private void btnstart_click(object sender, eventargs e)
        ...{
            //調用線程啟動函數
            count = int.parse(textbox1.text);
            this.startthread();
        }

        //停止線程
        private void btnstop_click(object sender, eventargs e)
        ...{
            //調用線程停止函數
            this.stopthread();
        }      
    }
}
 編譯后,運行,在文本框中輸入200,點擊開始按鈕,標簽為動態增長,點擊停止可以暫停程序的執行。

 

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和政县| 景德镇市| 井研县| 襄城县| 灵寿县| 罗田县| 康马县| 灯塔市| 邢台县| 平江县| 牟定县| 玛曲县| 同江市| 板桥市| 新竹县| 镇远县| 静宁县| 简阳市| 岳阳市| 鄂尔多斯市| 延川县| 蒙阴县| 荔浦县| 晋中市| 宣城市| 黑水县| 井陉县| 无为县| 通城县| 宣恩县| 林甸县| 凤台县| 北川| 莎车县| 西昌市| 海兴县| 西丰县| 德惠市| 遵化市| 新闻| 石楼县|