VB.net Multithreading Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thowle

    #1

    VB.net Multithreading Problem

    Okay, I'm sure there is a very simple solution to my problems here and I am
    just too tired, but then again maybe not.

    Here is an example of my code:
    Dim Conn_Thrd as new system.threadin g.thread(addres sof connecttoserver ())

    public sub connecttoserver ()
    'This works good
    Treeview1.nodes .add("Test Node")

    'This doesnt
    clrnd()

    'Neither does this
    me.treeview1.in voke(clrnd)
    end sub

    'This ONLY works good when it is called inside the thread containing the
    treeview1 control.
    public function clrnd()
    Treeview1.nodes .clear()
    end sub


    'This works good
    Private sub Button1_click (...) handles button1.click
    clrnd()
    end sub

    'This works good
    Private sub Button2_click (...) handles button2.click
    Conn_Thrd = new system.threadin g.thread(addres sof connecttoserver ())
    Conn_Thrd.start ()
    end sub


    Please help :)

    Thanks,
    Travis
  • Calvin Luttrell

    #2
    Re: VB.net Multithreading Problem

    Travis,

    You need to setup a Delegate then call the delegate function from your invoke. You're on the right track for sure.

    look at:





    -Calvin Luttrell
    Projectthunder. com, Inc.


    "thowle" <thowle@brtc.ne t> wrote in message news:D8F272FA-7993-4217-9F1C-B89DC76AA9F1@mi crosoft.com...[color=blue]
    > Okay, I'm sure there is a very simple solution to my problems here and I am
    > just too tired, but then again maybe not.
    >
    > Here is an example of my code:
    > Dim Conn_Thrd as new system.threadin g.thread(addres sof connecttoserver ())
    >
    > public sub connecttoserver ()
    > 'This works good
    > Treeview1.nodes .add("Test Node")
    >
    > 'This doesnt
    > clrnd()
    >
    > 'Neither does this
    > me.treeview1.in voke(clrnd)
    > end sub
    >
    > 'This ONLY works good when it is called inside the thread containing the
    > treeview1 control.
    > public function clrnd()
    > Treeview1.nodes .clear()
    > end sub
    >
    >
    > 'This works good
    > Private sub Button1_click (...) handles button1.click
    > clrnd()
    > end sub
    >
    > 'This works good
    > Private sub Button2_click (...) handles button2.click
    > Conn_Thrd = new system.threadin g.thread(addres sof connecttoserver ())
    > Conn_Thrd.start ()
    > end sub
    >
    >
    > Please help :)
    >
    > Thanks,
    > Travis[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: VB.net Multithreading Problem

      thowle <thowle@brtc.ne t> wrote:[color=blue]
      > Okay, I'm sure there is a very simple solution to my problems here and I am
      > just too tired, but then again maybe not.[/color]

      See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

      Note that even when it *was* working for you, there's no guarantee
      about it - you really shouldn't touch the UI at all from a non-UI
      thread.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • thowle

        #4
        RE: VB.net Multithreading Problem

        Okay, here is what I've done now. I created a class called "ConnectToServe r"

        in that class, it will fire off an event called "ConnectComplet e"

        In the mainform that contains GUI I have the sub to handle the event, and I
        have it calling a function "GetServerList" . However, it is still calling it
        from inside the thread inwhich does not contain the GUI therefore it fails,
        EVEN if i have a treeview1.invok e(GetServerList ()).

        How can I get this to work?

        "thowle" wrote:
        [color=blue]
        > Okay, I'm sure there is a very simple solution to my problems here and I am
        > just too tired, but then again maybe not.
        >
        > Here is an example of my code:
        > Dim Conn_Thrd as new system.threadin g.thread(addres sof connecttoserver ())
        >
        > public sub connecttoserver ()
        > 'This works good
        > Treeview1.nodes .add("Test Node")
        >
        > 'This doesnt
        > clrnd()
        >
        > 'Neither does this
        > me.treeview1.in voke(clrnd)
        > end sub
        >
        > 'This ONLY works good when it is called inside the thread containing the
        > treeview1 control.
        > public function clrnd()
        > Treeview1.nodes .clear()
        > end sub
        >
        >
        > 'This works good
        > Private sub Button1_click (...) handles button1.click
        > clrnd()
        > end sub
        >
        > 'This works good
        > Private sub Button2_click (...) handles button2.click
        > Conn_Thrd = new system.threadin g.thread(addres sof connecttoserver ())
        > Conn_Thrd.start ()
        > end sub
        >
        >
        > Please help :)
        >
        > Thanks,
        > Travis[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          RE: VB.net Multithreading Problem

          thowle <thowle@brtc.ne t> wrote:[color=blue]
          > Okay, here is what I've done now. I created a class called "ConnectToServe r"
          >
          > in that class, it will fire off an event called "ConnectComplet e"
          >
          > In the mainform that contains GUI I have the sub to handle the event, and I
          > have it calling a function "GetServerList" . However, it is still calling it
          > from inside the thread inwhich does not contain the GUI therefore it fails,
          > EVEN if i have a treeview1.invok e(GetServerList ()).
          >
          > How can I get this to work?[/color]

          You need to create a delegate from GetServerList, not call it directly.

          Could you post a short but complete program which demonstrates the
          problem?

          See http://www.pobox.com/~skeet/csharp/complete.html for details of
          what I mean by that.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • thowle

            #6
            RE: VB.net Multithreading Problem

            Bacically, here is the concept of my application in a smaller form.
            (Not all of it is complete)

            private sub Button1_click(. ..) handles button1.click
            AddHandler Connect.Connect ed, AddressOf Connected

            Dim ConnectThread as new system.threadin g.thread(addres s of Connect)
            ConnectThread.s tart()
            end sub

            Sub Connected()
            treeview1.invok e(GetServerList ())
            End sub

            Public function GetServerList()
            ....access database....
            treeview1.nodes .add("Value")
            ....close recordset, and db conn....
            end function



            Public Class Connect
            Public Event Connected()
            ....connect to server....
            ....yay, we are connected :)....
            RaiseEvent Connected()
            End class

            Thats about it.




            "Jon Skeet [C# MVP]" wrote:
            [color=blue]
            > thowle <thowle@brtc.ne t> wrote:[color=green]
            > > Okay, here is what I've done now. I created a class called "ConnectToServe r"
            > >
            > > in that class, it will fire off an event called "ConnectComplet e"
            > >
            > > In the mainform that contains GUI I have the sub to handle the event, and I
            > > have it calling a function "GetServerList" . However, it is still calling it
            > > from inside the thread inwhich does not contain the GUI therefore it fails,
            > > EVEN if i have a treeview1.invok e(GetServerList ()).
            > >
            > > How can I get this to work?[/color]
            >
            > You need to create a delegate from GetServerList, not call it directly.
            >
            > Could you post a short but complete program which demonstrates the
            > problem?
            >
            > See http://www.pobox.com/~skeet/csharp/complete.html for details of
            > what I mean by that.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too
            >[/color]

            Comment

            • thowle

              #7
              RE: VB.net Multithreading Problem

              Okay I got it working. and it was pretty wild :)

              As Jon Skeet said, you must make a delegate for the GetServerList() function
              as follows:

              Delegate Function GetServerTView( )
              Public ReadOnly fnGetServerTVie w As GetServerTView

              then:

              Public Sub New()
              fnGetServerTVie w = New GetServerTView( AddressOf Me.GetServerLis t)
              end Sub

              from here, you can call this from any thread as
              treeview1.invok e(fnGetServerTV iew)!


              Thx for help yall :)

              "thowle" wrote:
              [color=blue]
              > Bacically, here is the concept of my application in a smaller form.
              > (Not all of it is complete)
              >
              > private sub Button1_click(. ..) handles button1.click
              > AddHandler Connect.Connect ed, AddressOf Connected
              >
              > Dim ConnectThread as new system.threadin g.thread(addres s of Connect)
              > ConnectThread.s tart()
              > end sub
              >
              > Sub Connected()
              > treeview1.invok e(GetServerList ())
              > End sub
              >
              > Public function GetServerList()
              > ....access database....
              > treeview1.nodes .add("Value")
              > ....close recordset, and db conn....
              > end function
              >
              >
              >
              > Public Class Connect
              > Public Event Connected()
              > ....connect to server....
              > ....yay, we are connected :)....
              > RaiseEvent Connected()
              > End class
              >
              > Thats about it.
              >
              >
              >
              >
              > "Jon Skeet [C# MVP]" wrote:
              >[color=green]
              > > thowle <thowle@brtc.ne t> wrote:[color=darkred]
              > > > Okay, here is what I've done now. I created a class called "ConnectToServe r"
              > > >
              > > > in that class, it will fire off an event called "ConnectComplet e"
              > > >
              > > > In the mainform that contains GUI I have the sub to handle the event, and I
              > > > have it calling a function "GetServerList" . However, it is still calling it
              > > > from inside the thread inwhich does not contain the GUI therefore it fails,
              > > > EVEN if i have a treeview1.invok e(GetServerList ()).
              > > >
              > > > How can I get this to work?[/color]
              > >
              > > You need to create a delegate from GetServerList, not call it directly.
              > >
              > > Could you post a short but complete program which demonstrates the
              > > problem?
              > >
              > > See http://www.pobox.com/~skeet/csharp/complete.html for details of
              > > what I mean by that.
              > >
              > > --
              > > Jon Skeet - <skeet@pobox.co m>
              > > http://www.pobox.com/~skeet
              > > If replying to the group, please do not mail me too
              > >[/color][/color]

              Comment

              Working...