Results 1 to 9 of 9

Thread: Help with showing Window Form

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Help with showing Window Form

    hi guys help please..
    I have a code below that basically display a form (formMsgPopUp) and call a function (ownerForm.retCode = ownerForm.Transmit() that would take a few second
    before it can return a value and after it returns a value it will then close the form that it opens before it calls the function. But what is happening right now is when i call the .Show() method (formMsgPopUp.Show() the form is not properly displayed (Please see attached screenshot). The form is the cue to the user that some action is being perform and it would take a few seconds.

    CODE:
    Code:
                    FrmMsgPopUp formMsgPopUp = new FrmMsgPopUp();
                    formMsgPopUp.Show();
    
                    ownerForm.retCode = ownerForm.Transmit();
    
                    formMsgPopUp.Close();
    Current displayed form


    should be something like this.
    Attached Images Attached Images   

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Help with showing Window Form

    Thats because the code is running on a single thread, the thread that handles all the painting and updating for the formMsgPopUp form will be too busy executing the code in the Transmit method. You'd have to run the code in the Transmit method in a new thread OR show the second form in a new thread.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Help with showing Window Form

    Thanks for that...but when i use the thread the form quickly disappear..Please see my code below.
    Code:
                    System.Threading.Thread tMsg = new System.Threading.Thread(new System.Threading.ThreadStart(showMsgBox));
                    tMsg.Start();
                    System.Threading.Thread.Sleep(1000);
                    ownerForm.retCode = ownerForm.Transmit(); 
    
            public static void showMsgBox()
            {
                FrmMsgPopUp FormMsgPopUp = new FrmMsgPopUp();
                FormMsgPopUp.Show();
            }

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Help with showing Window Form

    Ah yes, that would be because the thread terminates once it reaches the end of showMsgBox, so the form would terminate along with its thread. Call ShowDialog instead of Show to keep the thread from terminating until the user has closed the form.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Help with showing Window Form

    You should be aware that displaying a form by calling ShowDialog in a background thread will not make it behave like a modal dialogue. The form is modal to the thread that owns it, so the background thread will block but the user will be able to ignore the dialogue and keep working in the main form. If that's OK then that's OK but it's something to be aware of.

  6. #6
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary, Canada
    Posts
    453

    Re: Help with showing Window Form

    As has been mentioned, the thread that you are running in is opening the form, doing it's stuff and then closing the form without giving any processing time over to windows to actually draw the formMsgPopUp form.

    Now you could put the line
    Code:
    Application.DoEvents();
    after the show, which would allow windows to do it's house keeping and redraw the form. But many people would burn you at the stake for using such a sledge hammer to crack a nut.

    A neater solution is to simply call the Refresh event on the form after you show it, which will cause it to be redrawn.

    So your code becomes:
    Code:
    FrmMsgPopUp formMsgPopUp = new FrmMsgPopUp();
                    formMsgPopUp.Show();
                    formMsgPopUp.Refresh();
                    ownerForm.retCode = ownerForm.Transmit();
    
                    formMsgPopUp.Close();
    "I'd rather have a full bottle in front of me than a full frontal lobotomy!"

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Help with showing Window Form

    Quote Originally Posted by SurfDemon
    As has been mentioned, the thread that you are running in is opening the form, doing it's stuff and then closing the form without giving any processing time over to windows to actually draw the formMsgPopUp form.

    Now you could put the line
    Code:
    Application.DoEvents();
    after the show, which would allow windows to do it's house keeping and redraw the form. But many people would burn you at the stake for using such a sledge hammer to crack a nut.

    A neater solution is to simply call the Refresh event on the form after you show it, which will cause it to be redrawn.

    So your code becomes:
    Code:
    FrmMsgPopUp formMsgPopUp = new FrmMsgPopUp();
                    formMsgPopUp.Show();
                    formMsgPopUp.Refresh();
                    ownerForm.retCode = ownerForm.Transmit();
    
                    formMsgPopUp.Close();
    Yes that would force the form to redraw, however if you were to click the window or in any other way interact with it, it would become "white" and unresponsive as shown in the first post on this thread.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Help with showing Window Form

    Basically, all UI elements should be handled by the UI thread. If you want to display a form it should be done from the UI thread. You could display the form in the UI thread and then use a BackgroundWorker in that form to do the work. The form is owned by the UI thread but it remains responsive because the work is being done in a background thread.

  9. #9
    Hyperactive Member
    Join Date
    Mar 2001
    Location
    Calgary, Canada
    Posts
    453

    Re: Help with showing Window Form

    Quote Originally Posted by Atheist
    Yes that would force the form to redraw, however if you were to click the window or in any other way interact with it, it would become "white" and unresponsive as shown in the first post on this thread.
    Mmmm. Unresponsive yes. But it shouldn't become white (undrawn). It will just stay there (being unresponsive). Which I think is what they are looking for.
    "I'd rather have a full bottle in front of me than a full frontal lobotomy!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width