techmanbd Posted March 29, 2004 Posted March 29, 2004 I have a problem. I have some code that runs and I out into a try statement. And then I have a stop button that invokes an error to stop the code running when I am programming mode this works, but if I start from the EXE I get the Just In Time box. is there a way to turn it off so the program just stops. I put an example code below. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim x As Integer Me.pgbTesting.Step = 1 Me.pgbTesting.Visible = True Try For x = 1 To 100 timedelay(500) Me.pgbTesting.PerformStep() Application.DoEvents() Next Catch ex As Exception MessageBox.Show("IT WORKKKEEEEDDDDD") Me.pgbTesting.Visible = False End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Error 11 End Sub The reason I am doing it this way is because the way I had it was too messy, I had a variable boolstop that when I pushed the stop button it would set it to true. But the actual code this would go in is long so every few lines I would have to do an IF statement, I figured invoking an error while the code is running in a TRY statement would stop the code and goto the catch, but doesn't. it just puts the JIT box up. Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
Moderators Robby Posted March 29, 2004 Moderators Posted March 29, 2004 The BoolStop is better than invoking an error, but better still would be if you use Threading, then you can stop a process in a clean managed way. Quote Visit...Bassic Software
techmanbd Posted March 29, 2004 Author Posted March 29, 2004 OK, I am definatly new to threading. OK I have the following Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim th As New ThreadStart(AddressOf test) Dim t As New Thread(th) t.Start() Me.timedelay(1000) t.Abort() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click End Sub Public Sub test() Dim x As Integer Me.pgbTesting.Step = 1 Me.pgbTesting.Visible = True Try For x = 1 To 100 Me.timedelay(100) Me.pgbTesting.PerformStep() Application.DoEvents() Next Catch ex As ThreadAbortException MessageBox.Show("IT WORKKKEEEEDDDDD") Me.pgbTesting.Visible = False End Try End Sub now the t.abort works fine when in button1. But I don't want to abort there. Button1 is to start and I want to be able to abort the thread using button2. I have tried using different ways which I thought would do it but having no luck. Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
Administrators PlausiblyDamp Posted March 29, 2004 Administrators Posted March 29, 2004 (edited) try Dim t As Thread Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim th As New ThreadStart(AddressOf test) t=new thread(th) t.Start() Me.timedelay(1000) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click t.Abort() End Sub not tested at all ;) Not even proof read :D but moving the declaration of the thread into a form level variable means you can access it from anywhere in the form. Edited February 27, 2007 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
techmanbd Posted March 29, 2004 Author Posted March 29, 2004 That works, thanks, but have now developed another problem. When it runs the thread the first time my progress bar pgbTesting does become visible, and when I stop it it does stop an becomes invisible. Now when I click button 1 again, the thread does start again but the progress bar does not become visible. any idea? Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
techmanbd Posted March 29, 2004 Author Posted March 29, 2004 Never mind, I figured it out. Intead of having the progress bar setting within the sub. I just do all the setting right before I start the thread Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.