locked
Update UI without using multiple threads RRS feed

  • Question

  • Hi All,

    Is there any way to update my form during a lenghty operation WITHOUT using threading? I mean, I have a form with a couple of buttons. Clicking one of the buttons triggers a lengthy operation. During this operation I want to show some kind of progress indicator on the form (like a spinning wheel icon or something like that) to give the user some feedback (as opposed to the unresponsive form that I have now). I used background worker thread and achieved what I wanted to do. But my form is hosted in another application and multi threading is risky in that app (which is a huge app btw), so that solution is ruled out. So,  I am searching for other ways to do this. I tried invoke, begininvoke etc but nothing seems to work.

    Any comments are welcome. Please share any related info.

    Thanks in advance,
    Surya K C

     

    Wednesday, June 16, 2010 9:12 PM

Answers

  • You should use threads.  When executed properly, threads perform their work seamlessly.  The BackgroundWorker class behaves as if it were tuned to update a Windows Form.

    Why have you ruled out threads?  A "huge app" is not a reason.  If memory is being squeezed down to nothing, then the main app is the issue and needs to employ better memory management techniques. 

    Rudy  =8^D

     


    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Reed Copsey, JrMVP Wednesday, June 16, 2010 10:01 PM
    • Marked as answer by Jing0 Tuesday, June 22, 2010 8:50 AM
    Wednesday, June 16, 2010 9:50 PM
  • Yeh.  There is a way of updating inside a long running process that many use:  Application.DoEvents().  Using that will open up your "huge app" to far worse possibilities than any BackgroundWorker could.  A clean pre-.NET way to do processing essentiallly in the background was to use a state timer.  When the timer counts down, the app is in an idle state and is free to respond to messages and update as required.  You simply make a loop around a WindowsFormsTimer:

        int timerState = 0;
        
    int maxState = 1;
        
    private void timer1_Tick(object sender, EventArgs e)
        {
          timer1.Stop();
          
    switch (timerState)
          {
            
    case 0:
              
    //Do state 0 stuff.
              break;
            
    case 1:
              
    //Do state 1 stuff.
              break;
          }
          
    if (timerState < maxState) timerState += 1; else timerState = 0;
          timer1.Start();
        }

    • Marked as answer by Jing0 Tuesday, June 22, 2010 8:50 AM
    Thursday, June 17, 2010 6:44 PM

All replies

  • You should use threads.  When executed properly, threads perform their work seamlessly.  The BackgroundWorker class behaves as if it were tuned to update a Windows Form.

    Why have you ruled out threads?  A "huge app" is not a reason.  If memory is being squeezed down to nothing, then the main app is the issue and needs to employ better memory management techniques. 

    Rudy  =8^D

     


    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Reed Copsey, JrMVP Wednesday, June 16, 2010 10:01 PM
    • Marked as answer by Jing0 Tuesday, June 22, 2010 8:50 AM
    Wednesday, June 16, 2010 9:50 PM
  • Thanks a lot for the response Rudy!

    I might not have given the right reasons for not going for threading but there are quite a few of them :). One of them, and the most important of them, is that my management doesn't want to take a risk just a few weeks from release. The form that I am working on is something that they can do without :), so they don't want to take any chances by introducing multi threading into the product which makes certification a bigger task than what we can afford at this moment.
    So, even though they liked my solution with the background worker thread (which works like a breeze btw :)), they want me to explore any other ways of doing this without using multiple threads.
    That's what I am trying to find out, if this can be done on a single thread. I don't have much experience with pre-.NET era windows applications coding, so I am pretty sure there are ways to do this that I am missing. One of my colleagues mentioned message loops and being able to achieve this (updating UI on a single thread in the middle of a lengthy operation). I don't remember the exact correlation that he implied at that time but that is what I remember.

    So, to summarize, even if multiple threads is the way to go, is there any other way to do what I am trying to do without using multiple threads?

    Thanks in advance,
    Surya K C

     

     

    Thursday, June 17, 2010 2:00 PM
  • Yeh.  There is a way of updating inside a long running process that many use:  Application.DoEvents().  Using that will open up your "huge app" to far worse possibilities than any BackgroundWorker could.  A clean pre-.NET way to do processing essentiallly in the background was to use a state timer.  When the timer counts down, the app is in an idle state and is free to respond to messages and update as required.  You simply make a loop around a WindowsFormsTimer:

        int timerState = 0;
        
    int maxState = 1;
        
    private void timer1_Tick(object sender, EventArgs e)
        {
          timer1.Stop();
          
    switch (timerState)
          {
            
    case 0:
              
    //Do state 0 stuff.
              break;
            
    case 1:
              
    //Do state 1 stuff.
              break;
          }
          
    if (timerState < maxState) timerState += 1; else timerState = 0;
          timer1.Start();
        }

    • Marked as answer by Jing0 Tuesday, June 22, 2010 8:50 AM
    Thursday, June 17, 2010 6:44 PM
  •  

    If there is anything unclear, please feel free to ask.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, June 22, 2010 8:50 AM