很多关于如何调用方法的例子,但是如何改变一个简单的属性?
为了演示,这里有一组非常简单的代码应该有所帮助.假设我需要从子表单设置visible属性,因此需要调用它:
Friend Sub activateItem(ByVal myItem As PictureBox)
If myItem.InvokeRequired = True Then
????
Else
myItem.Visible = True
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
谢谢
我想让我的getter线程安全.当我这样做时,我收到一个错误:
public ApplicationViewModel SelectedApplication
{
get
{
if (InvokeRequired)
{
BeginInvoke((Action<ApplicationViewModel>)SelectedApplication);
}
return _applicationsCombobox.SelectedItem as ApplicationViewModel;
}
}
Run Code Online (Sandbox Code Playgroud)
我有错误:
Cannot cast expression of type 'Foo.Model.ApplicationViewModel' to type 'Action<ApplicationViewModel>'
Run Code Online (Sandbox Code Playgroud) Lets assume that I have worker threads that increment a value on some control. Since an invoke is required, all the increments need to be done on the GUI thread. For that I use BeginInvoke.
My question is:
Can a race condition break the increment of the control, because multiple worker threads all invoked on the GUI thread simultaniously (and the increment itself someControl.Value += value; is obviously not atomic)?
Or to put it the opposite:
Is one Invoke guaranteed …
项目是C#.
所以我有一堆多线程代码,旨在作为库运行.这是UI的一个单独项目.
我的库有一个中心对象,需要在创建触发事件的任何内容之前创建.
是否可以在某些对象中传递此主对象,以便我的事件可以确定何时需要调用它们以返回到主UI线程?
我真的很想让UI不必进行大量的调用,因为他的事件处理程序几乎总是从一些随机的后台线程调用.
虽然关于使用Control.Invoke从工作线程更新GUI控件有很多关于SO的问题/答案,但我还是无法清楚地了解从控件读取数据而不更新它的主题.
示例:我在Windows窗体上有一个DataGridView.从工作线程,我想检查DGV上特定单元格的值,但我不需要/想要更新该值.从工作线程我调用一个方法,该方法将DataGridView对象作为其唯一参数,然后该方法检查DataGridView中的特定单元格但不修改它,在这种情况下我是否需要使用.Invoke?
现在代码似乎运行正常,没有明显的警告而没有使用.Invoke.但是,我不清楚这是否安全,因为它不会更新DGV,而只是查看DGV的细胞而不修改它们中的任何一个.
有人可以对此有所了解吗?我很抱歉,如果这是一个重复的问题,但我找不到任何其他问题,只关注只读一个控件而不在多线程情况下更新/修改它.
谢谢.
从工作线程调用的示例方法:
private DataGridViewRow getRowObject(DataGridView dgv)
{
foreach (DataGridViewRow row in dgv.Rows)
{
if ((int)dgv[specialColumn, row.Index].Value == 100)
return row;
}
}
Run Code Online (Sandbox Code Playgroud)