zoukankan      html  css  js  c++  java
  • C#一个线程操作另一个线程的控件的方法

    Using System.Threading;
    Thread thread1;
    Thread thread2;
    Delegate void AppendStringDelegate(string str);
    AppendStringDelegate appendStringDelegate;
    Public Form1()
    {
    InitializeComponent();
    appendStringDelegate=new AppendStringDelegate(AppendString);
    }
    Private void AppendString(string str)
    {
    richTextBox1.Text+=str;
    }
    Private void Method1()
    {
    While(true)
    {
    Thread.Sleep(100);
    richTexBox1.Invoke(appendStringDelegate,”a”);
    }
    }
    Private void Method2()
    {
    While(true)
    {
    while(true)
    {
    Thread.Sleep(100);
    richTextBox1.Invoke(appendStringDelegate,”b”);
    }
    }
    //启动线程
    Private void buttonStart_Click(object sender, EventArgs e)
    {
    richTextBox1.Text=””;
    thread1=new Thread(new ThreadStart(Method1));
    thread2=new Thread(new ThreadStart(Method2));
    thread1.Start();
    thread2.Start();
    }
    //终止线程
    Private void buttonStop_Click(object sender,EventArg e)
    {
    thread1.Abort();
    thread2.Join();
    thread2.Abort();
    thread2.Join();
    MessageBox.Show(“线程1,2终止成功”);
    }

  • 相关阅读:
    美化盒子和文本字体
    图片和多媒体
    学习node1_module对象
    学习vue5_组件
    学习vue4_input
    学习vue3
    学习vue2
    Ubuntu中U盘识别不了
    docker 建立新用户软件安装环境ubuntu
    计算机性能优化笔记
  • 原文地址:https://www.cnblogs.com/jordan2009/p/1628571.html
Copyright © 2011-2022 走看看