Handling events of threads from a different class's object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thiago777
    New Member
    • May 2007
    • 89

    Handling events of threads from a different class's object

    Question details: VB .NET / threads / events / GUI

    Imagine the following situation:

    A method from object "A" creates "n" threads.

    Variables from these threads contains values that should be updated to a form (only when this form exists), one form can be shown/created for each thread. Thought the forms might not exist, the threads will be always running.

    I dont want to control the components (eg. labels) of the form from the threads because if I do so, they must exist otherwise we get an NullReferenceEx ception.. and also dont want to create them all and leave them "invisible" because this would be a waste of memory (imagine if we have 20 forms). Because of this (we dont know when the form will be created), we cant invoke its components also.

    My idea was to create an "in-the-middle" object with all variables (that must be updated in the form) being changed from the threads , and make this object raise an event when there is a change, so that an event handler in the form's code will update these values to the form's labels/controls. What do think? so maybe I wouldnt need the threads handling all the controls directly.

    The forms are in a separate class, then how do you create an event handler of these threads that you dont even know the names (because it was instanciated in another class)? or how do you manipulate this object from outside (once we dont have the reference to it there)?

    As Microsoft says:
    A delegate is an object that you can use to call a method of another object
    .. but what about getting variables or thread's variables?

    thank you for your attention.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    To be honest, you asked, I think that this is a poor design. I wonder if your thread is going to chew up more memory then serving up the controls as the forms are requested. What thread are you going to keep running? I dont believe that this was how threads were intended to be used.

    Comment

    • thiago777
      New Member
      • May 2007
      • 89

      #3
      Originally posted by kenobewan
      To be honest, you asked, I think that this is a poor design. I wonder if your thread is going to chew up more memory then serving up the controls as the forms are requested. What thread are you going to keep running? I dont believe that this was how threads were intended to be used.
      that was a better response that I was expecting =)

      Im aware of this, but I dont find a good way to keep my forms updated in "real time" when my processes are running as threads, and especially when I dont know the exact number of threads that will be necessary (they will be created according to a configuration file), just as the forms should only "consume memory" when a user requests its info about the running threads.

      Probably this is due my lack of experience on programming, I would be glad if you could help me in some way.

      thank you,
      Thiago.

      Comment

      • thiago777
        New Member
        • May 2007
        • 89

        #4
        Originally posted by kenobewan
        I dont believe that this was how threads were intended to be used.
        How do you believe it is then?

        Comment

        • Shashi Sadasivan
          Recognized Expert Top Contributor
          • Aug 2007
          • 1435

          #5
          I wouldnt use threads...
          this is how i interpret your situation...
          forexample you have 3 forms open. (f1,f2,f3)
          values in f1 is changed, and you want to reload f2 and f3.

          If this is the situation....I would approach it in a different way
          1. all forms cal only have one instance open...So create a static class with all the forms as static variables, and if ther set to null, open/instantiate them (if required)
          2. Create some public events which are to fired in each othe forms to be updated.
          3. create public methods in the static class which will act as a trigger to update the forms
          4. when the value in f1 changes, use the method in the static (trigger of step 3) class to update the forms.

          Thats how I have done some of my applications. works well.
          Last edited by Shashi Sadasivan; Sep 3 '07, 11:20 PM. Reason: typo, de-abbriviating chat english

          Comment

          • thiago777
            New Member
            • May 2007
            • 89

            #6
            The problem is not the form running as a thread, but the interaction between my threads AND the forms.

            Comment

            • Shashi Sadasivan
              Recognized Expert Top Contributor
              • Aug 2007
              • 1435

              #7
              Originally posted by thiago777
              The problem is not the form running as a thread, but the interaction between my threads AND the forms.
              Yes, so if your static class stores all the instances of your forms, your thresds or any other object can access it !!!

              Comment

              • thiago777
                New Member
                • May 2007
                • 89

                #8
                Originally posted by Shashi Sadasivan
                Yes, so if your static class stores all the instances of your forms, your thresds or any other object can access it !!!
                I cant store all instances of my forms, once they should be created dynamically (must discover how many will be necessary during run-time)

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  I suggest you spend time reading about delegates *again* and then try your design again.

                  Comment

                  • thiago777
                    New Member
                    • May 2007
                    • 89

                    #10
                    Originally posted by r035198x
                    I suggest you spend time reading about delegates *again* and then try your design again.
                    Im trying a simpler way with event driven GUI's.

                    What I have is an object that raises some events. Then I'm trying to get these events from the form like this:

                    1-On the form's class constructor get a reference of the object.
                    2-Create some AddHandlers for these events
                    3-Create the methods that will be called from the event handler's to update the form's components info.

                    This is the code of the form:

                    Code:
                    Public Class ThreadsOverview
                       'Class responsible for handling events 
                       'from referenced object and displaying
                       'given info in the this form
                       Public ObjectReference As Object
                    
                       Private Sub New(ByRef ConnObjRef As Object)
                    
                          ' Dieser Aufruf ist für den Windows Form-Designer erforderlich.
                          InitializeComponent()
                    
                          ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
                    
                          ObjectReference= ConnObjRef
                    
                       End Sub
                    
                       Private Sub ThreadsOverview_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                          If Me.Visible = True Then
                             AddHandler ObjectReference.E_ConnStatus(sts as boolean) , AddressOf Me.ConnStatusChange
                    
                          End If
                       End Sub
                    End Class
                    The "E_ConnStat us" is the name of the event from the object.

                    The problem is Im stuck in the first step I described, this is the code giving the error:

                    Code:
                    AddHandler ObjectReference.E_ConnStatus(sts as boolean) , AddressOf Me.ConnStatusChange
                    Of course, the error is : "The AddHandler or statement event operand RemoveHandler must be a point-qualified expression or a simple name." ,
                    because it doesnt know the object. I never worked with "as Object" before, so Im not sure if this is the way to go.

                    How to fix this?

                    Comment

                    Working...