Tom Shelton wrote:
On 2007-12-14, Donald A. Fisher <ds****@bellsou th.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.EventArg s) Handles btnCreateExcel. Click
Me.btnCreateExc el.Enabled = False
Dim pCreateExcel As New ClassCreateExce l
AddHandler pCreateExcel.Cr eateExcelComple ted, AddressOf
CreateExcelCom pletedEventHand ler
Dim thCreateExcel As System.Threadin g.Thread
thCreateExcel = New System.Threadin g.Thread(Addres sOf
pCreateExcel.P rocess)
thCreateExcel.S tart()
End Sub
I've tried using the "CreateExcelCom pleteEventHandl er to re-enable the
button to no avail:
Code performed after actions are completed:
Private Sub CreateExcelComp letedEventHandl er(ByVal sender As
System.Objec t, ByVal e As System.EventArg s)
frmCreateExcelP rogress.Close()
Me.btnCreateExc el.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 backgroundworke r 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 CreateExcelComp leted(ByVal sender As Object, ByVal e As
System.EventArg s)
Then at the end of the class I have this line to call the handler:
RaiseEvent CreateExcelComp leted(Me, New System.EventArg s)
The handler runs the code above under "CreateExcelCom pletedEventHand ler"
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.InvalidO perationExcepti on 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.g et_Handle()
at System.Windows. Forms.Control.O nEnabledChanged (EventArgs e)
at System.Windows. Forms.ButtonBas e.OnEnabledChan ged(EventArgs e)
at System.Windows. Forms.Control.s et_Enabled(Bool ean value)
at MT2.frmMain.Cre ateExcelComplet edEventHandler( Object 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.ClassCreate Excel.Process() in K:\VBS_05_PROJ\ Munitions
Task Management Tools v1.0\Munitions Task Management Tools
v1.0\Class_Crea teExcel.vb:line 23
at System.Threadin g.ThreadHelper. ThreadStart_Con text(Object state)
at System.Threadin g.ExecutionCont ext.runTryCode( Object userData)
at
System.Runtime. CompilerService s.RuntimeHelper s.ExecuteCodeWi thGuaranteedCle anup(TryCode
code, CleanupCode backoutCode, Object userData)
at
System.Threadin g.ExecutionCont ext.RunInternal (ExecutionConte xt
executionContex t, ContextCallback callback, Object state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext
executionContex t, ContextCallback callback, Object state)
at System.Threadin g.ThreadHelper. ThreadStart()
when it reaches the line to re-enable the button:
Line to re-enable button:
Me.btnCreateExc el.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