Ok, my current code is working, and I will probably try to do the same with Async CTP. But I still want to understand what is happening.
I have a function like below
// In MainWindow.xaml.cs Task.Factory.StartNew(() => helper.Send()); // In class HttpHelper public void Send() { // ... try { Status = Statuses.Uploading; // write to request stream Status = Statuses.Downloading; // write to response stream Status = Statuses.Idle; // the exception is thrown here // ... } catch (Exception e) { // ... } }
Full code for HttpHelper @pastebin . Send() on line 76
I wonder why I get an exception? Maybe I did something wrong with streaming processing, but why does the exception only occur after I have successfully set the Status property 2 times?
UPDATE: Reason ...
I had an event handler listening to the StatusChanged event, in condition 1 if I forgot to use the UI thread to update the UI
helper.StatusChanged += (s, evt) => { _dispatcher.Invoke(new Action(() => txtStatus.Text = helper.Status.ToString())); // I used _dispatcher here correctly if (helper.Status == HttpHelper.Statuses.Idle || helper.Status == HttpHelper.Statuses.Error) progBar.IsIndeterminate = false; // but not here };
source share