Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Multy threading question

Multy threading question

Scheduled Pinned Locked Moved Visual Basic
question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pasan148
    wrote on last edited by
    #1

    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.

    D L Y 3 Replies Last reply
    0
    • P Pasan148

      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.

      D Offline
      D Offline
      Dimitri Witkowski
      wrote on last edited by
      #2

      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.

      P 1 Reply Last reply
      0
      • D Dimitri Witkowski

        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.

        P Offline
        P Offline
        Pasan148
        wrote on last edited by
        #3

        Where should i place this code? please explain it with my example.

        D 1 Reply Last reply
        0
        • P Pasan148

          Where should i place this code? please explain it with my example.

          D Offline
          D Offline
          Dimitri Witkowski
          wrote on last edited by
          #4

          Instead of

          ProgressBar1.Value += 1

          just replace this line

          Die Energie der Welt ist konstant. Die Entropie der Welt strebt einem Maximum zu.

          1 Reply Last reply
          0
          • P Pasan148

            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.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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


            1 Reply Last reply
            0
            • P Pasan148

              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.

              Y Offline
              Y Offline
              Yosh_
              wrote on last edited by
              #6

              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 the progresschanged event. Hope this helps.

              He who goes for revenge must first dig two graves.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups