Reputation: 41
I am trying to use threading and that includes a GUI controller, so I needed to invoke that controller but I have this problem- I cannot invoke a non-delegate type in this part of the code
if (toolStripTextBox1.Text != "")
{
if (RT[j].tabvalue.ToString() == tabControl1.Invoke(() =>
{ tabControl1.SelectedTab.Name; }))
{
RT[j].RitchT.Text = getH.getHtmlCode(toolStripTextBox1.Text);
}
}
while this is working on the other hand
tabControl1.Invoke(
() => { tabControl1.TabPages[R.RitchT.Name].Controls.Add(R.RitchT);
});
This is the invoke class
public static class ControlExtensions
{
public static void Invoke(this Control control, Action action)
{
if (control.InvokeRequired)
control.Invoke(new MethodInvoker(action), null);
else
action.Invoke();
}
}
How can I solve this?
Upvotes: 3
Views: 15081
Reputation: 942257
tabControl1.Invoke(() => { tabControl1.SelectedTab.Name; })
Yes, that's not legal syntax. The Control.BeginInvoke() method takes a Delegate as its first argument. A lambda expression doesn't convert implicitly to Delegate, you have to help. Fix:
tabControl1.Invoke(new Func<string>(() => { return tabControl1.SelectedTab.Name; }))
You'll have more trouble, you cannot assign the Text property of a RichTextBox in a worker thread, you'll have to use BeginInvoke() there as well. You'll get away with reading the Text properties but it is rather a bad idea, they may change while the worker thread is running and the user keeps operating the UI. You really want to favor a BackgroundWorker or Task, collect the data you need for the worker before you start it, update the UI with the results of the worker after it is done.
Upvotes: 3