线程 和异步委托

using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Data;

namespace  异步委托和线程调用
{
 
/// <summary>
 
/// Form1 的摘要说明。
 
/// </summary>

 public class Form1 : System.Windows.Forms.Form
 
{
默认代码
  
  
//线程
  private void button1_Click(object sender, System.EventArgs e)
  
{
   System.Threading.Thread th 
= new System.Threading.Thread(
    
new System.Threading.ThreadStart(Fun2));   
   
this.processEvent += new myProcessEventHandle(Form1_processEvent);
   th.Start();
   button2.Enabled 
= false;
  }


  
//异步委托
  private void button2_Click(object sender, System.EventArgs e)
  
{
   System.Threading.WaitCallback wc 
= new System.Threading.WaitCallback(Fun);
   
object obj = this.button1;//这里没用。到时候你可以用来传递参数
   this.processEvent += new myProcessEventHandle(Form1_processEvent);
   System.Threading.ThreadPool.QueueUserWorkItem(wc,obj);   
   button1.Enabled 
= false;
  }


  
public delegate void myProcessEventHandle(int percent);//定义事件
  public event myProcessEventHandle processEvent = null;//事件声明

  

  
//线程无法带参数启动,和这个函数通讯直接用成员变量就可以了
  private void Fun2()
  
{
   
int i = 0;
   String str 
= this.Text;
   
while (i<1000)
   
{
    i
++;
    
//this.Text = i.ToString();
    if (this.processEvent != null)
    
{
     
this.processEvent(i);
    }

    System.Threading.Thread.Sleep(
8);
   }
   
   TelMe();
   
this.Text = str;
  }


  
//异步委托可以也必须要一个参数,当然不想用就传一个null就可以了
  private void Fun(object obj)
  
{
   
int i = 0;
   String str 
= this.Text;
   
while (i<1000)
   
{
    i
++;
    
//this.Text = i.ToString();
    if (this.processEvent != null)
    
{
     
this.processEvent(i);
    }

    System.Threading.Thread.Sleep(
8);
   }
   
   TelMe();
   
this.Text = str;
  }


  
//不是回调,但是同一个类里写的,就可以直接调用了。
  private void TelMe()
  
{
   
this.processEvent -= new myProcessEventHandle(Form1_processEvent);
   button1.Enabled 
= true;
   button2.Enabled 
= true;
   MessageBox.Show(
"操作完成!");
  }


  
//事件方式通知主线程进度
  private void Form1_processEvent(int percent)
  
{
   
this.progressBar1.Value = percent;
   
this.Text = ((double)percent/10).ToString() + "%";
  }


  
private void Form1_Closed(object sender, System.EventArgs e)
  
{
   System.Environment.Exit(
0);//用于退出没有运行结束的线程
  }

 }

}



// 以上代码在vs2003可以运行。以前写的例子。

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
异步委托线程调用控件是指在多线程的情况下,通过使用异步委托来实现对控件的跨线程调用。这种方法可以解决在主线程中访问控件时可能出现的界面卡死、无响应等问题。 在C#中,可以通过使用Invoke或BeginInvoke方法来实现异步委托线程调用控件。使用Invoke方法可以确保任务在主线程上执行,而BeginInvoke方法允许任务在后台线程上执行。具体的使用方法如下: 1. 在主线程中创建委托方法,该方法用于更新控件的属性或执行其他与控件相关的操作。 2. 当需要在后台线程中更新控件时,使用Invoke或BeginInvoke方法,将委托方法作为参数传递给它们。 3. 在委托方法中,通过控件的Invoke方法或BeginInvoke方法来更新控件。 请注意,为了避免线程冲突和不可预料的结果,强烈不推荐使用Control.CheckForIllegalCrossThreadCalls = false;这种方法。相反,应该采用合理的方法,即在主线程中负责UI界面的调度,将耗时的操作放在后台线程中进行,并使用Invoke委托来触发UI刷新。这样可以确保界面的稳定性和响应性,避免出现卡死、无响应或报错等问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C#跨线程调用控件](https://blog.csdn.net/tonghaojie/article/details/78767046)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [C# winform delegate委托以及异步调用委托(跨线程调用控件UI)使用总结](https://blog.csdn.net/gxlzhhx/article/details/128568462)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值