RT
这个问题已经困扰我很久了,查了很多资料,还是没解决
是这样的,我做一个知识库管理编辑器,用的是RichTextBox,导入文件用的是LoadFile方法。
当LoadFile加载大数据(比如:100M)时,为了友好响应,所以会弹出WaitForm,可是WaitForm却不能响应,代码如下:/// <summary>
private delegate void LoadFileDelegate(string path, RichTextBoxStreamType streamType); /// 加载文件
/// </summary>
/// <param name="path"></param>
public bool LoadFile(string path)
{
WaitForm waitForm = null; bool isSuc = true;
RichTextBoxStreamType streamType = RichTextBoxStreamType.RichText; try
{
Thread thread = new Thread(new ThreadStart(delegate()
{
System.Threading.Thread.Sleep(200);
LoadFileDelegate loadFileDelegate = new LoadFileDelegate(LoadFile);
loadFileDelegate.BeginInvoke(path, streamType,
result =>
{
LoadFileDelegate loadFileDelegate2 = (LoadFileDelegate)result.AsyncState;
loadFileDelegate2.EndInvoke(result);
if (waitForm != null)
{
waitForm.Invoke(new MethodInvoker(delegate()
{
waitForm.Close();
waitForm.Dispose();
}));
}
}, loadFileDelegate);
}));
thread.Start(); waitForm = new WaitForm("正在导入文件,请稍侯...");
waitForm.ShowDialog();
//txtEditor.LoadFile(path, streamType);
}
catch (Exception ex)
{
LogFile.WriteLogInfo(ex.ToString(), "编辑器【加载文件】失败!");
isSuc = false;
}
return isSuc;
}
private void LoadFile(string path, RichTextBoxStreamType streamType)
{
this.BeginInvoke(new MethodInvoker(delegate()
{
txtEditor.LoadFile(path, streamType);
}));
}
这个问题已经困扰我很久了,查了很多资料,还是没解决
是这样的,我做一个知识库管理编辑器,用的是RichTextBox,导入文件用的是LoadFile方法。
当LoadFile加载大数据(比如:100M)时,为了友好响应,所以会弹出WaitForm,可是WaitForm却不能响应,代码如下:/// <summary>
private delegate void LoadFileDelegate(string path, RichTextBoxStreamType streamType); /// 加载文件
/// </summary>
/// <param name="path"></param>
public bool LoadFile(string path)
{
WaitForm waitForm = null; bool isSuc = true;
RichTextBoxStreamType streamType = RichTextBoxStreamType.RichText; try
{
Thread thread = new Thread(new ThreadStart(delegate()
{
System.Threading.Thread.Sleep(200);
LoadFileDelegate loadFileDelegate = new LoadFileDelegate(LoadFile);
loadFileDelegate.BeginInvoke(path, streamType,
result =>
{
LoadFileDelegate loadFileDelegate2 = (LoadFileDelegate)result.AsyncState;
loadFileDelegate2.EndInvoke(result);
if (waitForm != null)
{
waitForm.Invoke(new MethodInvoker(delegate()
{
waitForm.Close();
waitForm.Dispose();
}));
}
}, loadFileDelegate);
}));
thread.Start(); waitForm = new WaitForm("正在导入文件,请稍侯...");
waitForm.ShowDialog();
//txtEditor.LoadFile(path, streamType);
}
catch (Exception ex)
{
LogFile.WriteLogInfo(ex.ToString(), "编辑器【加载文件】失败!");
isSuc = false;
}
return isSuc;
}
private void LoadFile(string path, RichTextBoxStreamType streamType)
{
this.BeginInvoke(new MethodInvoker(delegate()
{
txtEditor.LoadFile(path, streamType);
}));
}
在使用C#进行应用程序设计时,经常会采用多线程的方式进行一些后台任务的工作。对于不同的应用场景,使用的策略也不尽相同。
1. 后台循环任务,少量UI更新:例如批量上传文件,并提供进度。这种情况使用BackgroundWorker组件是非常好的选择。
2. 耗时的后台任务:这里的耗时任务是指一个时间较长的任务,并且不能精确获取进度,如:调用一个远程WebService接口。这种情况可以开两个线程,一个工作,一个更新UI(不能提供进度,只能显示动画表示系统在运行中)。
3. 耗时的UI任务:当工作压力集中在UI响应上时,可以在工作者线程中增加延时,从而让UI线程获得响应时间。整个工作的总体时间会增加,但用户响应效果会好很多。
System.Threading.Thread.Sleep(200)
可以看出来sleep时waitForm有响应,sleep后waitForm就没响应了是由于异步调用时达不到效果,所以外边又套用一个线程,还是没效果,代码有点乱我试过把waitForm.ShowDialog();放在单独线程里也没响应大侠,你是高手,我觉得这是所有软件都会遇到的,到底该怎么解决呢?如果只是少量更新UI那肯定没这个问题,我是想如何能异步更新UI
backgroundworker 在处理这种情况上也不行吧
2,第二个方法改成这样:
private void LoadFile(string path, RichTextBoxStreamType streamType)
{
txtEditor.LoadFile(path, streamType);
}
我就不用在我的项目中了,因为还存在保存的问题。
RichTextBox.LoadFile太霸道了,总得卡一下