多线程【C#】

【Thread】

Thread thread = new Thread(ThreadStart start);

。。。。。

thread.IsBackground = true;
thread.Start();

public delegate void ThreadStart();

所以,参数的类型是方法。

(

  () =>
         {
            this.txtV.Invoke(new Action<string>(data =>
            {
                 this.lblV.Text = data;//将读取的控件值,在其他控件中显示出来
            }), this.txtV.Text);
          }

);

 thread.IsBackground = true;
 thread.Start();

public object Invoke(Delegate method, params object[] args)//【方法,参数】

子控件都继承 .Invoke  

this.dgv1.Invoke(new Action<DataTable>(t => { this.dgv1.DataSource = t; }), dt1);
        //访问数据库
        private void btnExecute1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(() =>
            {
                string classCount = DBUtility.SQLHelper.GetSingleResult("select count(*) from Products").ToString();
                this.lblResult1.Invoke(new Action<string>(count => { this.lblResult1.Text = count; }), classCount);
            });
            thread.IsBackground = true;
            thread.Start();
        }
      
        //跨线程访问数据库,并在dgv中展示数据
        private void btnGetData_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(() =>
            {
                DataSet ds = DBUtility.SQLHelper.GetDataSet("select * from ProductInventory;select ProductId,ProductName,Unit from Products");
                DataTable dt1 = ds.Tables[0];
                DataTable dt2 = ds.Tables[1];

                this.dgv1.Invoke(new Action<DataTable>(t => { this.dgv1.DataSource = t; }), dt1);
                this.dgv2.Invoke(new Action<DataTable>(t => { this.dgv2.DataSource = t; }), dt2);
            });
            thread.IsBackground = true;
            thread.Start();
        }
    }

【启动】 .Start();

thread = new Thread(() =>
 {
     while (true)
     {
         try
         {
            Thread.Sleep(500);
             lblInfo.Invoke(new Action(() =>
             {
                 lblInfo.Text += counter++ + ",";
             }));
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message + "  异常位置:" + counter++);
         }
     }
 });
thread.Start();

【暂停】.Suspend();

//暂停
private void btnSuspend_Click(object sender, EventArgs e)
{
    if (thread.ThreadState == ThreadState.Running ||
        thread.ThreadState == ThreadState.WaitSleepJoin)
    {
        thread.Suspend();
    }
}

【继续】.Resume();

        //继续
        private void btnResume_Click(object sender,

相关推荐

  1. C++ 线

    2024-05-26 04:16:45       27 阅读
  2. C++ 线

    2024-05-26 04:16:45       19 阅读
  3. C#线

    2024-05-26 04:16:45       12 阅读
  4. C++ 线

    2024-05-26 04:16:45       12 阅读
  5. 线C#】

    2024-05-26 04:16:45       9 阅读
  6. C++ 线 atomic

    2024-05-26 04:16:45       46 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-26 04:16:45       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-26 04:16:45       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-26 04:16:45       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-26 04:16:45       20 阅读

热门阅读

  1. 网络模型-单臂路由配置相关命令

    2024-05-26 04:16:45       10 阅读
  2. 容器化部署

    2024-05-26 04:16:45       9 阅读
  3. pgsql 多个模式相同的表获取主键

    2024-05-26 04:16:45       11 阅读
  4. 计算机笔记14(续20个)

    2024-05-26 04:16:45       14 阅读
  5. 计算机笔记13(续20个)

    2024-05-26 04:16:45       13 阅读
  6. Web API 实现方式主流平台

    2024-05-26 04:16:45       8 阅读
  7. Kafka SSL认证

    2024-05-26 04:16:45       9 阅读