DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 28

Thread: VB threads

  1. #1
    wally Guest

    VB threads


    Does anyone have an example running using threads in vb.net? I would like
    to see a complete example so that I can understand what is going on with
    the code that I have running.

    Wally

  2. #2
    Govind_kanshi Guest

    Re: VB threads

    A very simple sample,
    A thread executes a function while the main thread listens for keyboard
    input and quits when you enter q and hot Enter.

    Systems.Threading provides the base Thread class.

    The way thread is assigned what to execute is provided by AddressOf operator
    which gets the function pointer from the function signature.

    '----------------------------------------------------------
    Imports System
    Imports System.Threading

    Namespace Sample
    Module TestThread
    private m_counter as Integer
    private MAXINT as Integer = 10000

    Sub DoCount()
    While(1)
    Console.WriteLine(m_counter)
    m_counter = m_counter + 1

    if m_counter = MAXINT then
    m_counter = 1
    End if
    End While
    End Sub

    Shared Sub Main()

    Dim i as Integer
    Dim tThread as Thread
    Dim c as Char

    c = CChar("c")


    tThread = new Thread(AddressOf DoCount)


    tThread.Start
    While ( Not (c = CChar("q")) )
    i = Console.Read()
    c = CChar(i)

    End While
    tThread.Stop
    End Sub
    End Module
    End Namespace
    '----------------------------------------------------





    "wally" <wallym@mindspring.com> wrote in message
    news:398b0abe$1@news.devx.com...
    >
    > Does anyone have an example running using threads in vb.net? I would like
    > to see a complete example so that I can understand what is going on with
    > the code that I have running.
    >
    > Wally




  3. #3
    Thomas Eyde Guest

    Re: VB threads

    This is a joke, right? Or..?

    I thought the AddressOf operator worked only on methods in a .bas/modeule
    and not on those in a class. Or has that changed in VB.Net?

    This mechanics is slightly better than the good old timer approach, but not
    very OO.

    Please tell me the Thread object supports classes and use one or both kinds
    of inheritance to achieve it

    --
    /Thomas
    thomas.eyde@eunet.no


    Govind_kanshi <Govind_kanshi@email.msn.com> wrote in message
    news:398d1833@news.devx.com...
    > A very simple sample,
    > A thread executes a function while the main thread listens for keyboard
    > input and quits when you enter q and hot Enter.
    >
    > Systems.Threading provides the base Thread class.
    >
    > The way thread is assigned what to execute is provided by AddressOf

    operator
    > which gets the function pointer from the function signature.
    >
    > '----------------------------------------------------------
    > Imports System
    > Imports System.Threading
    >
    > Namespace Sample
    > Module TestThread
    > private m_counter as Integer
    > private MAXINT as Integer = 10000
    >
    > Sub DoCount()
    > While(1)
    > Console.WriteLine(m_counter)
    > m_counter = m_counter + 1
    >
    > if m_counter = MAXINT then
    > m_counter = 1
    > End if
    > End While
    > End Sub
    >
    > Shared Sub Main()
    >
    > Dim i as Integer
    > Dim tThread as Thread
    > Dim c as Char
    >
    > c = CChar("c")
    >
    >
    > tThread = new Thread(AddressOf DoCount)
    >
    >
    > tThread.Start
    > While ( Not (c = CChar("q")) )
    > i = Console.Read()
    > c = CChar(i)
    >
    > End While
    > tThread.Stop
    > End Sub
    > End Module
    > End Namespace
    > '----------------------------------------------------
    >
    >
    >
    >
    >
    > "wally" <wallym@mindspring.com> wrote in message
    > news:398b0abe$1@news.devx.com...
    > >
    > > Does anyone have an example running using threads in vb.net? I would

    like
    > > to see a complete example so that I can understand what is going on with
    > > the code that I have running.
    > >
    > > Wally

    >
    >




  4. #4
    Govind_kanshi Guest

    Re: VB threads

    I think you are talking of the "runnable" Interface ? Well I was trying
    to find that too without much success.

    The AddressOf method can get the offset to the classes too, so this method
    can be get a ref to fn in a class theoratically (I have not tested that)

    from docs
    "
    Creating a new instance of a System.Threading.Thread object can create new
    managed threads. The constructor for System.Threading.Thread takes, as its
    only parameter, a Thread Delegate.

    A delegate is an object that acts as reference to a method on another class.
    A thread delegate is a special type of delegate that references a method
    that will be executed by a new thread.

    "


    "Thomas Eyde" <thomas.eyde@eunet.no> wrote in message
    news:398ddeba$1@news.devx.com...
    > This is a joke, right? Or..?
    >
    > I thought the AddressOf operator worked only on methods in a .bas/modeule
    > and not on those in a class. Or has that changed in VB.Net?
    >
    > This mechanics is slightly better than the good old timer approach, but

    not
    > very OO.
    >
    > Please tell me the Thread object supports classes and use one or both

    kinds
    > of inheritance to achieve it
    >
    > --
    > /Thomas
    > thomas.eyde@eunet.no
    >
    >
    > Govind_kanshi <Govind_kanshi@email.msn.com> wrote in message
    > news:398d1833@news.devx.com...
    > > A very simple sample,
    > > A thread executes a function while the main thread listens for keyboard
    > > input and quits when you enter q and hot Enter.
    > >
    > > Systems.Threading provides the base Thread class.
    > >
    > > The way thread is assigned what to execute is provided by AddressOf

    > operator
    > > which gets the function pointer from the function signature.
    > >
    > > '----------------------------------------------------------
    > > Imports System
    > > Imports System.Threading
    > >
    > > Namespace Sample
    > > Module TestThread
    > > private m_counter as Integer
    > > private MAXINT as Integer = 10000
    > >
    > > Sub DoCount()
    > > While(1)
    > > Console.WriteLine(m_counter)
    > > m_counter = m_counter + 1
    > >
    > > if m_counter = MAXINT then
    > > m_counter = 1
    > > End if
    > > End While
    > > End Sub
    > >
    > > Shared Sub Main()
    > >
    > > Dim i as Integer
    > > Dim tThread as Thread
    > > Dim c as Char
    > >
    > > c = CChar("c")
    > >
    > >
    > > tThread = new Thread(AddressOf DoCount)
    > >
    > >
    > > tThread.Start
    > > While ( Not (c = CChar("q")) )
    > > i = Console.Read()
    > > c = CChar(i)
    > >
    > > End While
    > > tThread.Stop
    > > End Sub
    > > End Module
    > > End Namespace
    > > '----------------------------------------------------
    > >
    > >
    > >
    > >
    > >
    > > "wally" <wallym@mindspring.com> wrote in message
    > > news:398b0abe$1@news.devx.com...
    > > >
    > > > Does anyone have an example running using threads in vb.net? I would

    > like
    > > > to see a complete example so that I can understand what is going on

    with
    > > > the code that I have running.
    > > >
    > > > Wally

    > >
    > >

    >
    >




  5. #5
    James Foxall Guest

    Re: VB threads


    "Thomas Eyde" <thomas.eyde@eunet.no> wrote:
    >This is a joke, right? Or..?
    >
    >I thought the AddressOf operator worked only on methods in a .bas/modeule
    >and not on those in a class. Or has that changed in VB.Net?


    You can use AddressOf in a class module now. Not only that, I was told (in
    a session at the PDC I thing) that you can now call functions without your
    own project using the address returned by AddressOf.

    James Foxall

  6. #6
    James Foxall Guest

    Re: VB threads


    >The AddressOf method can get the offset to the classes too, so this method
    >can be get a ref to fn in a class theoratically (I have not tested that)


    Actually, you'll see AddressOf used in classes a lot - it's used in the code
    that gets automatically generated to create a form at runtime to hook up
    event handlers. It's pretty cool stuff, at least to me.

    James


  7. #7
    Bill McCarthy Guest

    Re: VB threads


    "James Foxall" <jamesf@tigerpawsoftware.com> wrote in message
    news:398e1d28$1@news.devx.com...
    >
    > "Thomas Eyde" <thomas.eyde@eunet.no> wrote:
    > >This is a joke, right? Or..?
    > >
    > >I thought the AddressOf operator worked only on methods in a .bas/modeule
    > >and not on those in a class. Or has that changed in VB.Net?

    >
    > You can use AddressOf in a class module now. Not only that, I was told (in
    > a session at the PDC I thing) that you can now call functions without your
    > own project using the address returned by AddressOf.
    >


    Yeh, but it makes me wonder how the instance of the class is handled by the
    garbage collection, as I can't see the GC being able to know that an
    external dll has got a reference to a class's function. Hmm.. might have to
    do some crash testing on that one.. <g>



  8. #8
    Michael \(michka\) Kaplan Guest

    Re: VB threads

    (and for people who want to do it in VB6, methods now exist for that sort of
    thing <g>).

    --
    MichKa

    random junk of dubious value at the multilingual
    http://www.trigeminal.com/ and a new book on
    i18N in VB at http://www.trigeminal.com/michka.asp

    "James Foxall" <jamesf@tigerpawsoftware.com> wrote in message
    news:398e1e1f$1@news.devx.com...
    >
    > >The AddressOf method can get the offset to the classes too, so this

    method
    > >can be get a ref to fn in a class theoratically (I have not tested that)

    >
    > Actually, you'll see AddressOf used in classes a lot - it's used in the

    code
    > that gets automatically generated to create a form at runtime to hook up
    > event handlers. It's pretty cool stuff, at least to me.
    >
    > James
    >




  9. #9
    James Foxall Guest

    Re: VB threads


    >Yeh, but it makes me wonder how the instance of the class is handled by the

    garbage collection, as I can't see the GC being able to know that an external
    dll has got a reference to a class's function. Hmm.. might have to do some
    crash testing on that one.. <g>

    Wow, Bill, that is a thought. I haven't got that far anyway. <g> I had a
    bizzare deal happen to me today. I placed a splitter control on a form, but
    did not dock the control to an edge. For that matter, I don't believe it
    docked itself either. I then went into the code for the Win Form to dig about
    a bit. When I tried to switch back to view the interface of the frm, I got
    an error about the splitter control having to be docked to an edge, and that
    I couldn't view the form designer until I corrected this problem!!! As it
    turned out, I had no way to correct the problem (that I could come up with
    anyway) without viewing the Win Form in design view, since the code within
    the module is actually used to build the form at runtime. Circular problem
    and a New Project! Ouch!

    Another wierd thing, this one related to AddressOf. When you double-click
    a control you get the default event for the control (such as Click for a
    command button). .Net has been kind enough to add the code that hooks the
    control and uses AddressOf to point the hook to the new procdure. If you
    come back later and delete the procedure (not sure if it has to have code
    in it or not), Visual Basic gives you a compile error when you attempt to
    run the project, because it doesn't automatically delete the statement that
    performs the hook! The statement with AddressOf is left holding an invalid
    function reference, so your project won't compile.

    There sure is a lot more to watch out for, even when doing simple things
    compared to Visual Basic 6. But there sure is a LOT more functiolity.

    James Foxall

  10. #10
    James Foxall Guest

    Re: VB threads


    "Michael \(michka\) Kaplan" <former_mvp@spamfree.trigeminal.nospam.com> wrote:
    >(and for people who want to do it in VB6, methods now exist for that sort

    of
    >thing <g>).


    Like keeping track of hooked user-controls in an array or linked list within
    a Class module, or is there a better way? <Do tell! <g>>

    James Foxall

  11. #11
    Govind_kanshi Guest

    Re: VB threads

    Let us take a wild guess and wise listeners fill the holes or blow new ones
    ?

    Looking at the code generated you can see the ldftn/ldvirtftn is used to
    load the
    function pointer to the stack.
    Going through the assembler's guide, looks like ngwssdk supports the
    function pointers and they are not objects like the delegates ( actually if
    you see the docs they mention to use a delegate for threadstart. But
    obviously in VB that is not possible (?).

    So we have to instantiate the class containing our function and we can get
    the address of the function (which makes sense unless you have static
    methods)

    So the question is how would GC know who is using the one of the
    instance/static functions via a pointer.

    GC knows about the base class and it also knows the variable (thread) which
    points to an instance of function of class or a static member function.
    So that should be easy,.... I stand to be corrected. So its graph's node
    should go away once the thread does an exclusive stop.

    What I was not able to find is a daemon thread( IsBackground should be the
    one , but I am skeptic unless my sample works)

    regards
    Govind



    "Bill McCarthy" <Bill_McC@iprimus.com.au> wrote in message
    news:398e2cd8@news.devx.com...
    >
    > "James Foxall" <jamesf@tigerpawsoftware.com> wrote in message
    > news:398e1d28$1@news.devx.com...
    > >
    > > "Thomas Eyde" <thomas.eyde@eunet.no> wrote:
    > > >This is a joke, right? Or..?
    > > >
    > > >I thought the AddressOf operator worked only on methods in a

    ..bas/modeule
    > > >and not on those in a class. Or has that changed in VB.Net?

    > >
    > > You can use AddressOf in a class module now. Not only that, I was told

    (in
    > > a session at the PDC I thing) that you can now call functions without

    your
    > > own project using the address returned by AddressOf.
    > >

    >
    > Yeh, but it makes me wonder how the instance of the class is handled by

    the
    > garbage collection, as I can't see the GC being able to know that an
    > external dll has got a reference to a class's function. Hmm.. might have

    to
    > do some crash testing on that one.. <g>
    >
    >






  12. #12
    Michael \(michka\) Kaplan Guest

    Re: VB threads

    Is there a better way? Matt Curland's PushParamThunk is superior to the
    methods you have given. :-)

    --
    MichKa

    random junk of dubious value at the multilingual
    http://www.trigeminal.com/ and a new book on
    i18N in VB at http://www.trigeminal.com/michka.asp

    "James Foxall" <jamesf@tigerpawsoftware.com> wrote in message
    news:398e2ff8$1@news.devx.com...
    >
    > "Michael \(michka\) Kaplan" <former_mvp@spamfree.trigeminal.nospam.com>

    wrote:
    > >(and for people who want to do it in VB6, methods now exist for that sort

    > of
    > >thing <g>).

    >
    > Like keeping track of hooked user-controls in an array or linked list

    within
    > a Class module, or is there a better way? <Do tell! <g>>
    >
    > James Foxall




  13. #13
    Michael \(michka\) Kaplan Guest

    Re: VB threads

    I doubt that the second problem is reallysomething for them to fix.... any
    more than they would ever handle the case where you delete a function but
    not its callers.

    --
    MichKa

    random junk of dubious value at the multilingual
    http://www.trigeminal.com/ and a new book on
    i18N in VB at http://www.trigeminal.com/michka.asp

    "James Foxall" <jamesf@tigerpawsoftware.com> wrote in message
    news:398e2f7a$1@news.devx.com...
    >
    > >Yeh, but it makes me wonder how the instance of the class is handled by

    the
    > garbage collection, as I can't see the GC being able to know that an

    external
    > dll has got a reference to a class's function. Hmm.. might have to do some
    > crash testing on that one.. <g>
    >
    > Wow, Bill, that is a thought. I haven't got that far anyway. <g> I had a
    > bizzare deal happen to me today. I placed a splitter control on a form,

    but
    > did not dock the control to an edge. For that matter, I don't believe it
    > docked itself either. I then went into the code for the Win Form to dig

    about
    > a bit. When I tried to switch back to view the interface of the frm, I got
    > an error about the splitter control having to be docked to an edge, and

    that
    > I couldn't view the form designer until I corrected this problem!!! As it
    > turned out, I had no way to correct the problem (that I could come up with
    > anyway) without viewing the Win Form in design view, since the code within
    > the module is actually used to build the form at runtime. Circular problem
    > and a New Project! Ouch!
    >
    > Another wierd thing, this one related to AddressOf. When you double-click
    > a control you get the default event for the control (such as Click for a
    > command button). .Net has been kind enough to add the code that hooks the
    > control and uses AddressOf to point the hook to the new procdure. If you
    > come back later and delete the procedure (not sure if it has to have code
    > in it or not), Visual Basic gives you a compile error when you attempt to
    > run the project, because it doesn't automatically delete the statement

    that
    > performs the hook! The statement with AddressOf is left holding an invalid
    > function reference, so your project won't compile.
    >
    > There sure is a lot more to watch out for, even when doing simple things
    > compared to Visual Basic 6. But there sure is a LOT more functiolity.
    >
    > James Foxall




  14. #14
    James Foxall Guest

    Re: VB threads


    "Michael \(michka\) Kaplan" <former_mvp@spamfree.trigeminal.nospam.com> wrote:
    >Is there a better way? Matt Curland's PushParamThunk is superior to the

    methods you have given. :-)

    Sounds interesting. Where I could I learn more about that?

    Thanks!

    James Foxall

  15. #15
    James Foxall Guest

    Re: VB threads


    "Michael \(michka\) Kaplan" <former_mvp@spamfree.trigeminal.nospam.com> wrote:
    >I doubt that the second problem is reallysomething for them to fix.... any
    >more than they would ever handle the case where you delete a function but
    >not its callers.


    I'd have to argue this one. Millions of VB'ers are used to easily adding
    an event (which doesn't seem so easy now), and just deleting it when they're
    done. If VB generates the statement for you when you add the event handler,
    it should remove the same statement when you delete the event handler. If
    the behavior stays as is, it will confuse a LOT of people. Confused me at
    first.

    James Foxall

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links