-1

This is my code:

For i As Integer = 0 to 20
    mythread = New System.Threading.Thread(AddressOf myfunction)
    mythread.Start()
Next

I want to kill all threads that are running myfunction()

I tried:

For Each hello123 As System.Threading.Thread(AddressOf myfunction)
    thread.abort(hello123)
Next

But came up with an error (on AddressOf):

Array bounds cannot appear in type specifiers.

Stefan Đorđević
  • 565
  • 1
  • 4
  • 22
  • 1
    [**You shouldn't use `Thread.Abort()` to stop a thread**](https://stackoverflow.com/a/1560567/3740093). You must implement your own check _**inside**_ `myfunction` that will terminate the thread if necessary. For instance: `If IsThread1Running = False Then Return`. -- Also consider using [**tasks**](https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/task-cancellation). – Visual Vincent Sep 03 '17 at 20:04
  • As for accessing each thread in a loop: You can only iterate your threads as .NET objects if you store them yourself somewhere, such as in a [**`List(Of T)`**](https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx). – Visual Vincent Sep 03 '17 at 20:06
  • I've added **do until hey = 0** loop to my function (hey is integer), so when i press stop button, function is automatically stopped and it works fine, but i want to know if there's better solution. – Stefan Đorđević Sep 03 '17 at 20:07
  • Not really. The _actual_ thread will terminate itself once `myfunction` has finished executing. Stopping what you do in there and letting the method (function) end normally is the best (and safest) way as that's how threads are intended to operate. – Visual Vincent Sep 03 '17 at 20:09
  • Possible duplicate of [How to terminate a thread in C#?](https://stackoverflow.com/questions/14131608/how-to-terminate-a-thread-in-c) – Visual Vincent Sep 03 '17 at 20:13
  • Sure it is, but what if it's downloading webpage's html, which is my case? As far as i know i cannot stop it which is the biggest problem. – Stefan Đorđević Sep 03 '17 at 21:07
  • Downloading a page doesn't take long unless you have a _**REALLY**_ slow connection. Though if you want to be able to cancel it either write your own downloading code or use [**`WebClient.DownloadStringAsync()`**](https://msdn.microsoft.com/en-us/library/system.net.webclient.downloadstringasync(v=vs.110).aspx) and [**`WebClient.CancelAsync()`**](https://msdn.microsoft.com/en-us/library/system.net.webclient.cancelasync(v=vs.110).aspx). – Visual Vincent Sep 04 '17 at 05:58
  • I'm talking about really big page :) – Stefan Đorđević Sep 04 '17 at 19:26
  • Ok, well I suggest using a `WebClient`. – Visual Vincent Sep 04 '17 at 19:43

1 Answers1

0

To stop a function, we need to define a boolean. Then, function needs to be inside do while our_boolean = true loop. Stop button's code must have our_boolean = false, so when it's pressed, do while loop is gonna end immediately.

It looks something like this:

Do While potato = True
    'code
Loop

When stop button is pressed, loop is gonna stop immediately because potato is set to false. Then when we want to continue running the code, we just need to set potato to true and call function.

Stefan Đorđević
  • 565
  • 1
  • 4
  • 22