I have UI WPF application.
May by some one have any ideas why this code is not working?
Case 1:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
//some logic
};
worker.RunWorkerAsync();
In that case i am getting exception The calling thread cannot access this object because a different thread owns it. Then i changed it to this:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
this.Dispatcher.BeginInvoke(
new Action(() => { //my code here }), null);
};
After that UI getting frozen during executing this code. Like it is executing in the same thread
Case 2:
BackgroundWorker worker = new BackgroundWorker();
worker.RunWorkerAsync(new Action(() =>
{
//some code here
}));
In that case code inside Action not executed.
//----------------------------------UPDATED--------------------------------------//
Thank you guy's for help
The reason why my code isn't worked propally is mentioned below, i did access to some UI elements in bakground thread. Now i am getting all the values from UI elements before to call BackgroundWorker. I am declared new variables, assign them all values from UI elements i need and then i am passing this variables to BackgroundWorker instead of UI elements like it was before.