|
1 | 1 | # ezThread |
2 | | -Library for easy threading tasks |
| 2 | +**ezThread** is a .NET standard library made to make threading easy. Features currently include an easy way to execute functions and cancel them async and a class that'll run your functions with the amount of threads requested. |
| 3 | + |
| 4 | +## Jobs |
| 5 | + |
| 6 | +Job is a class that contains your functions and can execute and cancel async. |
| 7 | +Jobs are required for the JobManager class. |
| 8 | +### Example |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + Job j = new Job(() => |
| 13 | + { |
| 14 | + Console.WriteLine("Job1"); |
| 15 | + }); |
| 16 | + |
| 17 | + //runs the job optional parameter of how many times to execute |
| 18 | + j.execute(); |
| 19 | + |
| 20 | + //runs the job async optional parameter of how many times to execute |
| 21 | + j.executeAsync(); |
| 22 | + |
| 23 | + //cancels the execution of the job if its running async |
| 24 | + j.cancelExecution(); |
| 25 | + |
| 26 | +## JobManager |
| 27 | + |
| 28 | +JobManager is a class you can provide a list of jobs too and the amount of threads you want to execute the jobs with. |
| 29 | +### Example |
| 30 | + |
| 31 | + List<Job> joblist = new List<Job>(); |
| 32 | + //Creating a 100 jobs to execute |
| 33 | + for (int i = 0; i < 100; i++) |
| 34 | + { |
| 35 | + int e = i; |
| 36 | + Job j = new Job(() => |
| 37 | + { |
| 38 | + Console.WriteLine("Job"+e); |
| 39 | + }); |
| 40 | + joblist.Add(j); |
| 41 | + } |
| 42 | + |
| 43 | + //Creates a jobmanager class with our list and 10 threads to execute them with |
| 44 | + JobManager jobManager = new JobManager(joblist, 10); |
| 45 | + |
| 46 | + //Call this function everytime you create a jobmanager class and everytime you remove or add a job using addJob() or removeJob() |
| 47 | + jobManager.setThreads(); |
| 48 | + |
| 49 | + //Starts the execution of the jobs |
| 50 | + jobManager.startThreads(); |
| 51 | + |
| 52 | + //Returns true if all threads are done |
| 53 | + jobManager.isDone(); |
| 54 | + |
| 55 | + //Stops the threads from proceeding to the next queued job and terminates them |
| 56 | + jobManager.killThreads(); |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +## Notes: |
| 61 | + |
| 62 | + - Do not use more threads than the amount of jobs. |
| 63 | + - Too many threads might slow down the device your running on and bottleneck the program. |
| 64 | + - In networking cases sometimes the more threads you use the slowler it'll execute if they're bottlenecked by the speed of your internet. |
| 65 | + - The killthread() function isn't meant to be used right now, but if you're gonna use it the first thread has an ID of 0 and the last one has an ID of `Threads-1` since threads are stored in a list. |
| 66 | + - Easier ways to control the threads coming in the future. |
0 commit comments