Results 1 to 5 of 5

Thread: [2005] Is it possible for a thread to show a form?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    30

    [2005] Is it possible for a thread to show a form?

    I started a thread to open a network connection to a server. If there is an error opening the connection, I want the thread to show a form telling the user to check to see if the server is running, then I put the thread to sleep for five seconds and loop back and try again. When I tried this the form showed, but the label with the message on it is just a blank white box and if I hover the cursor over the form it shows as an hourglass. So I'm wondering if it is it possible to do this or can you only show forms on the UI thread?

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

    Re: [2005] Is it possible for a thread to show a form?

    A thread can certainly show a form. The reason it does what it does is because you are putting the thread to sleep. You are putting this new forms UI thread to sleep and thats gonna cause all event handling, painting and everything to just halt. You should invoke a method on the main thread and open up the form from there, that way you can pause the worker thread without negative effects.
    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
    Junior Member
    Join Date
    May 2007
    Posts
    30

    Re: [2005] Is it possible for a thread to show a form?

    When the thread resumes, shouldn’t it finish displaying the form? The form never fully displays.

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

    Re: [2005] Is it possible for a thread to show a form?

    I dont know, perhaps it has a negative impact on the creation of the form to put the creation thread to sleep so soon.
    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
    111,221

    Re: [2005] Is it possible for a thread to show a form?

    How are you displaying this form? You should be calling ShowDialog and not sleeping the thread until AFTER that returns. That said, it's not such a good idea to display forms on background threads because they may be ignored indefinitely. Even if you call ShowDialog the form will prevent access to the background thread, not the UI thread, so the user could keep on using your main form and simply ignore the message. That's not good design. If you want to interact with the user you should do it on the UI thread. Either delegate and display the form on the UI as a modal dialogue or else use a balloon tip on a NotfifyIcon if you don't want the message to interfere with the user's work on the main form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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