Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

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.

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
  • Administrators
Posted (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 by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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?

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted
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
Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...