![]() |
|
[C#] How to create a simple multithreaded program - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] How to create a simple multithreaded program (/showthread.php?tid=5204507) Pages: Pages
1
2
|
How to create a simple multithreaded program - Verizur - Mar 15, 2016 In this tutorial I'll be teaching you the basics in multithreading. This is very useful if you want to speed-up your programs actions. First we'll need to add this reference to our project: Code using System.Threading;Now we have to decide how many threads we want to start: Code static void Main(string[] args)Now its time for the actual thread creation part: Code static void Main(string[] args)And last but not least - the sub worker: Code static void Work()OUTPUT: ![]() This is a very basic tutorial to help you understand how multithreading actually works. * - Cecilia - Mar 15, 2016 What about tasks/parallels/threadpools? * - Verizur - Mar 15, 2016 (Mar 15, 2016 05:24 PM)LaPanthere Wrote: What about tasks/parallels/threadpools? This is just a basic tutorial for beginners.🤝 * - +Haze - Mar 15, 2016 You can't cover a tutorial on threads and not talk about unsafe and safe thread calls. Like what is the proper way to handle cross thread calls. Also creating threads like this is ehhh, atleast use a thread pool if you don't intend n using Tasks/Parralell * - Cecilia - Mar 15, 2016 (Mar 15, 2016 11:37 PM)+Haze Wrote: You can't cover a tutorial on threads and not talk about unsafe and safe thread calls. Like what is the proper way to handle cross thread calls. Also creating threads like this is ehhh, atleast use a thread pool if you don't intend n using Tasks/Parralell Its not terribly hard to invoke the ui thread from a different thread, or similar... * - +Haze - Mar 16, 2016 (Mar 15, 2016 11:45 PM)LaPanthere Wrote: Its not terribly hard to invoke the ui thread from a different thread, or similar... That's not the point, it should be covered imho. * - Odus - Mar 16, 2016 (Mar 16, 2016 12:11 AM)+Haze Wrote: That's not the point, it should be covered imho. A hell of a lot more should be covered and explained, even as a beginner tutorial. Instructing to paste code teaches nothing. * - Assault - Mar 16, 2016 Code int Threads = 5; // Number of threadsfyi: i == 1 means 4 iterations, not 5 in your case * - Rhez - Mar 16, 2016 Code int Threads = 5; // Number of threads1<5 = True {Do the code here} - increment 2<5 = True {Do the code here} - increment 3<5 = True {Do the code here} - increment 4<5 = True {Do the code here} - increment 5<5 = False {Nothing} - nothing It means that it will only have 4 iteration not 5 lol. * - unSatisfied - Mar 16, 2016 Why not introduce tasks? Most of the time Task.Run (with lambda expression) is a much more elegant solution. * - mrnikbobjeff - Mar 16, 2016 (Mar 16, 2016 09:54 AM)unSatisfied Wrote: Why not introduce tasks? Most of the time Task.Run (with lambda expression) is a much more elegant solution. Especially because Thread is obsolete, Task should be used when available! * - Verizur - Mar 16, 2016 Thanks for all the feedback guys. I know that there's a lot that I'm not talking about in the thread. This is because I just made a quick VERY BASIC thread on how multithreading works for users who are making their first bots etc. * - Oliver Sykes - Mar 16, 2016 (Mar 15, 2016 11:45 PM)LaPanthere Wrote: Its not terribly hard to invoke the ui thread from a different thread, or similar... It's not terribly hard to write the following either. Code var thread = new Thread({x => SomeMethod.DoShit(x)});(Mar 16, 2016 02:35 PM)Verizur Wrote: Thanks for all the feedback guys. I know that there's a lot that I'm not talking about in the thread. If they can't figure this much out, they really don't need to be writing bots imo. * - unSatisfied - Mar 16, 2016 (Mar 16, 2016 12:35 PM)mrnikbobjeff Wrote: Especially because Thread is obsolete, Task should be used when available! Totally agree, I never use Thread anymore. * - Cecilia - Mar 16, 2016 (Mar 16, 2016 05:55 PM)unSatisfied Wrote: Totally agree, I never use Thread anymore. LOL. Thread isn't fucking deprecated/obsolete, its extremely useful in different circumstances. For multi-threading (repeating same action over and over) it is not ideal but still can be used. |