T
Ty
I have a project that fills a DB in the background using asyn
threading when the project loads. The issue is that I have a
progressbar, button, and lable that I am using to let the user know
that there is work being done so at the end of the task I want to hide
those items but I am getting the error "Cross-thread operation not
valid: Control accessed from a thread other than the thread it was
created on".
I am using the example from here.
http://www.aspfree.com/c/a/VB.NET/D...-Asynchronous-Programming-with-VB-NET-2005/3/
The controls are created in the main thread when the form loads.
I realize that I am missing something or not placing the code in the
correct place. Here is my code.
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
btnrunonce_Click(sender, e)
End Sub
Private Sub btnrunonce_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnrunonce.Click
StartExecuteTaskAsync()
End Sub
'*******************************************TEMPLATE CODE FOR
ASYNC APP
PROCESSING******************************************************
Dim thExecuteTaskAsync As Thread = Nothing
Private Sub StartExecuteTaskAsync()
'clear existing thread
If Not thExecuteTaskAsync Is Nothing Then
thExecuteTaskAsync.Abort()
thExecuteTaskAsync.Join()
thExecuteTaskAsync = Nothing
End If
'start a new thread to execute the task asynchronously
thExecuteTaskAsync = New Thread(AddressOf ExecuteTaskAsync)
thExecuteTaskAsync.Start()
End Sub
Private Sub ExecuteTaskAsync()
MY TASK CODE IS HERE
btnGetComputers.Enabled = True *****ERROR THROWN
HERE*********************************
ColourProgressBar3.Visible = False
btnrunonce.Visible = False
lblFirst.Visible = False
End If
End With
End Sub
'================================================================
''DELEGATE declaration
Private Delegate Sub delShowStatus(ByVal i As String)
Dim ShowStatus As New delShowStatus(AddressOf ShowProgress)
Private Sub ShowProgress(ByVal i As String)
If ColourProgressBar3.Value <> 10 Then
ColourProgressBar3.Value = ColourProgressBar3.Value + 1
Else
ColourProgressBar3.Value = 0
End If
End Sub
'=============================================================
Private Sub Form3_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'this is necessary if the form is trying to close, even before
the completion of task
If Not thExecuteTaskAsync Is Nothing Then
thExecuteTaskAsync.Abort()
End If
End Sub
Thanks,
Ty
threading when the project loads. The issue is that I have a
progressbar, button, and lable that I am using to let the user know
that there is work being done so at the end of the task I want to hide
those items but I am getting the error "Cross-thread operation not
valid: Control accessed from a thread other than the thread it was
created on".
I am using the example from here.
http://www.aspfree.com/c/a/VB.NET/D...-Asynchronous-Programming-with-VB-NET-2005/3/
The controls are created in the main thread when the form loads.
I realize that I am missing something or not placing the code in the
correct place. Here is my code.
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
btnrunonce_Click(sender, e)
End Sub
Private Sub btnrunonce_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnrunonce.Click
StartExecuteTaskAsync()
End Sub
'*******************************************TEMPLATE CODE FOR
ASYNC APP
PROCESSING******************************************************
Dim thExecuteTaskAsync As Thread = Nothing
Private Sub StartExecuteTaskAsync()
'clear existing thread
If Not thExecuteTaskAsync Is Nothing Then
thExecuteTaskAsync.Abort()
thExecuteTaskAsync.Join()
thExecuteTaskAsync = Nothing
End If
'start a new thread to execute the task asynchronously
thExecuteTaskAsync = New Thread(AddressOf ExecuteTaskAsync)
thExecuteTaskAsync.Start()
End Sub
Private Sub ExecuteTaskAsync()
MY TASK CODE IS HERE
btnGetComputers.Enabled = True *****ERROR THROWN
HERE*********************************
ColourProgressBar3.Visible = False
btnrunonce.Visible = False
lblFirst.Visible = False
End If
End With
End Sub
'================================================================
''DELEGATE declaration
Private Delegate Sub delShowStatus(ByVal i As String)
Dim ShowStatus As New delShowStatus(AddressOf ShowProgress)
Private Sub ShowProgress(ByVal i As String)
If ColourProgressBar3.Value <> 10 Then
ColourProgressBar3.Value = ColourProgressBar3.Value + 1
Else
ColourProgressBar3.Value = 0
End If
End Sub
'=============================================================
Private Sub Form3_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'this is necessary if the form is trying to close, even before
the completion of task
If Not thExecuteTaskAsync Is Nothing Then
thExecuteTaskAsync.Abort()
End If
End Sub
Thanks,
Ty