线程中的委托不能及时更新UI的有关问题
线程中的委托不能及时更新UI的问题
感觉执行到fileTransFinishedEvent.WaitOne(); 阻塞了。简要代码如下:
public class ProtocolFileSend
{
public delegate void ShowProcessHandle(int percent, string message);
private event ShowProcessHandle sftpSendProgressEvent;
public event ShowProcessHandle SftpSendProgressEvent
{
add
{
sftpSendProgressEvent += value;
}
remove
{
sftpSendProgressEvent -= value;
}
}
ManualResetEvent fileTransFinishedEvent = new ManualResetEvent(false);
public bool TransFile(string fileName)
{
fileTransFinishedEvent.Reset();
ThreadPool.QueueUserWorkItem(new WaitCallback(TransFileInner), fileName);
fileTransFinishedEvent.WaitOne();
}
private void TransFileInner(object oFileName)
{
try
{
bool sendRes = SendFile(fileName.ToString());//该函数耗时较长,里面会调用SftpSendProgressEvent汇报进度
}
catch(Exception ex)
{
}
fileTransFinishedEvent.Set();
}
}
public class MainFrm:Form
{
public void button1_click()
{
ProtocolFileSend proSFTP = new ProtocolFileSend(remoteIP, remotePort);
proSFTP.SftpSendProgressEvent += new ProtocolFileSend.ShowProcessHandle(proSFTP_SftpSendProgress);
bool res = proSFTP.TransFile(fileName);
}
void proSFTP_SftpSendProgress(int percent, string message)
{
UpdateProgressBar(percent, message);//写日志发现 该行确实执行了,但是是在 SendFile最后阶段才执行的。所在在等待过程中进度条没有更新,只是最后快结束的时候进度条刷新了一下。
}
}
请各位帮忙看下如何处理比较好,谢谢!
------解决方案--------------------
感觉执行到fileTransFinishedEvent.WaitOne(); 阻塞了。简要代码如下:
public class ProtocolFileSend
{
public delegate void ShowProcessHandle(int percent, string message);
private event ShowProcessHandle sftpSendProgressEvent;
public event ShowProcessHandle SftpSendProgressEvent
{
add
{
sftpSendProgressEvent += value;
}
remove
{
sftpSendProgressEvent -= value;
}
}
ManualResetEvent fileTransFinishedEvent = new ManualResetEvent(false);
public bool TransFile(string fileName)
{
fileTransFinishedEvent.Reset();
ThreadPool.QueueUserWorkItem(new WaitCallback(TransFileInner), fileName);
fileTransFinishedEvent.WaitOne();
}
private void TransFileInner(object oFileName)
{
try
{
bool sendRes = SendFile(fileName.ToString());//该函数耗时较长,里面会调用SftpSendProgressEvent汇报进度
}
catch(Exception ex)
{
}
fileTransFinishedEvent.Set();
}
}
public class MainFrm:Form
{
public void button1_click()
{
ProtocolFileSend proSFTP = new ProtocolFileSend(remoteIP, remotePort);
proSFTP.SftpSendProgressEvent += new ProtocolFileSend.ShowProcessHandle(proSFTP_SftpSendProgress);
bool res = proSFTP.TransFile(fileName);
}
void proSFTP_SftpSendProgress(int percent, string message)
{
UpdateProgressBar(percent, message);//写日志发现 该行确实执行了,但是是在 SendFile最后阶段才执行的。所在在等待过程中进度条没有更新,只是最后快结束的时候进度条刷新了一下。
}
}
请各位帮忙看下如何处理比较好,谢谢!
------解决方案--------------------