-
Notifications
You must be signed in to change notification settings - Fork 5
MultiThreading
This maybe useless in some cases but it comes pretty handy in some too where we want to make multiple requests to a certain site/server.
examples here will be simple, the usage of this is limited to what you want it to do,
the "Threading" class contains alot of methods to work with threading and here are few examples.
Contains 3 main methods which are
- Run
- RunFor
- RunForEach
they all run in a parallel thread to do an action accordingly
we will be going through them in the examples down.
Run method can hold multiple threads to invoke an action of anytype that doesnt hold a parameter to it
// initialize new instance of the multithreading class, which also takes how much threads you want to run.
// the threads can only be integers, 5 threads for this example
using (MultiThreading threads = new MultiThreading(5))
{
// ThreadCount will set the threads amount or get them.
// threads.ThreadCount = 5;
// we can also activate infinite mode to repeat the process, which will result in an endless loop unless we stop it with a special condition
// threads.EnableInfiniteRepeat = true/false;
int i = 0;
threads.Run(() =>
{
i++;
Console.WriteLine(i);
});
}
which will output this in the console
1 2 3 4 5
we'll make a dummy void here
public void dummy(string str)
{
Console.WriteLine(str);
}
then we can invoke it with the parameter
using (MultiThreading threads = new MultiThreading(5))
{
string dummystring = "Hello Thread";
threads.Run(() => dummy(dummystring));
}
which will output
Hello Thread Hello Thread Hello Thread Hello Thread Hello Thread
Dummy action void
public static void dummyaction()
{
Console.WriteLine("Hello Thread");
}
Then we invoke it normally
using (MultiThreading threads = new MultiThreading(5))
{
threads.Run(dummyaction);
}
this will output
Hello Thread Hello Thread Hello Thread Hello Thread Hello Thread
The "RunFor" method can be used for loop and such where you pass it 2 integer parameter which are (fromInclusive) and (toExclusive) and the Action which takes an integer parameter that indicates the current thread, runs each threads continuously.
The same goes for the usage as in the "Run" method
List<string> dummylist = new List<string>();
using (MultiThreading threads = new MultiThreading(dummylist.Count))
{
threads.RunFor(0, dummylist.Count,(int i)=> {
Console.WriteLine(i);
});
}
lets say that dummylist's count = 5 the output would be
0 1 2 3 4
"RunForEach" takes a list of object and an action that takes the same type of an object as a parameter, invoke method is the same the 2 threading methods above.
Dummy action that takes an object parameter
private static void dummyaction(object obj)
{
Console.WriteLine(obj);
}
we can set the parameter type depending on what the "RunForEach" object type.
List<int> dummylist = new List<int>();
using (MultiThreading threads = new MultiThreading(5))
{
threads.RunForEach<int>(dummylist,dummyaction);
}
this will run 5 continues threads in which each thread takes 1 object from the list and pass it as a parameter to the dummyaction until the method reaches the end of the list to stop, the infinite repeat will keep passing objects from the list as a parameter on the dummyaction until the infinite repeat stopped.
note: in this case we used a list, but arrays and object collection can also be used.
if the list had 5 numbers which are (4,5,6,7,8) the output will be
4 5 6 7 8