I've got everything working except for this call which throws an error no matter how I change things.
This is a WPF Application written in C#.
Things I've Tried:
- Accessing it from a Static Context
- Accessing it from the UI Thread (mainWindow.img)
Here is my current code where I access it in a static context:
var worker = new Thread(() => RenderBarcodeImage(imageBytes));
worker.SetApartmentState(ApartmentState.STA);
worker.Name = "RenderBarcodeImage";
worker.IsBackground = true;
worker.Start();
worker.Join();
Application.Current.Dispatcher.Invoke(new Action(() =>
{
var mainWindow = Application.Current.MainWindow as MainWindow;
if (mainWindow == null) return;
mainWindow.PairingBarcodeImage.Stretch = mainWindow.PairingBarcodeImage.Width < MainWindow.img.Width ? Stretch.Uniform : Stretch.None;
mainWindow.PairingBarcodeImage.Source = MainWindow.img.Source;
if (mainWindow.PairingBarcodeImage.Source != null)
mainWindow.SaveBarcodeButton.IsEnabled = true;
}));
No matter what every time I try to access img
it throws
InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
How would I access the img
object from another thread?