By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,978 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,978 IT Pros & Developers. It's quick & easy.

Disable Button During Processing & Re-Enable After Processing?

P: n/a
Hello. I've been working a vb project and have a form with a button on
it that performs some actions after disabling the button when clicked:

Code disabling button and starting actions:
Private Sub btnCreateExcel_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCreateExcel.Click
Me.btnCreateExcel.Enabled = False
Dim pCreateExcel As New ClassCreateExcel
AddHandler pCreateExcel.CreateExcelCompleted, AddressOf
CreateExcelCompletedEventHandler
Dim thCreateExcel As System.Threading.Thread
thCreateExcel = New System.Threading.Thread(AddressOf
pCreateExcel.Process)
thCreateExcel.Start()
End Sub
I've tried using the "CreateExcelCompleteEventHandler to re-enable the
button to no avail:

Code performed after actions are completed:
Private Sub CreateExcelCompletedEventHandler(ByVal sender As
System.Object, ByVal e As System.EventArgs)
frmCreateExcelProgress.Close()
Me.btnCreateExcel.Enabled = True
End Sub
The actions completed really don't matter (I think). I just want the
button to be disabled so the user cannot start multiple events by
double-clicking or being impatient and clicking again and then re-enable
the button after processing has finished. Any help would be great!
Thanks in advance!

Don
Dec 14 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2007-12-14, Donald A. Fisher <ds****@bellsouth.netwrote:
Hello. I've been working a vb project and have a form with a button on
it that performs some actions after disabling the button when clicked:

Code disabling button and starting actions:
Private Sub btnCreateExcel_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCreateExcel.Click
Me.btnCreateExcel.Enabled = False
Dim pCreateExcel As New ClassCreateExcel
AddHandler pCreateExcel.CreateExcelCompleted, AddressOf
CreateExcelCompletedEventHandler
Dim thCreateExcel As System.Threading.Thread
thCreateExcel = New System.Threading.Thread(AddressOf
pCreateExcel.Process)
thCreateExcel.Start()
End Sub
I've tried using the "CreateExcelCompleteEventHandler to re-enable the
button to no avail:

Code performed after actions are completed:
Private Sub CreateExcelCompletedEventHandler(ByVal sender As
System.Object, ByVal e As System.EventArgs)
frmCreateExcelProgress.Close()
Me.btnCreateExcel.Enabled = True
End Sub
>
The actions completed really don't matter (I think). I just want the
button to be disabled so the user cannot start multiple events by
double-clicking or being impatient and clicking again and then re-enable
the button after processing has finished. Any help would be great!
Thanks in advance!

Don
This maybe a silly question - but is the event even beign fired? And if
it is, your most likely going to have issues because it's going to be
raised from your background thread...

Personally, if your going to do this, you might want to look into using
hte backgroundworker component.

--
Tom Shelton
Dec 14 '07 #2

P: n/a
Tom Shelton wrote:
On 2007-12-14, Donald A. Fisher <ds****@bellsouth.netwrote:
>Hello. I've been working a vb project and have a form with a button on
it that performs some actions after disabling the button when clicked:

Code disabling button and starting actions:
Private Sub btnCreateExcel_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCreateExcel.Click
Me.btnCreateExcel.Enabled = False
Dim pCreateExcel As New ClassCreateExcel
AddHandler pCreateExcel.CreateExcelCompleted, AddressOf
CreateExcelCompletedEventHandler
Dim thCreateExcel As System.Threading.Thread
thCreateExcel = New System.Threading.Thread(AddressOf
pCreateExcel.Process)
thCreateExcel.Start()
End Sub
I've tried using the "CreateExcelCompleteEventHandler to re-enable the
button to no avail:

Code performed after actions are completed:
Private Sub CreateExcelCompletedEventHandler(ByVal sender As
System.Object, ByVal e As System.EventArgs)
frmCreateExcelProgress.Close()
Me.btnCreateExcel.Enabled = True
End Sub
>The actions completed really don't matter (I think). I just want the
button to be disabled so the user cannot start multiple events by
double-clicking or being impatient and clicking again and then re-enable
the button after processing has finished. Any help would be great!
Thanks in advance!

Don

This maybe a silly question - but is the event even beign fired? And if
it is, your most likely going to have issues because it's going to be
raised from your background thread...

Personally, if your going to do this, you might want to look into using
hte backgroundworker component.
Yes, the event is fired. I've even added a message box to confirm. I
do, however, get an debugging error that I'm not understanding. At the
beginning of my separate class that performs actions as follows:

Event Code:
Public Event CreateExcelCompleted(ByVal sender As Object, ByVal e As
System.EventArgs)
Then at the end of the class I have this line to call the handler:
RaiseEvent CreateExcelCompleted(Me, New System.EventArgs)
The handler runs the code above under "CreateExcelCompletedEventHandler"
after the class is finished (this is where I've added the message box
and I get it). I'm getting a debug error:

System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control 'btnCreateExcel'
accessed from a thread other than the thread it was created on."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.OnEnabledChanged(Even tArgs e)
at System.Windows.Forms.ButtonBase.OnEnabledChanged(E ventArgs e)
at System.Windows.Forms.Control.set_Enabled(Boolean value)
at MT2.frmMain.CreateExcelCompletedEventHandler(Objec t sender,
EventArgs e) in K:\VBS_05_PROJ\Munitions Task Management Tools
v1.0\Munitions Task Management Tools v1.0\frmMain.vb:line 159
at MT2.ClassCreateExcel.Process() in K:\VBS_05_PROJ\Munitions
Task Management Tools v1.0\Munitions Task Management Tools
v1.0\Class_CreateExcel.vb:line 23
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.runTryCode(Objec t userData)
at
System.Runtime.CompilerServices.RuntimeHelpers.Exe cuteCodeWithGuaranteedCleanup(TryCode
code, CleanupCode backoutCode, Object userData)
at
System.Threading.ExecutionContext.RunInternal(Exec utionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

when it reaches the line to re-enable the button:

Line to re-enable button:
Me.btnCreateExcel.Enabled = True
This may be a little too detailed and I apologize. I am not familiar
with background workers. I did much reading and found a lot about
threads etc. Am I going about it all wrong? Thanks again.

Don
Dec 15 '07 #3

P: n/a
On 2007-12-14, Donald A. Fisher <ds****@bellsouth.netwrote:
Tom Shelton wrote:
>On 2007-12-14, Donald A. Fisher <ds****@bellsouth.netwrote:
>>Hello. I've been working a vb project and have a form with a button on
it that performs some actions after disabling the button when clicked:

Code disabling button and starting actions:
Private Sub btnCreateExcel_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCreateExcel.Click
Me.btnCreateExcel.Enabled = False
Dim pCreateExcel As New ClassCreateExcel
AddHandler pCreateExcel.CreateExcelCompleted, AddressOf
CreateExcelCompletedEventHandler
Dim thCreateExcel As System.Threading.Thread
thCreateExcel = New System.Threading.Thread(AddressOf
pCreateExcel.Process)
thCreateExcel.Start()
End Sub
I've tried using the "CreateExcelCompleteEventHandler to re-enable the
button to no avail:

Code performed after actions are completed:
Private Sub CreateExcelCompletedEventHandler(ByVal sender As
System.Object, ByVal e As System.EventArgs)
frmCreateExcelProgress.Close()
Me.btnCreateExcel.Enabled = True
End Sub
>>The actions completed really don't matter (I think). I just want the
button to be disabled so the user cannot start multiple events by
double-clicking or being impatient and clicking again and then re-enable
the button after processing has finished. Any help would be great!
Thanks in advance!

Don

This maybe a silly question - but is the event even beign fired? And if
it is, your most likely going to have issues because it's going to be
raised from your background thread...

Personally, if your going to do this, you might want to look into using
hte backgroundworker component.
Yes, the event is fired. I've even added a message box to confirm. I
do, however, get an debugging error that I'm not understanding. At the
beginning of my separate class that performs actions as follows:

Event Code:
Public Event CreateExcelCompleted(ByVal sender As Object, ByVal e As
System.EventArgs)
Then at the end of the class I have this line to call the handler:
RaiseEvent CreateExcelCompleted(Me, New System.EventArgs)
The handler runs the code above under "CreateExcelCompletedEventHandler"
after the class is finished (this is where I've added the message box
and I get it). I'm getting a debug error:

System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control 'btnCreateExcel'
accessed from a thread other than the thread it was created on."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.OnEnabledChanged(Even tArgs e)
at System.Windows.Forms.ButtonBase.OnEnabledChanged(E ventArgs e)
at System.Windows.Forms.Control.set_Enabled(Boolean value)
at MT2.frmMain.CreateExcelCompletedEventHandler(Objec t sender,
EventArgs e) in K:\VBS_05_PROJ\Munitions Task Management Tools
v1.0\Munitions Task Management Tools v1.0\frmMain.vb:line 159
at MT2.ClassCreateExcel.Process() in K:\VBS_05_PROJ\Munitions
Task Management Tools v1.0\Munitions Task Management Tools
v1.0\Class_CreateExcel.vb:line 23
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.runTryCode(Objec t userData)
at
System.Runtime.CompilerServices.RuntimeHelpers.Exe cuteCodeWithGuaranteedCleanup(TryCode
code, CleanupCode backoutCode, Object userData)
at
System.Threading.ExecutionContext.RunInternal(Exec utionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

when it reaches the line to re-enable the button:

Line to re-enable button:
Me.btnCreateExcel.Enabled = True
This may be a little too detailed and I apologize. I am not familiar
with background workers. I did much reading and found a lot about
threads etc. Am I going about it all wrong? Thanks again.

Don
Don,

As I suspected - you are getting a cross thread access violation. You
can not directly access windows controls outside of the thread on which
they were created. You need to change the code of your method
(something like):
Private Sub CreateExcelCompletedEventHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Me.InvokeRequired Then
Me.Invoke ( _
new EventHandler(Me.CreateExcelCompletedEventHandler), _
new Object() {sender, e))
Else
frmCreateExcelProgress.Close()
Me.btnCreateExcel.Enabled = True
End If
End Sub

--
Tom Shelton
Dec 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.