• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#多线程操作实例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.多线程操作 一旦打开线程就必须记得关闭

1.第一部分

这是个数字叠加小功能

       private void CountTo(int countTo, CancellationToken ct)
        {
            int sum = 0;
            for (; countTo > 0; countTo--)
            {
                if (ct.IsCancellationRequested)
                {
                    break;
                }
                sum += countTo;
                //Invoke方法用于获得创建lbl_Status的线程所在的上下文
                this.Invoke(new Action(() => textBox2.Text = sum.ToString()));
                Thread.Sleep(200);
            }
        }
   private CancellationTokenSource _cts;
        private void button1_Click(object sender, EventArgs e)
        {
            _cts = new CancellationTokenSource();
            ThreadPool.QueueUserWorkItem(state => CountTo(int.Parse(textBox1.Text), _cts.Token));
        }

代码中Textbox1输入叠加数量的范围 ,textbox2 为显示Textbox

取消累加

   private void button2_Click(object sender, EventArgs e)
        {
            if (_cts != null)
                _cts.Cancel();
        }

2.第二部分

这是个listbox 点击添加1.2.3.4.5。。。。。直到您点击停止

  Thread[] workThreads = new Thread[1];  
 private void button3_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();
                    }
                }
            }  
        }
   public void DataGet()
          {
            while (true)
            {
                if (listBox1.Items.Count > 0)
                {


                    int text = int.Parse(listBox1.Items[listBox1.Items.Count - 1].ToString()) + 1;
                    AddItem(text.ToString());
                    Thread.Sleep(1000);
                }
                   
                 
            }
            //如果此方法退出了,那么这个线程也就退出了  
           }  
 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);
            }
        }

 

关闭线程

   private void button4_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            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();
                }
            }
            this.listBox1.Items.Add("1");
        }

3.第三部分是模仿着自己写的

      //调取主板的声音
        [DllImport("kernel32.dll", EntryPoint = "Beep")]
        public static extern int Beep(int dwFreq, int dwDuration);
  public void PLaySound()
        {
            for (int a = 1; a < 10; a++)
            {
                Beep(800, 1000);
                Beep(500, 1000);
            }
            //thread.Join();
        }

 

 private void button5_Click(object sender, EventArgs e)
        {
                 thread = new Thread(new ThreadStart(PLaySound));
                 thread.Start(); 
        }

关闭进程

   private void button6_Click(object sender, EventArgs e)
        {
            thread.Abort();
        }

进程操作有很多种,仅供自己在编程时使用

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
oledbaspc#连接excel的字符串发布时间:2022-07-10
下一篇:
复习前面一个月的学习C#感觉道路好艰难啊发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap