none
UI thread is blocked/freezed... RRS feed

  • Question

  • Hi All ,

    I have around 5 big task that are performed sequentially and the output for the first one makes the second task to work , so if the first task fail , second one will not kick off.

    While this is gng on i need to keep updating the UI on the status that are coming from these tasks and also allow them to click them on various buttons that are there.

    I am using WPF + C#

    It will be really gr8 if someone can thru some light on this . I cannot perform the tasks asyn/background as the other task are also lying in the main thread waiting for the result from previuos one.

    Thanks in advance.

    Friday, January 28, 2011 5:28 PM

Answers

  • It will be really gr8 if someone can thru some light on this . I cannot perform the tasks asyn/background as the other task are also lying in the main thread waiting for the result from previuos one.

     

    If you want to keep your UI responsive, you'll really need to push these tasks into a background thread.  That being said, you can push ALL of the tasks into a single background thread, so they still work synchronously, but then marshal the calls to your UI across for status updates.

     

    This will keep your interface responsive, plus allow you to do the work sequentially.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, January 28, 2011 5:32 PM
    Moderator
  • What you're asking is essentially to preform work on the UI thread and yet keep it listening to requests from the user. You simply can't do the two things, you'll have to run your tasks in a different thread. However, running a different thread doesn't necessarily mean your run your tasks parallel to each other, if you keep your code checked you can easily preform your tasks in order using different threads.

    First of all, if you run the tasks without any user input you should have no problems doing it on a different thread. If you want the user to interact before each task is preform then you just need to disable (not freeze) the UI until the current task is done. You can do this very easily by using the BackgroundWorker class.

    This class enables you to run your task on a different thread while maintaining full control over it's execution status from the main thread. All you need to do is to create a worker for each of your tasks and register your execution code to the DoWork event, then when you'll run the DoWorkAsync the event would fire. As long as you set the WorkerReportProgress to true, you'll be able to report your progress back to the main thread. The RunWorkerCompleted event would fire when you finish the work in the DoWork event (Notice this could also mean the thread was cancelled), then you'll be able to run the next task. All this while keeping the UI responsive.

    Check out the link I provided, it contains more information then I specified here as well as working examples.

    Regards

    Yoni

    Friday, January 28, 2011 5:55 PM
  • Seems the best candidate for BackgroundWorker. You can update UI with StatusChanged event handler and start the next task in WorkCompleted event handler.

    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

    You don't even need to marshal your calls to UI thread using Dispatcher as these event handlers are executed on UI thread.

    Thanks,
    Muhammad
    http://shujaatsiddiqi.blogspot.com/


    Muhammad Siddiqi
    Friday, January 28, 2011 10:27 PM

All replies

  • It will be really gr8 if someone can thru some light on this . I cannot perform the tasks asyn/background as the other task are also lying in the main thread waiting for the result from previuos one.

     

    If you want to keep your UI responsive, you'll really need to push these tasks into a background thread.  That being said, you can push ALL of the tasks into a single background thread, so they still work synchronously, but then marshal the calls to your UI across for status updates.

     

    This will keep your interface responsive, plus allow you to do the work sequentially.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, January 28, 2011 5:32 PM
    Moderator
  • What you're asking is essentially to preform work on the UI thread and yet keep it listening to requests from the user. You simply can't do the two things, you'll have to run your tasks in a different thread. However, running a different thread doesn't necessarily mean your run your tasks parallel to each other, if you keep your code checked you can easily preform your tasks in order using different threads.

    First of all, if you run the tasks without any user input you should have no problems doing it on a different thread. If you want the user to interact before each task is preform then you just need to disable (not freeze) the UI until the current task is done. You can do this very easily by using the BackgroundWorker class.

    This class enables you to run your task on a different thread while maintaining full control over it's execution status from the main thread. All you need to do is to create a worker for each of your tasks and register your execution code to the DoWork event, then when you'll run the DoWorkAsync the event would fire. As long as you set the WorkerReportProgress to true, you'll be able to report your progress back to the main thread. The RunWorkerCompleted event would fire when you finish the work in the DoWork event (Notice this could also mean the thread was cancelled), then you'll be able to run the next task. All this while keeping the UI responsive.

    Check out the link I provided, it contains more information then I specified here as well as working examples.

    Regards

    Yoni

    Friday, January 28, 2011 5:55 PM
  • Seems the best candidate for BackgroundWorker. You can update UI with StatusChanged event handler and start the next task in WorkCompleted event handler.

    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

    You don't even need to marshal your calls to UI thread using Dispatcher as these event handlers are executed on UI thread.

    Thanks,
    Muhammad
    http://shujaatsiddiqi.blogspot.com/


    Muhammad Siddiqi
    Friday, January 28, 2011 10:27 PM
  •  

    We are marking this issue as "Answered". If you have any new findings or concerns, please feel free to unmark the issue.

     

    Best regards


    Yves Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 10, 2011 9:16 AM
    Moderator