Multy threading question
-
I wrote following code Imports System.Threading Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s As New ThreadStart(AddressOf progress) Dim t As New Thread(s) t.Start() End Sub Sub progress() For x As Int32 = 1 To 10000 ProgressBar1.Value += 1 Next End Sub End Class I want to increase progress bar value within separate thread. When i run this code it wasn't run as i suppose and it threw Invalid operation exception. It's message is "Cross-thread operation not valid: Control 'ProgressBar1' accessed from a thread other than the thread it was created on." What happened here. Please someone explain it.
-
I wrote following code Imports System.Threading Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s As New ThreadStart(AddressOf progress) Dim t As New Thread(s) t.Start() End Sub Sub progress() For x As Int32 = 1 To 10000 ProgressBar1.Value += 1 Next End Sub End Class I want to increase progress bar value within separate thread. When i run this code it wasn't run as i suppose and it threw Invalid operation exception. It's message is "Cross-thread operation not valid: Control 'ProgressBar1' accessed from a thread other than the thread it was created on." What happened here. Please someone explain it.
You cannot access form from another thread. To do this, you should write:
Invoke(New MethodInvoker(Sub() ProgressBar1.Value += 1))
Die Energie der Welt ist konstant. Die Entropie der Welt strebt einem Maximum zu.
-
You cannot access form from another thread. To do this, you should write:
Invoke(New MethodInvoker(Sub() ProgressBar1.Value += 1))
Die Energie der Welt ist konstant. Die Entropie der Welt strebt einem Maximum zu.
-
Instead of
ProgressBar1.Value += 1
just replace this line
Die Energie der Welt ist konstant. Die Entropie der Welt strebt einem Maximum zu.
-
I wrote following code Imports System.Threading Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s As New ThreadStart(AddressOf progress) Dim t As New Thread(s) t.Start() End Sub Sub progress() For x As Int32 = 1 To 10000 ProgressBar1.Value += 1 Next End Sub End Class I want to increase progress bar value within separate thread. When i run this code it wasn't run as i suppose and it threw Invalid operation exception. It's message is "Cross-thread operation not valid: Control 'ProgressBar1' accessed from a thread other than the thread it was created on." What happened here. Please someone explain it.
This article[^] explains what to do, and how and why. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
If you want my opinion or comment, ask in a forum or on my profile page; I will not participate in frackin' Q&A
-
I wrote following code Imports System.Threading Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s As New ThreadStart(AddressOf progress) Dim t As New Thread(s) t.Start() End Sub Sub progress() For x As Int32 = 1 To 10000 ProgressBar1.Value += 1 Next End Sub End Class I want to increase progress bar value within separate thread. When i run this code it wasn't run as i suppose and it threw Invalid operation exception. It's message is "Cross-thread operation not valid: Control 'ProgressBar1' accessed from a thread other than the thread it was created on." What happened here. Please someone explain it.
Pasan,
Pasan148 wrote:
I want to increase progress bar value within separate thread. When i run this code it wasn't run as i suppose and it threw Invalid operation exception. It's message is "Cross-thread operation not valid: Control 'ProgressBar1' accessed from a thread other than the thread it was created on."
You can achieve this very easily using the background worker class (http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]). Just enable progress change and supports cancellation on the background worker object. Then, in the
Do_Work
sub, report progress, and you can easily update your progressbar in theprogresschanged
event. Hope this helps.He who goes for revenge must first dig two graves.