tags:

views:

19

answers:

2

Hello,

Let's say I have a TabControl with 2 tabs and a TreeView with 2 nodes at the root.

I want the application to behave in the following manner : if I click on Node1 from the treeview, the Tab1 from the TabControl should become active. Similarly, when I click on Tab2, I want the Node2 to become selected. And vice-versa.

So I simply coded this with TreeView_AfterSelect and TabControl_SelectedIndexChanged.

How do avoid "race conditions" between the 2 controls since they are triggering each other's event?

A: 

It is all on the UI thread, not sure what race conditions you are talking about. In addition events should not care about other events. Events come at no guaranteed point in time. Building an application contingent on events without a defined eventing architecture in your application will create many nightmares down stream.

Aaron
+1  A: 

Just do a check in each event to make sure that the node/tab you intend to select is not already selected. This isn't actually a "race condition". A race condition is when the order of two events is not guaranteed.

For example:

private void MyTreeView_AfterSelect (object sender, EventArgs e) {
    if (MyTreeView.SelectedNode == node2 && MyTab.SelectedIndex != 1 ) {
        MyTab.SelectedIndex = 1;
    }
}
Josh Einstein
thanks, I was not talking about race conditions (threads) per se, but didn't know how to word it :) this seems good enough to block triggering the events in cascade.
ibiza