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. Windows Forms
  4. Loading controls with a BackgroundWorker

Loading controls with a BackgroundWorker

Scheduled Pinned Locked Moved Windows Forms
csharptutorialdesignhelpquestion
6 Posts 3 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.
  • J Offline
    J Offline
    james nugent
    wrote on last edited by
    #1

    Hi, I'm working with C# on .NET 3.5SP1. What I'm trying to achieve is as follows: I have a form (frmShell) which can "spawn" other forms. Each of the other forms (frmMyFormType, for example), has a main content panel, into which I add a control of an appropriate type (cntMyFormTypeView, for example). Whilst the control is loading, I display a loading panel (cntLoader in my case). In the constructor of each form I create a loading panel, and add it to the Controls of the content panel like this: var Loader = new cntLoader { Dock = DockStyle.Fill }; contentPanel.Controls.Add(Loader); In the Form_Load event, I then start a background worker, whose DoWork method looks something like this: e.Result = Invoke(new CreateViewInvoker(CreateView), parameters.ObjectIdentifier); Where CreateViewInvoker is defined with the following: private delegate cntMyFormTypeView CreateViewInvoker(long itemId); And the CreateView method is as follows: private cntMyFormTypeView CreateView(long itemId) { return new cntMyFormTypeView(this, itemId) { Dock = DockStyle.Fill }; } In the Completed method of the background worker, I do the following: var control = (Control)e.Result; contentPanel.Controls.Clear(); contentPanel.Controls.Add(control); contentPanel.Invalidate(); The problem: this works, but freezes the UI when I Invoke the CreateView method (the constructor of cntMyFormTypeView can take a long time to run). This does make sense since it will be invoked on the UI thread. Does anyone have any suggestions as to how to do this differently such that I create a control in the background (without locking up my UI), but then still use it from my UI thread? Thanks, James

    F 1 Reply Last reply
    0
    • J james nugent

      Hi, I'm working with C# on .NET 3.5SP1. What I'm trying to achieve is as follows: I have a form (frmShell) which can "spawn" other forms. Each of the other forms (frmMyFormType, for example), has a main content panel, into which I add a control of an appropriate type (cntMyFormTypeView, for example). Whilst the control is loading, I display a loading panel (cntLoader in my case). In the constructor of each form I create a loading panel, and add it to the Controls of the content panel like this: var Loader = new cntLoader { Dock = DockStyle.Fill }; contentPanel.Controls.Add(Loader); In the Form_Load event, I then start a background worker, whose DoWork method looks something like this: e.Result = Invoke(new CreateViewInvoker(CreateView), parameters.ObjectIdentifier); Where CreateViewInvoker is defined with the following: private delegate cntMyFormTypeView CreateViewInvoker(long itemId); And the CreateView method is as follows: private cntMyFormTypeView CreateView(long itemId) { return new cntMyFormTypeView(this, itemId) { Dock = DockStyle.Fill }; } In the Completed method of the background worker, I do the following: var control = (Control)e.Result; contentPanel.Controls.Clear(); contentPanel.Controls.Add(control); contentPanel.Invalidate(); The problem: this works, but freezes the UI when I Invoke the CreateView method (the constructor of cntMyFormTypeView can take a long time to run). This does make sense since it will be invoked on the UI thread. Does anyone have any suggestions as to how to do this differently such that I create a control in the background (without locking up my UI), but then still use it from my UI thread? Thanks, James

      F Offline
      F Offline
      Frank Horn
      wrote on last edited by
      #2

      I think it's pest or cholera there. Either you create your control on the UI thread which then freezes while the control is being loaded, or you start a second thread with windows message queue and all for the control, but then you'll have to communicate with it via (Begin)Invoke, with all the overhead this requires.

      J J 2 Replies Last reply
      0
      • F Frank Horn

        I think it's pest or cholera there. Either you create your control on the UI thread which then freezes while the control is being loaded, or you start a second thread with windows message queue and all for the control, but then you'll have to communicate with it via (Begin)Invoke, with all the overhead this requires.

        J Offline
        J Offline
        james nugent
        wrote on last edited by
        #3

        Thanks for the reply. Do you have any pointers for documentation/examples on your second suggestion? There isn't too much communication between the form and its control, so the overhead might be bearable. Thanks, James

        F 1 Reply Last reply
        0
        • J james nugent

          Thanks for the reply. Do you have any pointers for documentation/examples on your second suggestion? There isn't too much communication between the form and its control, so the overhead might be bearable. Thanks, James

          F Offline
          F Offline
          Frank Horn
          wrote on last edited by
          #4

          Sorry, I never did this and only vaguely remember having read about it. Anyway, why do you need to load a control in the background? Usually loading a control doesnt't take so long that another thread makes sense. If it's because of many data or a huge image, you could load those in a (non-UI) worker thread.

          J 1 Reply Last reply
          0
          • F Frank Horn

            Sorry, I never did this and only vaguely remember having read about it. Anyway, why do you need to load a control in the background? Usually loading a control doesnt't take so long that another thread makes sense. If it's because of many data or a huge image, you could load those in a (non-UI) worker thread.

            J Offline
            J Offline
            james nugent
            wrote on last edited by
            #5

            It has several large-footprint controls on it(5 x DevExpress XtraGrids, an XtraLayoutControl, an XtraScheduler etc). They are all on tabs though, so I may try lazy loading on tab changes. I'm already using a background worker for making a service call to get data. Thanks for the replies, James

            1 Reply Last reply
            0
            • F Frank Horn

              I think it's pest or cholera there. Either you create your control on the UI thread which then freezes while the control is being loaded, or you start a second thread with windows message queue and all for the control, but then you'll have to communicate with it via (Begin)Invoke, with all the overhead this requires.

              J Offline
              J Offline
              Jon Hulatt
              wrote on last edited by
              #6

              You're not going to gain anything there. The framework marshals BeginInvoke calls to the correct thread by posting messages; in this instance having the extra thread actually creates more work.

              using System.Beer;

              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