0

I am using ASP.Net and what i want to achieve is that i have a service that i call, before calling the service i manipulate some UI controls and after the service is completed i manipulate the UI controls again.

Problem is that the UI controls values are never changed in the body of the Task.Factory (After service call is completed) and no exceptions fired.

Here is my code:

            try
        {
            Task ocrTask = Task.Factory.StartNew(() =>
            {
                OCRResult result = new OCRServiceHandler().Start(image);
                if (string.IsNullOrEmpty(result.Error))
                {
                    ViewState["OCRResult"] = result;

                    lbl_Status.Text = string.Empty;
                    div_Result.Visible = true;
                }
                else
                {
                    ViewState["OCRResult"] = null;

                    lbl_Status.Text = result.Error;
                    div_Result.Visible = false;
                }

                //Clear upload file
                myFile.Attributes.Clear();

                //Hide processing image
                img_Processing.Visible = false;
            }, CancellationTask.None, TaskCreationOptions.None, uiScheduler);
        }
        catch (Exception ex)
        {
            throw ex;
        }

I have tried using ContinueWith and update the UI controls there but same results, i have tried using ThreadStart also but same result, i have also tried changing the TaskScheduler type the same happens.

ykh
  • 1,775
  • 3
  • 31
  • 57
  • An aside: That `catch (Exception ex) { throw ex; }` is not needed, and actually [bad](http://stackoverflow.com/a/730255/39709). – Richard Ev Feb 26 '15 at 12:02
  • Yeah, i know. I was just debugging and testing – ykh Feb 26 '15 at 12:02
  • 1
    Gotcha...if you're trying to do what I think you are, then you may find it easier to go to _Debug -> Exceptions_ and check *on* the setting for _Common Language Runtime Exceptions ... thrown_. – Richard Ev Feb 26 '15 at 12:04

1 Answers1

0

You will want your long running work, and the UI updates in separate tasks. The reason being that the long running g task shouldn't be on the UI thread, but updates have to be, by nature. So using a continuewith is a very common pattern.

When you are using continuewith make sure to pass in the ui context as your scheduler. So, from the UIthread(important!) Do task.ContinueWith passing in taskscheduler.fromcurrentsynchronizationcontext as the optional task scheduler parameter.

Edit: just realized your using asp.net. I assume by the wording you are using web forms. Basically ignore the whole UI thread content. It doesn't apply to you. In fact, if this is on page load there is very little value in using a task at all.

Either way you need to hold up the rest of the processing of the page for things to work right.

Likely what you are currently seeing is you are letting the page render, and then trying to modify the contents. Its too late to do so.

You will need asynchronous at the client level to achieve what you want and then return JSON/JavaScript. Can't make a web forms page do its normal load asynchronously, it has to render or not render.

Tim
  • 2,878
  • 1
  • 14
  • 19