|
-
Jun 16th, 2007, 11:14 AM
#1
Thread Starter
Addicted Member
[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?
-
Jun 16th, 2007, 11:20 AM
#2
Re: [2005] New to VB.Net - Doubt about calling controls from external functions
 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
-
Jun 16th, 2007, 11:25 AM
#3
Thread Starter
Addicted Member
Re: [2005] New to VB.Net - Doubt about calling controls from external functions
Well, that's the same as what I wrote earlier...
-
Jun 16th, 2007, 11:31 AM
#4
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:
Delegate Sub DelegateBoolean(ByVal bln As Boolean)
Private Sub btnSendMsgEnabled(ByVal bln As Boolean)
If btnSendMsg.InvokeRequired Then
btnSendMsg.Invoke(New DelegateBoolean(AddressOf btnSendMsgEnabled), bln)
Else
btnSendMsg.Enabled = bln
End If
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.
-
Jun 16th, 2007, 11:33 AM
#5
Hyperactive Member
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.
-
Jun 16th, 2007, 11:45 AM
#6
Thread Starter
Addicted Member
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.
-
Jun 16th, 2007, 11:56 AM
#7
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?
-
Jun 16th, 2007, 12:12 PM
#8
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|