I want to call a method till it returns a value in my WPF
app.
void btnDecode_Click(object sender, RoutedEventArgs e)
{
var task = new Task(task);
task.Start();
task.Wait();
}
async void task()
{
Task<object> result= DecodedResult((BitmapSource)imageBarcode.Source);
object i = await result;
txtBarcodeContent.Text = i.ToString();
}
async Task<object> DecodedResult(BitmapSource renderTargetBitmap)
{
var reader = new BarcodeReader();
txtBarcodeContent.Text = "reading";
return reader.Decode(renderTargetBitmap);
}
But it throws me an error on task.Start();
"Additional information: The calling thread cannot access this object because a different thread owns it."
Why can't I access it and why does another thread own it?