Sep 20, 2014 09:31 AM
(This post was last modified: Sep 20, 2014 09:34 AM by SIM-1456245.)
Hey people,
So the other day I was in need of a multi-threaded application i.e. thread-pool. If you don't know what a thread-pool is, it's basically used mostly when you need to execute a single piece of code multiple times at the same time. Some situations would be, bot's, crawlers, brute-forcers, you name it.. Well if you have tried to use a thread-pool, you know that the MSDN example looks very dirty. And to be honest the whole concept is not that easy to grasp. So I implemented the whole thread-pool thing in my code the way MSDN shows and it ruined my "beautiful" looking code because of the way it works with the arrays. I sat down and recoded the whole concept in my way, but then an idea popped up, why not code a whole handler class that will help not only me, but the others. So here it is. The concept is, you declare a new handler and set the method as a delegate that has the code on it and then set the amount of threads you want to run that code in. When you hit start, it will start the threads and it has multiple events. First it has started, that will fire up whenever you start the process. Then there is stopped, it fires up whenever all of the threads finished working. Also there are stopped/started events for each thread. And there is also executed event, it will fire up whenever the code get's executed, this is useful with the loop function. When we are at the loop function, if you set the loop property to true, the method that you passed will be called on all threads until you call the stop method. This is useful for many things that you want to run until something is done. Keep in mind that if you call stop, all of the threads will finish their job and then stop, it will not force them to stop. Also if the loop property is not set to true, the stop method will do absolutely nothing. Every event has custom event args that has elapsed time info, threadid(the thread number in that pool), etc. Lastly there is the method IsRunning that will tell you if any of the threads are running. Another thing to note is that initially you must set the method to be executed, but you can change it later.
tl;dr: A class that hooks a method and executes it on multiple threads using a thread-pool. You also get status with multiple events.
Future ideas:
-Separate AppDomain
-Killable threads
Enough talking, here is the code:
http://pastebin.com/8Ry3VF4H
Example app(you can find the code in the source for this as well):
Spoiler (Click to View)
So the other day I was in need of a multi-threaded application i.e. thread-pool. If you don't know what a thread-pool is, it's basically used mostly when you need to execute a single piece of code multiple times at the same time. Some situations would be, bot's, crawlers, brute-forcers, you name it.. Well if you have tried to use a thread-pool, you know that the MSDN example looks very dirty. And to be honest the whole concept is not that easy to grasp. So I implemented the whole thread-pool thing in my code the way MSDN shows and it ruined my "beautiful" looking code because of the way it works with the arrays. I sat down and recoded the whole concept in my way, but then an idea popped up, why not code a whole handler class that will help not only me, but the others. So here it is. The concept is, you declare a new handler and set the method as a delegate that has the code on it and then set the amount of threads you want to run that code in. When you hit start, it will start the threads and it has multiple events. First it has started, that will fire up whenever you start the process. Then there is stopped, it fires up whenever all of the threads finished working. Also there are stopped/started events for each thread. And there is also executed event, it will fire up whenever the code get's executed, this is useful with the loop function. When we are at the loop function, if you set the loop property to true, the method that you passed will be called on all threads until you call the stop method. This is useful for many things that you want to run until something is done. Keep in mind that if you call stop, all of the threads will finish their job and then stop, it will not force them to stop. Also if the loop property is not set to true, the stop method will do absolutely nothing. Every event has custom event args that has elapsed time info, threadid(the thread number in that pool), etc. Lastly there is the method IsRunning that will tell you if any of the threads are running. Another thing to note is that initially you must set the method to be executed, but you can change it later.
tl;dr: A class that hooks a method and executes it on multiple threads using a thread-pool. You also get status with multiple events.
Future ideas:
-Separate AppDomain
-Killable threads
Enough talking, here is the code:
http://pastebin.com/8Ry3VF4H
Example app(you can find the code in the source for this as well):
Spoiler (Click to View)


![[Image: 8b73ed42ce.png]](https://puu.sh/bGJnO/8b73ed42ce.png)
