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. WPF
  4. Opening new WPF form in seperate thread.

Opening new WPF form in seperate thread.

Scheduled Pinned Locked Moved WPF
csharpwpfdesignbusinesshelp
5 Posts 3 Posters 2 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.
  • H Offline
    H Offline
    Hema Bairavan
    wrote on last edited by
    #1

    Dear All, Good day. Need your help on threading. I want to use a wpf window as progress window across my project. On the main UI thread while doing the business logics i need to open a new wpf window in separate thread as progress window and once the Business logic is done i want to close the same. I googled and found couple of examples to open a wpf window in separate thread and with new STA.. but am not able to find how to close the same window once done. My progress window doesn't allow the user to close it. global variable progresswindow _pw = new progresswindow(); ShowProgress() { _pw.Show(); } HideProgress() { _pw.Hide(); } MainThreadBusinessLogic() { LogicStarts... ShowProgress(); LogicEnds.. HideProgress(); } This is wat i wanted to do. But as the main thread is very busy it wont show the progress window i know. I used the background worker and showed the progress bar.. But the thing is i have different business logic which will work based on the users clicks.. so i may need to create different workers for handling those. If i do something like above mentioned window as progress window i can commonly use the same for all places. Hence request your help here my friends. thanks in advance.

    S 1 Reply Last reply
    0
    • H Hema Bairavan

      Dear All, Good day. Need your help on threading. I want to use a wpf window as progress window across my project. On the main UI thread while doing the business logics i need to open a new wpf window in separate thread as progress window and once the Business logic is done i want to close the same. I googled and found couple of examples to open a wpf window in separate thread and with new STA.. but am not able to find how to close the same window once done. My progress window doesn't allow the user to close it. global variable progresswindow _pw = new progresswindow(); ShowProgress() { _pw.Show(); } HideProgress() { _pw.Hide(); } MainThreadBusinessLogic() { LogicStarts... ShowProgress(); LogicEnds.. HideProgress(); } This is wat i wanted to do. But as the main thread is very busy it wont show the progress window i know. I used the background worker and showed the progress bar.. But the thing is i have different business logic which will work based on the users clicks.. so i may need to create different workers for handling those. If i do something like above mentioned window as progress window i can commonly use the same for all places. Hence request your help here my friends. thanks in advance.

      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      You are doing it all backwards. All UI lives on the main thread (including the progress window) All business logic lives on a background thread When you need to update the progress dialog from the background thread, use Dispatcher.Invoke.

      H 1 Reply Last reply
      0
      • S SledgeHammer01

        You are doing it all backwards. All UI lives on the main thread (including the progress window) All business logic lives on a background thread When you need to update the progress dialog from the background thread, use Dispatcher.Invoke.

        H Offline
        H Offline
        Hema Bairavan
        wrote on last edited by
        #3

        Hi Sledge, Thanks for your reply. And you are right i am doing in the back ward only. But the thing is, I have more the 20 forms in my application which have different functionality. And even in single form i have more than 5 buttons which will do different business logic's. So that is reason i wanted to do this approach, other wise i need to create separate background workers for all buttons or single background worker with different parameters.. which is more messy. So i planned to have a separate progress window which can be accessed across all the forms. But as we know both are in same thread causing issue. i planned to open and use the new window in different thread. Expecting your reply thanks in advance.

        S J 2 Replies Last reply
        0
        • H Hema Bairavan

          Hi Sledge, Thanks for your reply. And you are right i am doing in the back ward only. But the thing is, I have more the 20 forms in my application which have different functionality. And even in single form i have more than 5 buttons which will do different business logic's. So that is reason i wanted to do this approach, other wise i need to create separate background workers for all buttons or single background worker with different parameters.. which is more messy. So i planned to have a separate progress window which can be accessed across all the forms. But as we know both are in same thread causing issue. i planned to open and use the new window in different thread. Expecting your reply thanks in advance.

          S Offline
          S Offline
          SledgeHammer01
          wrote on last edited by
          #4

          Hema Bairavan wrote:

          which is more messy

          LOL... no, it's not. The way you have it now won't even work. The main UI will be completely unresponsive, have painting issues, etc. You can use async functions, lamba expressions, etc. Bottom line is, you'll have to move your business logic to the background thread in order to make your app work.

          1 Reply Last reply
          0
          • H Hema Bairavan

            Hi Sledge, Thanks for your reply. And you are right i am doing in the back ward only. But the thing is, I have more the 20 forms in my application which have different functionality. And even in single form i have more than 5 buttons which will do different business logic's. So that is reason i wanted to do this approach, other wise i need to create separate background workers for all buttons or single background worker with different parameters.. which is more messy. So i planned to have a separate progress window which can be accessed across all the forms. But as we know both are in same thread causing issue. i planned to open and use the new window in different thread. Expecting your reply thanks in advance.

            J Offline
            J Offline
            Jason Gleim
            wrote on last edited by
            #5

            Try looking at the BackgroundWorker class. It provides an encapsulation for a worker thread that supports passing updates to the UI taking care of all the dirty cross-thread marshaling. It is very easy to use and supports aborting as well. As for your progress window, you need to raise it when the first background thread is started and close it when the last background thread ends. You may want to consider creating the form at app startup then setting all of the thread progress notifications to call a notify update method on the form. The form could then determine if it needs to show/hide and which thread(s) progress to show. Again, if you use a backgroundworker, there is a thread complete event which fires when the thread is done making it easy to do something "on the way out the door". The progress window should be event-driven... only responding to events raised by the worker threads.

            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