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. Connect controls to thread

Connect controls to thread

Scheduled Pinned Locked Moved Windows Forms
helpquestionalgorithmsdiscussion
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.
  • M Offline
    M Offline
    Moshe T
    wrote on last edited by
    #1

    Hello, I can't find best practice to my issue, I will appreciate any help from you: In my scenario there is WinForm with few RadioButtons and Button. the user choose one of the RadioButton and then press on the Button. When the user click there are two things: 1. Start thread for searching... 2. Create new TextBox for display the results from the thread I just started. How can I "connect" the TextBox with the thread, so when I got the results from the thread I will know where to display the results ? Please remember the user can click one after another on the Button and each click will open new TextBox, so each thread must display its results on "its" TextBox. Thanks in advance, Moshet

    L D 2 Replies Last reply
    0
    • M Moshe T

      Hello, I can't find best practice to my issue, I will appreciate any help from you: In my scenario there is WinForm with few RadioButtons and Button. the user choose one of the RadioButton and then press on the Button. When the user click there are two things: 1. Start thread for searching... 2. Create new TextBox for display the results from the thread I just started. How can I "connect" the TextBox with the thread, so when I got the results from the thread I will know where to display the results ? Please remember the user can click one after another on the Button and each click will open new TextBox, so each thread must display its results on "its" TextBox. Thanks in advance, Moshet

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

      You could create a little class that represents one search job. It would hold a reference to the textbox, create its own thread, execute the necessary search in the background, and take care of displaying the results. You then would instantiate said class each time the user pushes the button. Warning: you should create and manipulate Controls only with code running on the main thread. See this article[^]. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      M 1 Reply Last reply
      0
      • L Luc Pattyn

        You could create a little class that represents one search job. It would hold a reference to the textbox, create its own thread, execute the necessary search in the background, and take care of displaying the results. You then would instantiate said class each time the user pushes the button. Warning: you should create and manipulate Controls only with code running on the main thread. See this article[^]. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        M Offline
        M Offline
        Moshe T
        wrote on last edited by
        #3

        Thank you Luc. In my application I have 2 layers (I try to keep this way as possible as I can): 1.Bussiness Objects - the search engine (objects) 2.Presentation - forms. Your suggestion will mix-up those layers. As I know - in object oriented you should at least save those layers clear one from each other. I will be glad to get your opinion about it. Best Regards.

        L 1 Reply Last reply
        0
        • M Moshe T

          Thank you Luc. In my application I have 2 layers (I try to keep this way as possible as I can): 1.Bussiness Objects - the search engine (objects) 2.Presentation - forms. Your suggestion will mix-up those layers. As I know - in object oriented you should at least save those layers clear one from each other. I will be glad to get your opinion about it. Best Regards.

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

          Moshe T wrote:

          Your suggestion will mix-up those layers

          Huh? I agree with having those layers, however they need to exchange your information. An ECG is an object (belonging to your "Business Objects"), even if you don't look at it; and it does not care how it gets viewed, so passing GUI stuff to it would be wrong. A Form or a Control for viewing an ECG is a specialized object (part of your presentation layer), it needs to get ECG data, so either let it accept an ECG, or, maybe better, some interface that suffices for its purposes. In the latter case, the ECG object must provide that interface, and the coupling between both layers is more relaxed as the Form/Control doesn't require an actual ECG object any more. Unless I'm mistaken, that is what I meant to say, and said earlier. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          M 1 Reply Last reply
          0
          • L Luc Pattyn

            Moshe T wrote:

            Your suggestion will mix-up those layers

            Huh? I agree with having those layers, however they need to exchange your information. An ECG is an object (belonging to your "Business Objects"), even if you don't look at it; and it does not care how it gets viewed, so passing GUI stuff to it would be wrong. A Form or a Control for viewing an ECG is a specialized object (part of your presentation layer), it needs to get ECG data, so either let it accept an ECG, or, maybe better, some interface that suffices for its purposes. In the latter case, the ECG object must provide that interface, and the coupling between both layers is more relaxed as the Form/Control doesn't require an actual ECG object any more. Unless I'm mistaken, that is what I meant to say, and said earlier. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            M Offline
            M Offline
            Moshe T
            wrote on last edited by
            #5

            Well, your explanation is quite similar what I have here ! I thought there are best practice for this issue, or build-in solution in the .NET Framework. (I was searching information about Binding data, but my scenario doesn't fit for that). Thank you very much for your assistance Best Regards.

            1 Reply Last reply
            0
            • M Moshe T

              Hello, I can't find best practice to my issue, I will appreciate any help from you: In my scenario there is WinForm with few RadioButtons and Button. the user choose one of the RadioButton and then press on the Button. When the user click there are two things: 1. Start thread for searching... 2. Create new TextBox for display the results from the thread I just started. How can I "connect" the TextBox with the thread, so when I got the results from the thread I will know where to display the results ? Please remember the user can click one after another on the Button and each click will open new TextBox, so each thread must display its results on "its" TextBox. Thanks in advance, Moshet

              D Offline
              D Offline
              dybs
              wrote on last edited by
              #6

              BackgroundWorker.RunWorkerCompleted[^] should work for you. The RunWorkerCompleted event will fire on the same thread the BackgroundWorker was started from (typically the main GUI thread). You'll need to get your search results from the DoWork event into the RunWorkerCompleted event, and you should have access to your TextBox. Dybs

              The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

              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