I have a UserControl with a TreeView control called mTreeView. I can receive data updates from several different streams, and this leads to a TreeView update. To do this, I developed the following template: all data update event handlers should get a lock, and then check InvokeRequired; if so, do the work by calling Invoke. Here is the relevant code:
public partial class TreeViewControl : UserControl
{
object mLock = new object();
void LockAndInvoke(Control c, Action a)
{
lock (mLock)
{
if (c.InvokeRequired)
{
c.Invoke(a);
}
else
{
a();
}
}
}
public void DataChanged(object sender, NewDataEventArgs e)
{
LockAndInvoke(mTreeView, () =>
{
mTreeView.BeginUpdate();
mTreeView.EndUpdate();
});
}
}
, , , InvalidOperationException mTreeView.BeginUpdate(), , mTreeView , , . LockAndInvoke, , c.InvokeRequired , else ! InvokeRequired true , else.
- , , ?
EDIT: , , InvokeRequired , , . , . ?