Results 1 to 8 of 8

Thread: [2005] New to VB.Net - Doubt about calling controls from external functions

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    [2005] New to VB.Net - Doubt about calling controls from external functions

    OK, I decided to start looking at VB.Net, and I've been having some problems with something as simple as enabling buttons.

    I have a class that raises an event in a thread running at the background. When the event is raised I make a simple comprobation like, in order to enable a button.

    If Not btnSendMsg.Enabled Then btnSendMsg.Enabled = True.

    However this throws an error because it's a different subroutine (or subprocess, I don't know which is the correct English term here). So after looking at Visual Studio help I've seen I have to force the invoking of a callback.

    Is this always so? Is it correct to disable the CheckForIllegalCrossThreadCalls property?

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] New to VB.Net - Doubt about calling controls from external functions

    Quote Originally Posted by Neverbirth
    OK, I decided to start looking at VB.Net, and I've been having some problems with something as simple as enabling buttons.

    I have a class that raises an event in a thread running at the background. When the event is raised I make a simple comprobation like, in order to enable a button.

    If Not btnSendMsg.Enabled Then btnSendMsg.Enabled = True.

    However this throws an error because it's a different subroutine (or subprocess, I don't know which is the correct English term here). So after looking at Visual Studio help I've seen I have to force the invoking of a callback.

    Is this always so? Is it correct to disable the CheckForIllegalCrossThreadCalls property?
    Hi,

    You can try this:

    Code:
    If btnSendMsg.Enabled = False Then 
    btnSendMsg.Enabled = True
    End if
    Hope it helps,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: [2005] New to VB.Net - Doubt about calling controls from external functions

    Well, that's the same as what I wrote earlier...

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] New to VB.Net - Doubt about calling controls from external functions

    You will have to create a subroutine that enables/disables the button, and invoke the subroutine. Here:

    VB.Net Code:
    1. Delegate Sub DelegateBoolean(ByVal bln As Boolean)
    2. Private Sub btnSendMsgEnabled(ByVal bln As Boolean)
    3. If btnSendMsg.InvokeRequired Then
    4.     btnSendMsg.Invoke(New DelegateBoolean(AddressOf btnSendMsgEnabled), bln)
    5. Else
    6.     btnSendMsg.Enabled = bln
    7. End If
    8. End Sub

    A control can not be accessed from another thread than the one it was created on. The Invoke method runs the specified subroutine on the thread the control was created on.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005] New to VB.Net - Doubt about calling controls from external functions

    You can use InvokeRequired to determine if an Invoke is necessary.
    Prefix has no suffix, but suffix has a prefix.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: [2005] New to VB.Net - Doubt about calling controls from external functions

    That's what I'm already doing Atheist (except that I make an instance of the callback outside of the invoke function, but it doesn't matter).

    I guess this is the right way of doing this, thanks for your explanation. Dunno, after all this time of using VB6, and some other languages, I feel strange doing this heh.

    BTW, which is the right way of stopping a thread? I've seen the suspend function is deprecated.

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] New to VB.Net - Doubt about calling controls from external functions

    Stopping the thread completely? The Abort method will stop the thread. Suspend will cause the thread to simply freeze, which there is really no need to do, thats why the IDE advices you to use some synchronizing methods instead if you want to synchronize threads.

    If you want the thread to be aborted when the application stops you set IsBackground to true and it will do so automatically.

    EDIT: What was the exact error message you where getting?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: [2005] New to VB.Net - Doubt about calling controls from external functions

    Ah, didn't notice the abort method, my bad...

    I wasn't getting any error message with the thread, but just wanted to know how it's correctly done.

    I'm working on porting an old VB6 project that let send files to different clients through sockets.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width