0

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(); }
}
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76

1 Answers1

0

In general, I use the following to invoke Properties from different Tasks. I can't follow your code, wehre you want to invoke, so you have to adjust the code to you application needs:

button1.Invoke(new Action(()=>button1.Visible = true;))
Jack
  • 60
  • 8