Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Form.Show() in a backgroundworker doesn't work

Form.Show() in a backgroundworker doesn't work

Scheduled Pinned Locked Moved C#
question
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    teknolog123
    wrote on last edited by
    #1

    hi, Whenever I start a instance of a form with frm.Show() from a backgroundworkder or a thread other than the mainform's thread like the sample below, the new form is unstable. but it's okay with frm.ShowDialog(). I want to use frm.show() because the code don't stop and wait for the user action. Any idea about it?

    //this way, the frm hangs
    private void bgworker_DoWork(object sender, DoWorkEventArgs e)
    {
    NewWindow frm=new NewWindow(); //this is not the main form
    frm.Show();
    }

    //this way it's okay
    private void bgworker_DoWork(object sender, DoWorkEventArgs e)
    {
    NewWindow frm=new NewWindow(); //this is not the main form
    frm.ShowDialog();
    }

    L L B 3 Replies Last reply
    0
    • T teknolog123

      hi, Whenever I start a instance of a form with frm.Show() from a backgroundworkder or a thread other than the mainform's thread like the sample below, the new form is unstable. but it's okay with frm.ShowDialog(). I want to use frm.show() because the code don't stop and wait for the user action. Any idea about it?

      //this way, the frm hangs
      private void bgworker_DoWork(object sender, DoWorkEventArgs e)
      {
      NewWindow frm=new NewWindow(); //this is not the main form
      frm.Show();
      }

      //this way it's okay
      private void bgworker_DoWork(object sender, DoWorkEventArgs e)
      {
      NewWindow frm=new NewWindow(); //this is not the main form
      frm.ShowDialog();
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I think your problem may be because using Form.Show() you have two threads running UI displays, and they are probably interfering with each other. Whereas ShowDialog() stops all activity until the dialog is dismissed by user interaction. You should put all your UI code (showing and manipulating forms) in your main thread and use the background thread(s) to do other work.

      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

      T 1 Reply Last reply
      0
      • L Lost User

        I think your problem may be because using Form.Show() you have two threads running UI displays, and they are probably interfering with each other. Whereas ShowDialog() stops all activity until the dialog is dismissed by user interaction. You should put all your UI code (showing and manipulating forms) in your main thread and use the background thread(s) to do other work.

        Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

        T Offline
        T Offline
        teknolog123
        wrote on last edited by
        #3

        thank you very much. At least, I got to know that all UI actions must be executed within the same thread.

        1 Reply Last reply
        0
        • T teknolog123

          hi, Whenever I start a instance of a form with frm.Show() from a backgroundworkder or a thread other than the mainform's thread like the sample below, the new form is unstable. but it's okay with frm.ShowDialog(). I want to use frm.show() because the code don't stop and wait for the user action. Any idea about it?

          //this way, the frm hangs
          private void bgworker_DoWork(object sender, DoWorkEventArgs e)
          {
          NewWindow frm=new NewWindow(); //this is not the main form
          frm.Show();
          }

          //this way it's okay
          private void bgworker_DoWork(object sender, DoWorkEventArgs e)
          {
          NewWindow frm=new NewWindow(); //this is not the main form
          frm.ShowDialog();
          }

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          You should create and access all GUI parts from the main thread; the ProgressChanged and RunWorkerCompleted handlers of a BGW do run on the main thread (provided the main thread was used to create the BGW!). So maybe you could create the one Form in the ProgressChanged handler. If not, look here[^] for a recommended way to get GUI things done from another thread. :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          1 Reply Last reply
          0
          • T teknolog123

            hi, Whenever I start a instance of a form with frm.Show() from a backgroundworkder or a thread other than the mainform's thread like the sample below, the new form is unstable. but it's okay with frm.ShowDialog(). I want to use frm.show() because the code don't stop and wait for the user action. Any idea about it?

            //this way, the frm hangs
            private void bgworker_DoWork(object sender, DoWorkEventArgs e)
            {
            NewWindow frm=new NewWindow(); //this is not the main form
            frm.Show();
            }

            //this way it's okay
            private void bgworker_DoWork(object sender, DoWorkEventArgs e)
            {
            NewWindow frm=new NewWindow(); //this is not the main form
            frm.ShowDialog();
            }

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            On a technical level, you need to marshal this into the UI thread, as the others said. (The reason it's hanging is because there is no message loop in that thread.) But if you find yourself doing that there's a good chance that your design is in need of revision; you shouldn't generally be doing direct updates of the UI from a worker thread anyway, even if it did work correctly. Almost by definition, a background thread is business logic, and you should always keep that separate from UI. A better solution is to have your background worker signal events (either the built in progress event or new events that you define), and have the UI hook onto them and update itself. Note that these events are dispatched in the context of the background thread so you need to use Invoke or BeginInvoke in the handlers to update the UI.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups