• 大小: 27KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-02
  • 语言: C#
  • 标签: c#  

资源简介

C#多线程编程实例 线程与窗体交互源码.rar

资源截图

代码片段和文件信息

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 Thread_example
{
    public partial class Form1 : Form
    {
        //声明线程数组
        Thread[] workThreads = new Thread[10];

        public Form1()
        {
            InitializeComponent();
        }

        //此委托允许异步的调用为Listbox添加Item
        delegate void AddItemCallback(string text);

        //这种方法演示如何在线程安全的模式下调用Windows窗体上的控件。
        private void AddItem(string text)
        {
            if (this.listBox1.InvokeRequired)
            {
                AddItemCallback d = new AddItemCallback(AddItem);
                this.Invoke(d new object[] { text });
            }
            else
            {
                this.listBox1.Items.Add(text);
            }
        }

        //数据采集方法
        public void DataGet()
        {
            while (true)
            {
                AddItem(“ok“);
                Thread.Sleep(1000);
            }
            //如果此方法退出了,那么这个线程也就退出了
        }

        /// 
        /// 启动线程
        /// 

        /// 
        /// 
        private void button1_Click(object sender EventArgs e)
        {
            //循环创建并启动线程执行
            for (int i = 0; i < workThreads.Length; i++)
            {
                if (workThreads[i] == null)
                {
                    //如果线程不存在,则创建
                    workThreads[i] = new Thread(new ThreadStart(DataGet));
                    workThreads[i].Name = i.ToString();
                    workThreads[i].Start();
                }
                else
                {
                    //已经存在,如果没有运行,则启动
                    if (workThreads[i].ThreadState == ThreadState.Aborted || workThreads[i].ThreadState == ThreadState.Stopped)
                    {
                        workThreads[i] = new Thread(new ThreadStart(DataGet));
                        workThreads[i].Name = i.ToString();
                        workThreads[i].Start();
                    }else{
                        workThreads[i].Start();
                    }
                }
            }
        }

        /// 
        /// 停止线程
        /// 

        /// 
        /// 
        private void button2_Click(object sender EventArgs e)
        {
            //循环停止线程执行
            for (int i = 0; i < workThreads.Length; i++)
            {
                //如果线程存在,并且状态不是停止也不是终止的,则终止该线程
                if (workThreads[i] != null && workThreads[i].ThreadState != ThreadState.Stopped && workThreads[i].ThreadState != ThreadState.Aborted)
                {
                    workThreads[i].Abort();
                }
            }
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        884  2014-07-10 13:28  Thread_example.sln

    ..A..H.      9728  2014-07-10 13:28  Thread_example.suo

     文件      11608  2014-07-10 14:27  Thread_example\bin\Debug\Thread_example.vshost.exe

     文件        490  2010-03-17 22:39  Thread_example\bin\Debug\Thread_example.vshost.exe.manifest

     文件       3316  2014-07-10 14:26  Thread_example\Form1.cs

     文件       3295  2014-07-10 14:02  Thread_example\Form1.Designer.cs

     文件       5817  2014-07-10 14:02  Thread_example\Form1.resx

     文件       5319  2014-07-10 14:26  Thread_example\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        475  2014-07-10 13:28  Thread_example\Program.cs

     文件       1378  2014-07-10 13:28  Thread_example\Properties\AssemblyInfo.cs

     文件       2879  2014-07-10 13:28  Thread_example\Properties\Resources.Designer.cs

     文件       5612  2014-07-10 13:28  Thread_example\Properties\Resources.resx

     文件       1100  2014-07-10 13:28  Thread_example\Properties\Settings.Designer.cs

     文件        249  2014-07-10 13:28  Thread_example\Properties\Settings.settings

     文件       3435  2014-07-10 13:31  Thread_example\Thread_example.csproj

     目录          0  2014-07-10 13:28  Thread_example\obj\x86\Debug\TempPE

     目录          0  2014-07-10 14:27  Thread_example\obj\x86\Debug

     目录          0  2014-07-10 13:31  Thread_example\bin\Debug

     目录          0  2014-07-10 13:28  Thread_example\obj\x86

     目录          0  2014-07-10 13:28  Thread_example\bin

     目录          0  2014-07-10 13:28  Thread_example\obj

     目录          0  2014-07-10 13:28  Thread_example\Properties

     目录          0  2014-07-10 13:29  Thread_example

     文件      10050  2019-07-20 20:24  新建 XLSX 工作表.xlsx

----------- ---------  ---------- -----  ----

                65635                    24


评论

共有 条评论