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