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. ProgressBar Thread

ProgressBar Thread

Scheduled Pinned Locked Moved Visual Basic
help
18 Posts 3 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.
  • C Christian Graus

    You're not allowed to interact with UI components on another thread. I'd use the BackgroundWorker class and use it's progress notification to tell the main thread to update it's progress bar.

    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

    A Offline
    A Offline
    ayeleteric
    wrote on last edited by
    #3

    Thank you much, have you got any simple example on this.

    Eric H.

    C 1 Reply Last reply
    0
    • A ayeleteric

      Thank you much, have you got any simple example on this.

      Eric H.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #4

      http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^] This is the MSDN example.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      A 1 Reply Last reply
      0
      • C Christian Graus

        http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^] This is the MSDN example.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        A Offline
        A Offline
        ayeleteric
        wrote on last edited by
        #5

        Thank you, but I already saw it before and it is a litle complicated (I am beginner). I found exactly what I need. Beginners, see following code: Option Explicit On Imports System.ComponentModel Public Class Form1 Private worker As BackgroundWorker Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load worker = New BackgroundWorker() worker.WorkerReportsProgress = True AddHandler worker.DoWork, New DoWorkEventHandler(AddressOf OnWork) AddHandler worker.ProgressChanged, New ProgressChangedEventHandler(AddressOf OnProgressChanged) End Sub Private Sub OnWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Dim i As Integer For i = 1 To 100 System.Threading.Thread.Sleep(10) worker.ReportProgress(i) Next End Sub Private Sub OnProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Invoke(New ChangeProgressBarHandler(AddressOf ChangeProgressBar), e.ProgressPercentage) End Sub Private Delegate Sub ChangeProgressBarHandler(ByVal percentage As Integer) Private Sub ChangeProgressBar(ByVal percentage As Integer) ProgressBar1.Value = percentage End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click worker.RunWorkerAsync() End Sub End Class

        Eric H.

        1 Reply Last reply
        0
        • A ayeleteric

          Hello, I am trying to do as following and I am getting the message "Cross-thread operation not valid: Control 'ProgressBar1' accessed from a thread other than the thread it was created on.": My Code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ind As Integer Button1.Enabled = False ProgressBar1.Step = 21 ProgressBar1.Value = 0 Dim trd As Threading.Thread = New Threading.Thread(AddressOf ThreadDo) trd.Start() ListBox1.Items.Clear() For ind = 0 To 50000 ListBox1.Items.Add(ind) Next Button1.Enabled = True End Sub Private Sub ThreadDo() ProgressBar1.PerformStep() If ProgressBar1.Value >= ProgressBar1.Maximum Then Timer1.Enabled = False End If End Sub Please help Thank you

          Eric H.

          S Offline
          S Offline
          ScottM1
          wrote on last edited by
          #6

          Here is 1 line that will stop that error and you can carry on using a thread.

          Control.CheckForIllegalCrossThreadCalls = false

          Just put it in the constructor of your form.

          There are 10 types of people in the world, those who understand binary and those who dont.

          C A 2 Replies Last reply
          0
          • S ScottM1

            Here is 1 line that will stop that error and you can carry on using a thread.

            Control.CheckForIllegalCrossThreadCalls = false

            Just put it in the constructor of your form.

            There are 10 types of people in the world, those who understand binary and those who dont.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #7

            Yes, but that sucks. The error is there for a reason.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            S 1 Reply Last reply
            0
            • C Christian Graus

              Yes, but that sucks. The error is there for a reason.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              S Offline
              S Offline
              ScottM1
              wrote on last edited by
              #8

              :| You're just upset because you didn't think of it or you don't understand it. I give lessons on Wednesdays and Thursdays if you'd like to attend. :) I know that it could give problems but for basic applications where only one thread accesses a control it is perfect. I have used this plenty of times before and had no problems.

              There are 10 types of people in the world, those who understand binary and those who dont.

              C 1 Reply Last reply
              0
              • S ScottM1

                Here is 1 line that will stop that error and you can carry on using a thread.

                Control.CheckForIllegalCrossThreadCalls = false

                Just put it in the constructor of your form.

                There are 10 types of people in the world, those who understand binary and those who dont.

                A Offline
                A Offline
                ayeleteric
                wrote on last edited by
                #9

                Thak you, I try this as following, but it doen't work Public Class Form1 Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. Control.CheckForIllegalCrossThreadCalls = False End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ind As Integer Button1.Enabled = False ProgressBar1.Step = 5000 ProgressBar1.Value = 0 Dim trd As Threading.Thread = New Threading.Thread(AddressOf ThreadDo) trd.Start() ListBox1.Items.Clear() For ind = 0 To 5000 ListBox1.Items.Add(ind) ListBox1.SelectedIndex = ind Next Button1.Enabled = True End Sub Private Sub ThreadDo() ProgressBar1.PerformStep() If ProgressBar1.Value >= ProgressBar1.Maximum Then Timer1.Enabled = False End If End Sub End Class

                Eric H.

                C S 2 Replies Last reply
                0
                • S ScottM1

                  :| You're just upset because you didn't think of it or you don't understand it. I give lessons on Wednesdays and Thursdays if you'd like to attend. :) I know that it could give problems but for basic applications where only one thread accesses a control it is perfect. I have used this plenty of times before and had no problems.

                  There are 10 types of people in the world, those who understand binary and those who dont.

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #10

                  smyers wrote:

                  You're just upset because you didn't think of it or you don't understand it.

                  ROTFL - I know about it and know exactly what it does.

                  smyers wrote:

                  I have used this plenty of times before and had no problems.

                  If someone has crossed the road with a blindfold on once or twice without problems, does that make it a good thing to do ? I didn't think so. I prefer to help people learn to code properly, rather than give them nasty shortcuts that are going to bite them down the track when their code gets a little more complex.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  S 1 Reply Last reply
                  0
                  • A ayeleteric

                    Thak you, I try this as following, but it doen't work Public Class Form1 Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. Control.CheckForIllegalCrossThreadCalls = False End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ind As Integer Button1.Enabled = False ProgressBar1.Step = 5000 ProgressBar1.Value = 0 Dim trd As Threading.Thread = New Threading.Thread(AddressOf ThreadDo) trd.Start() ListBox1.Items.Clear() For ind = 0 To 5000 ListBox1.Items.Add(ind) ListBox1.SelectedIndex = ind Next Button1.Enabled = True End Sub Private Sub ThreadDo() ProgressBar1.PerformStep() If ProgressBar1.Value >= ProgressBar1.Maximum Then Timer1.Enabled = False End If End Sub End Class

                    Eric H.

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #11

                    It seems to me that you should do some reading, and perhaps not worry about threads for a while until you've got a greater understanding of more basic things. When asking a question, you should also define 'doesn't work', that doesn't tell us anything. Does it blow up ? Does it not do what yuo wanted ? Does it do anyting at all ? We don't know. Telling us the line that blows up and the error would also help. And, what you're doing now is telling the framework not to protect you from yourself. This may work fine for such a simple app, but you'd do better to learn how to use a more powerful thread class ( BackgroundWorker ) and to ensure that your UI access is all in one thread. Then you can write *good* code, instead of just code that compiles.

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                    A 1 Reply Last reply
                    0
                    • C Christian Graus

                      It seems to me that you should do some reading, and perhaps not worry about threads for a while until you've got a greater understanding of more basic things. When asking a question, you should also define 'doesn't work', that doesn't tell us anything. Does it blow up ? Does it not do what yuo wanted ? Does it do anyting at all ? We don't know. Telling us the line that blows up and the error would also help. And, what you're doing now is telling the framework not to protect you from yourself. This may work fine for such a simple app, but you'd do better to learn how to use a more powerful thread class ( BackgroundWorker ) and to ensure that your UI access is all in one thread. Then you can write *good* code, instead of just code that compiles.

                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                      A Offline
                      A Offline
                      ayeleteric
                      wrote on last edited by
                      #12

                      You right, when I am saying doesn't work, it means in this case that first the listbox fullfilled and then the progressbar get to 100%. I don't see any thread working. I am sorry my english is not mother tongue, I hope you understand Thanks

                      Eric H.

                      C 1 Reply Last reply
                      0
                      • A ayeleteric

                        Thak you, I try this as following, but it doen't work Public Class Form1 Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. Control.CheckForIllegalCrossThreadCalls = False End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ind As Integer Button1.Enabled = False ProgressBar1.Step = 5000 ProgressBar1.Value = 0 Dim trd As Threading.Thread = New Threading.Thread(AddressOf ThreadDo) trd.Start() ListBox1.Items.Clear() For ind = 0 To 5000 ListBox1.Items.Add(ind) ListBox1.SelectedIndex = ind Next Button1.Enabled = True End Sub Private Sub ThreadDo() ProgressBar1.PerformStep() If ProgressBar1.Value >= ProgressBar1.Maximum Then Timer1.Enabled = False End If End Sub End Class

                        Eric H.

                        S Offline
                        S Offline
                        ScottM1
                        wrote on last edited by
                        #13

                        I just tried to run this code and it runs. Don't know what you trying to do though.

                        There are 10 types of people in the world, those who understand binary and those who dont.

                        A 1 Reply Last reply
                        0
                        • S ScottM1

                          I just tried to run this code and it runs. Don't know what you trying to do though.

                          There are 10 types of people in the world, those who understand binary and those who dont.

                          A Offline
                          A Offline
                          ayeleteric
                          wrote on last edited by
                          #14

                          the listbox is fullfilled but the progressbar doesn't progress in parallel while the listbox is adding items. First the listbox add items second the progressbar =100% Isn't it? Thank you

                          Eric H.

                          1 Reply Last reply
                          0
                          • C Christian Graus

                            smyers wrote:

                            You're just upset because you didn't think of it or you don't understand it.

                            ROTFL - I know about it and know exactly what it does.

                            smyers wrote:

                            I have used this plenty of times before and had no problems.

                            If someone has crossed the road with a blindfold on once or twice without problems, does that make it a good thing to do ? I didn't think so. I prefer to help people learn to code properly, rather than give them nasty shortcuts that are going to bite them down the track when their code gets a little more complex.

                            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                            S Offline
                            S Offline
                            ScottM1
                            wrote on last edited by
                            #15

                            Christian Graus wrote:

                            If someone has crossed the road with a blindfold on once or twice without problems, does that make it a good thing to do

                            It wouldn't be a problem if no cars ever drove down that road. Like I said, using one thread will give you no problems. PS - What does ROTFL stand for? PPS - The lessons offer is still open :-D

                            There are 10 types of people in the world, those who understand binary and those who dont.

                            C 1 Reply Last reply
                            0
                            • S ScottM1

                              Christian Graus wrote:

                              If someone has crossed the road with a blindfold on once or twice without problems, does that make it a good thing to do

                              It wouldn't be a problem if no cars ever drove down that road. Like I said, using one thread will give you no problems. PS - What does ROTFL stand for? PPS - The lessons offer is still open :-D

                              There are 10 types of people in the world, those who understand binary and those who dont.

                              C Offline
                              C Offline
                              Christian Graus
                              wrote on last edited by
                              #16

                              smyers wrote:

                              Like I said, using one thread will give you no problems.

                              Fair enough. You're welcome to offer whatever advice you see fit. Experience tells me that if your advice is easier to follow, people will prefer it, even if it's bad.

                              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                              1 Reply Last reply
                              0
                              • A ayeleteric

                                You right, when I am saying doesn't work, it means in this case that first the listbox fullfilled and then the progressbar get to 100%. I don't see any thread working. I am sorry my english is not mother tongue, I hope you understand Thanks

                                Eric H.

                                C Offline
                                C Offline
                                Christian Graus
                                wrote on last edited by
                                #17

                                Your code makes no sense at all - your progress bar is running in a different thread to the thing you're tracking progress on. The thread that's adding to the listbox, should be the thread that runs the progress bar, otherwise there's no relationship between the progress made and the state of the bar. The code also calls a thread, outside of a loop, which will just step the progress bar once. And then it stops a timer, which we can't even see. Overall, this code shows every sign of you're being well out of your depth with the concepts you are playing with, I'd recommend working through a decent book and trying to learn some basics.

                                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                                A 1 Reply Last reply
                                0
                                • C Christian Graus

                                  Your code makes no sense at all - your progress bar is running in a different thread to the thing you're tracking progress on. The thread that's adding to the listbox, should be the thread that runs the progress bar, otherwise there's no relationship between the progress made and the state of the bar. The code also calls a thread, outside of a loop, which will just step the progress bar once. And then it stops a timer, which we can't even see. Overall, this code shows every sign of you're being well out of your depth with the concepts you are playing with, I'd recommend working through a decent book and trying to learn some basics.

                                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                                  A Offline
                                  A Offline
                                  ayeleteric
                                  wrote on last edited by
                                  #18

                                  If it is so easy for you to do this why don't you show me the solution like that I will be able to appropriate this easy example to my difficult application? In my side, I will continue to learn VB.NET in my rythm. Thank you

                                  Eric H.

                                  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