Daa*_*aan 0 c# wpf multithreading
在论坛上似乎发现了很多这些错误,但我不能在我的情况中应用大部分错误.
我的问题:
我有一个页面:PosterHome.xaml,上面有一个统一的网格.在我的代码隐藏中,我有一个drawthread:
drawThread = new Thread(new ThreadStart(drawPosters));
drawThread.SetApartmentState(ApartmentState.STA);
drawThread.Start();
Run Code Online (Sandbox Code Playgroud)
这个threadmethod(drawPosters)偶尔被另一个类唤醒,使用autoresetevent.我正在更改统一网格行的那一刻,我在这个方法中得到错误:
while (true)
{
waitEvent.WaitOne();
//do some calculations
// change uniform grid rows & cols
posterUniformGrid.Rows = calculatedRows; //**-> error is first thrown here**
posterUniformGird.Columns = calculatedCols;
}
Run Code Online (Sandbox Code Playgroud)
我该怎么处理?提前致谢.
迎接大安
您正在尝试访问posterUniformGrid从后台线程在UI线程上创建的内容.
要避免这种情况,请使用Dispatcher.
Dispatcher.Invoke(DispatcherPriority.Normal,
new Action<object[]>(SetGrid),
new object[] { calculatedRows, calculatedColumns });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2451 次 |
| 最近记录: |