我有如下代码:
private void SyncProcess()
{
while(m_SyncProcessStart)
{
BusComputer computer = GetNextSyncComputer();
if (computer != null)
{
computer.OnSyncProcessed += new BusComputer.SyncProcessedEventHandler(computer_OnSyncProcessed);
SyncFilesDelegate sdelegate = new SyncFilesDelegate(computer.SyncFiles);
sdelegate.BeginInvoke(null, null);
//ShowSyncInfoToUI("dd", toolStrip_CopyInfo.Text,"信息" , 100);
Thread.Sleep(1000);
}
}
}
void computer_OnSyncProcessed(BusComputer sender, SyncProcessedEventArgs e)
{
ShowSyncInfoToUI(e.Computer, toolStrip_CopyInfo.Text, "", e.Step);
}
SyncProcess()这个方法通过委托后用BeginInvoke执行,在执行过程中希望能更新UI界面,在OnSyncProcessed事件的响应方法中调用更新UI的方法。现在发现的问题是:在执行时SyncProcess()时窗体不能响应用户其他操作,必须执行完后才能响应。但如果把ShowSyncInfoToUI()方法放在SyncProcess()中,如上面注释的那一行一样,则执行时,窗体不会无响应。
现在的疑问是:当异步执行SyncProcess()时,里面注册的事件响应函数在执行时与SyncProcess()方法所在的线程不同?事件响应方法的执行仍然是在主线程?如何让事件响应方法也异步执行呢(已尝试过将computer_OnSyncProcessed方法体封成另一个委托并在这里异步执行,但还是不行)?请大家指教,不胜感激!
private void SyncProcess()
{
while(m_SyncProcessStart)
{
BusComputer computer = GetNextSyncComputer();
if (computer != null)
{
computer.OnSyncProcessed += new BusComputer.SyncProcessedEventHandler(computer_OnSyncProcessed);
SyncFilesDelegate sdelegate = new SyncFilesDelegate(computer.SyncFiles);
sdelegate.BeginInvoke(null, null);
//ShowSyncInfoToUI("dd", toolStrip_CopyInfo.Text,"信息" , 100);
Thread.Sleep(1000);
}
}
}
void computer_OnSyncProcessed(BusComputer sender, SyncProcessedEventArgs e)
{
ShowSyncInfoToUI(e.Computer, toolStrip_CopyInfo.Text, "", e.Step);
}
SyncProcess()这个方法通过委托后用BeginInvoke执行,在执行过程中希望能更新UI界面,在OnSyncProcessed事件的响应方法中调用更新UI的方法。现在发现的问题是:在执行时SyncProcess()时窗体不能响应用户其他操作,必须执行完后才能响应。但如果把ShowSyncInfoToUI()方法放在SyncProcess()中,如上面注释的那一行一样,则执行时,窗体不会无响应。
现在的疑问是:当异步执行SyncProcess()时,里面注册的事件响应函数在执行时与SyncProcess()方法所在的线程不同?事件响应方法的执行仍然是在主线程?如何让事件响应方法也异步执行呢(已尝试过将computer_OnSyncProcessed方法体封成另一个委托并在这里异步执行,但还是不行)?请大家指教,不胜感激!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货