1

Suppose I have a HttpHelper class that has a GetResponseStream(), that upload request data showing progress via events StatusChanged & ProgressChanged.

public MemoryStream GetResponseStream() {
    ...
    Status = Statuses.Uploading; // this doesn't raise StatusChanged
    // write to request stream
    ... // as I write to stream, ProgressChanged doesn't get raised too
    Status = Statuses.Downloading; // this too
    // write to response stream
    ... // same here
    Status = Statuses.Idle; // this runs ok. Event triggered, UI updated
}

Code @pastebin. GetRequestStream() on line 76. The class itself works fine except the using class need to call it like below

HttpHelper helper = new HttpHelper("http://localhost/uploadTest.php");
helper.AddFileHeader("test.txt", "test.txt", "text/plain", File.ReadAllBytes("./test.txt"));
helper.StatusChanged += (s, evt) =>
{
    _dispatcher.Invoke(new Action(() => txtStatus.Text = helper.Status.ToString()));

    if (helper.Status == HttpHelper.Statuses.Idle || helper.Status == HttpHelper.Statuses.Error)
        _dispatcher.Invoke(new Action(() => progBar.IsIndeterminate = false));

    if (helper.Status == HttpHelper.Statuses.Error)
        _dispatcher.Invoke(new Action(() => txtStatus.Text = helper.Error.Message));
};
helper.ProgressChanged += (s, evt) =>
{
    if (helper.Progress.HasValue)
        _dispatcher.Invoke(new Action(() => progBar.Value = (double)helper.Progress));
    else
        _dispatcher.Invoke(new Action(() => progBar.IsIndeterminate = true));
};
Task.Factory.StartNew(() => helper.GetResponseString());

If I had called the class using

helper.GetResponseString();

Then the class itself will work, but events don't seem to be raised. I think it has to do with the UI thread being blocked. How can I at recode the class such that its easier/cleaner for the using class to use it, without all the _dispatcher & Task stuff.

Also, I will like to know for sure whats causing the events/UI to not update. Even if the code is synchronous, can't it run the property changed/events anyways, its after the read/write afterall?

2
  • You should set the PasteBin syntax highlighting to C#. pastebin.com/FeAPB6rU Commented Nov 23, 2010 at 2:25
  • @SLaks, Thanks for notifying me on that, must have been doing PHP yest and forgot to change the language Commented Nov 23, 2010 at 3:01

1 Answer 1

2

You should look into using the BackgroundWorker instead of hand-crafting this yourself. Use ReportProgress to pass the state of processing to your UI thread.

Sign up to request clarification or add additional context in comments.

1 Comment

While this is probably a good suggestion for this question, I'd like to add a caveat, because there are scenarios in which this might be less good: a problem with this suggestion is that it'll end up consuming a thread for as long as the work is in progress. It's unlikely to be a problem in client-side code (which is what the question seems to be about), but on server-side code it would defeat the point of doing things asynchronously. Full-on async use of networking classes enables far more frugal use of threads, which can sometimes help server scalability. (But would be overengineering here.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.