-2

In c# im creating my threads like this:

void LaunchThread(string url, string search, string regexstring)
{     
    new Thread(delegate()
    {
        Scrape(url, search, regexstring,false);

    }).Start();

}

and I use an INT variable to follow how many threads are currently running but i have a feeling it can be a little wonky at times and not be accurate (due to when you time the check on how many exist)

I have 2 questions:

  1. Is there a variable that could tell me how many threads are currently running
  2. Is there a way to close/exit all threads mid way not waiting for them to complete?

thanks alot SO im new to c# and multitasking as a whole

Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28
tim_po
  • 105
  • 10
  • Yes there is. Yes also this is possible. Check msdn. – Adriano Repetti May 15 '15 at 18:35
  • @AdrianoRepetti that's not very helpful i suppose... a lot of questions in here can be answered with "check documentation"... – imlokesh May 15 '15 at 18:38
  • You need to learn a bit more about .NET threading before using it. Otherwise you are doing things wrong way. Instead of starting and tracking threads manually you can just let ThreadPool take care of it. For 2 you can use `ManualResetEventSlim`. – Vlad May 15 '15 at 18:39
  • @imlokesh number doesn't make them on topic! A basic search in docs is always desiderabile... – Adriano Repetti May 15 '15 at 18:41
  • i looked up ManualResetEventSlim and i didnt udnerstand what it is or why i should use it – tim_po May 15 '15 at 18:45
  • @Vlad mind explaining? all sources i used to look up ManualResetEventSlim didnt say anything about closing all threads. help is very appreciated – tim_po May 15 '15 at 18:59
  • You can use `ManualResetEventSlim.Set` as a signal. Inside your threads you should periodically check `ManualResetEventSlim.Wait(0)` every x ms and stop the operation when the check returns true. Think like this: you shouldn't "close" thread from outside, instead stop what it's doing from inside. You need to understand such threading concepts, that's why I said you need to learn more before using threading classes. – Vlad May 16 '15 at 21:45

2 Answers2

0
  1. If you're interested in only the threads you've started, keeping a counter like you describe should work fine. Just make sure that you use proper locking when you get/change it.
  2. If the work you're doing in the worker threads involves a loop, you can add logic to the loop that checks a variable to see whether it should keep running. You could set this variable from another thread (again, assuming proper locking).
adv12
  • 8,443
  • 2
  • 24
  • 48
  • Sadly it does not involve a loop but has a Webclient in it which might take a few seconds to complete - even minutes. – tim_po May 15 '15 at 18:44
0
  1. Keep a collection of the threads you spawn like List<Thread>. Then you can get the Count property to check the number of threads. You can even get live threads only using threadCollection.Count(t => t.IsAlive).

  2. I assume you're using HttpWebRequest. Check out these two threads:

Terminate loopless thread instantly without Abort or Suspend

Killing HttpWebRequest object using Thread.Abort

Community
  • 1
  • 1
imlokesh
  • 2,506
  • 2
  • 22
  • 26