qidao123.com ToB IT社区-企服评测·应用市场

标题: C# 窗体应用(.FET Framework) 线程操纵方法 [打印本页]

作者: 知者何南    时间: 2025-10-2 06:22
标题: C# 窗体应用(.FET Framework) 线程操纵方法
一、Thread线程利用方法

  1. Thread th1;
  2. th1 = new Thread(方法名);
  3. th1.IsBackground = true;
  4. th1.Start();
复制代码
  1. ///定义一个object接受参数的方法
  2. private void Test(object n){
  3.     string str1 = n as string;
  4. MessageBox.Show(str1);
  5. }
  6. // 调用方法
  7. Thread th2
  8. string str1 = “我是线程2”;
  9. th2 = new Thread(Test);
  10. th2.isBackground = true;
  11. th2.Start(str1);  // 这边在启动的时候传递参数
复制代码
  1. ThreadPool.QueueUserWorkItem(方法名);
复制代码
  1. Private void Form1_FormClosing(){
  2.    th1.Abort();
  3. }
复制代码
二、ThreadPool 线程利用方法
  1. ThreadPool.QueueUserWorkItem((str)=> {代码块}, “线程参数”);
复制代码
三、办理跨线程调用组件

  1. // 在 Form1() 方法中加入以下代码
  2. CheckForIllegalCrossThreadCalls = false;
复制代码
  1. 组件.BeginInvoke(new Action<变量类型>((变量)=>{代码块}), 形参);
复制代码
  1. textBox1.BeginInvoke(new Action<string>((str) => {
  2.     textBox1.Text += str;
  3. }), "111111\r\n");
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com ToB IT社区-企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.5