I have simple WPF application where I using threads. E.g. In new thread I get DateTime and I want return it to the TextBox in main thread. I read to do this I must use ControlDispatcher.Invoke method. But something is wrong..
namespace Bizantyjskie
{
public static class ControlExtensions
{
public static void InvokeIfRequired(this Control control, Action action)
{
if (System.Threading.Thread.CurrentThread != control.Dispatcher.Thread)
control.Dispatcher.Invoke(action);
else
action();
}
public static void InvokeIfRequired<T>(this Control control, Action<T> action, T parameter)
{
if (System.Threading.Thread.CurrentThread != control.Dispatcher.Thread)
control.Dispatcher.Invoke(action, parameter);
else
action(parameter);
}
}
class watki
{
public watki(MainWindow mw)
{
_mw = mw;
}
public MainWindow _mw;
public void dzialaj()
{
Thread watek1 = new Thread(new ThreadStart(w1));
watek1.Start();
}
private void w1()
{
string godz = DateTime.Now.TimeOfDay.ToString();
ControlExtensions.InvokeIfRequired((value) => _mw.tb_w1.Text = value, godz);
}
}
}
Problem is with
ControlExtensions.InvokeIfRequired((value) => _mw.tb_w1.Text = value,
godz);
I got an error.
Cannot convert lambda expression to type
‘System.Windows.Controls.Control’ because it is not a delegate type
You’re calling an extension method as a static one and forgetting to pass a parameter. Change it to this:
Alternatively, if you still want to call it as a static method: