简体   繁体   中英

Invoke Property

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(); }
}

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;))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM