问题描述
我有乐趣与WPF和麻烦了。我用Google搜索,发现这个网站拥有的我,但没有任何可行的解决方案同样的问题。
的问题是,我有一个按钮,执行数据的一些处理(约30秒)。我想有按钮禁用,并有日志写在文本框中......问题是,它并没有禁止,并没有写任何东西在文本框,直到处理完全做到了。
你知道吗?
私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
this.button1.IsEnabled = FALSE;
//长的东西在这里
txtLog.AppendText(Environment.NewLine +BLABLA);
//这里结束很长的东西
this.button1.IsEnabled = TRUE;
}
正如其他人所说,使用的或做异步地工作的其他方法。
您可以在您的窗口
声明它,地方初始化它像加载
事件,并用它在点击
事件。这是你的方法,改为使用的BackgroundWorker
,假设你已经声明它的窗下
为 _bw
:
私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
{
_bw =新的BackgroundWorker();
_bw.DoWork + =新DoWorkEventHandler((0,参数)=>
{
//长的东西在这里
this.Dispatcher.Invoke((行动)(()=> txtLog.AppendText(Environment.NewLine +BLABLA)));
});
_bw.RunWorkerCompleted + =新RunWorkerCompletedEventHandler((0,参数)=>
{
//这里结束很长的东西
this.Dispatcher.Invoke((行动)(()=> this.button1.IsEnabled = TRUE));
});
}
私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
this.button1.IsEnabled = FALSE;
_bw.RunWorkerAsync();
}
请注意,任何的修改您的UI从另一个线程必须在的或的打电话,WPF不允许您要获取或设置的DependencyProperty
从任何线程值,但一来创建对象,其中(更多关于此这里)。
如果你想从 txtLog
来阅读,而不是修改它,在code将是相同的:
//长的东西在这里
this.Dispatcher.Invoke((行动)(()=>
{
字符串myLogText = txtLog.Text;
myLogText = myLogText + Environment.NewLine +BLABLA;
txtLog.Text = myLogText;
}));
I am having fun with WPF and got a problem. I have googled and found this website that has the same problem of me but without any working solution.
The problem is that I have a button that do some processing of data (around 30 sec). I want to have the button to disable and to have log writing in a text box... the problem is that it doesn't disable and it doesn't wrote any thing on the textbox until the processing is completely done.
Any idea?
private void button1_Click(object sender, RoutedEventArgs e)
{
this.button1.IsEnabled = false;
//Long stuff here
txtLog.AppendText(Environment.NewLine + "Blabla");
//End long stuff here
this.button1.IsEnabled = true;
}
As others have said, use the BackgroundWorker
or some other method of doing work asychronously.
You can declare it under your Window
, initialize it somewhere like the Loaded
event, and use it in the Click
event. Here's your method, modified to use BackgroundWorker
, assuming you've declared it under the Window
as _bw
:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_bw = new BackgroundWorker();
_bw.DoWork += new DoWorkEventHandler((o, args) =>
{
//Long stuff here
this.Dispatcher.Invoke((Action)(() => txtLog.AppendText(Environment.NewLine + "Blabla")));
});
_bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler((o, args) =>
{
//End long stuff here
this.Dispatcher.Invoke((Action)(() => this.button1.IsEnabled = true));
});
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.button1.IsEnabled = false;
_bw.RunWorkerAsync();
}
Note that anything that modifies your UI from another thread must be done within a Dispatcher.Invoke
or Dispatcher.BeginInvoke
call, WPF does not allow you to get or set DependencyProperty
values from any thread but the one where the object was created (more about this here).
If you wanted to read from txtLog
instead of modifying it, the code would be the same:
//Long stuff here
this.Dispatcher.Invoke((Action)(() =>
{
string myLogText = txtLog.Text;
myLogText = myLogText + Environment.NewLine + "Blabla";
txtLog.Text = myLogText;
}));
这篇关于在一个循环中的WPF应用程序,怎么没有整个应用程序冻结?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!