I have written a program in a synchronous model that I now want to add threading to for long tasks, I have several properties that automate the retrieval of certain information like:
private int tabIdx { get { return tabs.SelectedIndex; } }
private string tabName { get { return tabs.SelectedTab.Text; } }
public TabControls TabControls { get { return (TabControls)tabs.TabPages[tabIdx].Controls[string.Format("tab_{0}_controls", tabIdx)]; } }
internal TabControls getTabControls(string name)
{
int desiredIdx = tabs.TabPages.IndexOfKey(name);
return (TabControls)tabs.TabPages[desiredIdx].Controls[string.Format("tab_{0}_controls", desiredIdx)];
}
public Core rCore { get { return rdbCores[tabIdx]; } }
Obviously if I try to access these properties from inside Task.Run(())
or likewise I'm going to get a cross-thread exception.
So my question is:
Is it possible to Invoke these properties with an method similar too:
private void invokeIfRequired(ISynchronizeInvoke obj, MethodInvoker action)
{
if (obj.InvokeRequired) { obj.Invoke(action, new object[0]); }
else { action(); }
}