I really do not understand how is this error happening at this code. Please check the code yourself
void dispatcherTimer_Tick(object sender, EventArgs e)
{
string srUrl = lstLocalIndex[irLocalIndex] + lstMainIndex[irMainIndex].Replace("0;","");
Task.Factory.StartNew(() =>
{
startNewWindow(srUrl);
});
}
void startNewWindow(string srUrl)
{
NewWindowThread<TitleWindow, string>(c => new TitleWindow(c), srUrl);
}
Now this code is where the error happening. I will also attach screenshot
private void NewWindowThread<T, P>(Func<P, T> constructor, P param) where T : Window
{
Thread thread = new Thread(() =>
{
T w = constructor(param);
w.Show();
w.Closed += (sender, e) => w.Dispatcher.InvokeShutdown();
try
{
System.Windows.Threading.Dispatcher.Run();
}
catch
{
}
});
thread.SetApartmentState(ApartmentState.STA);
try
{
thread.Start();
}
catch
{
}
}
This error causes whole software throw error and stop working even though i am calling them in new thread :(
This line throwing error System.Windows.Threading.Dispatcher.Run();
Please check also screenshot
C# 4.0 WPF
Task
and then theThread
, better to just place the code you want to run as startup code in theThread
. – Sardou