Reputation: 1461
I have a SL application that is running in a browser. The user presses a button and a OpenFiledDialog
is shown. When the user has selected a file, I want to do some processing on it. However, I also want to show a progress bar and enable the user to cancle the operation.
My code:
var dialog = new OpenFileDoalog { Filter = ... };
if(dialog.ShowDialog() != true)
return;
// Show the Progress bar
MyProgressBar.Visibility = System.Windows.Visibility.Visible;
// Do the processing on another thread
new Thread(() =>
{
var stream = dialog.File.OpenRead();
var data = Process(stream);
Dispatcher.BeginInvoke(() => IGotSomeDataForYou(data));
}).Start();
However, I get an InvalidOperationException
on FileInfo.OpenRead()
because This operation can only occur on the UI Thread.
.
However, if I do the work on the UI thread, the UI freezes as long as the processing is being done. And I cannot open the stream on the UI thread and then process it on the background thread, because that gives an error on cross-thread usage.
I would think this is a common problem but I failed to find a solution. What are my options in this case? Read it to a in-memory byte array first and then provess it? Anything else?
Thanks!
Upvotes: 0
Views: 960
Reputation: 4585
Don't call dialog.File.OpenRead(); in a separate thread. Call it on UI thread them get file returned from opendialog and set it to a private property and then process it on a background thread e.g upload it.
OpenFileDialog has some limitations in SL. check out my question for more details. Dialogs must be user-initiated. Silverlight
Regards.
Upvotes: 1