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)
{
int Threads = 5; // Number of threads
}
Now its time for the actual thread creation part:
Code static void Main(string[] args)
{
int Threads = 5; // Number of threads
for (int i = 1; i < Threads; i++)
{
Thread T = new Thread(new ThreadStart(Work)); // Declare thread
T.Name = "Thread " + i; // Set a thread name
T.Start(); // Start the thread
}
}
And last but not least - the sub worker:
Code static void Work()
{
Console.WriteLine(Thread.CurrentThread.Name + ": Hello world!");
Console.ReadKey();
}
OUTPUT:
This is a very basic tutorial to help you understand how multithreading actually works.
What about tasks/parallels/threadpools?
(Mar 15, 2016 05:24 PM)Cecilia Wrote: What about tasks/parallels/threadpools?
This is just a basic tutorial for beginners.🤝
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
(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...
(Mar 15, 2016 11:45 PM)Cecilia 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.
DO NOT DEAL WITH THIS MEMBER
(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.
Code int Threads = 5; // Number of threads
for (int i = 1; i < Threads; i++)
fyi: i == 1 means 4 iterations, not 5 in your case
slowly becoming less active, contact me on-site before dealing.
Mar 16, 2016 07:22 AM
(This post was last modified: Mar 16, 2016 07:25 AM by Rhez.)
Code int Threads = 5; // Number of threads
for (int i = 1; i < Threads; i++)
1<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.
Mar 16, 2016 09:54 AM
(This post was last modified: Mar 16, 2016 09:56 AM by unSatisfied.)
Why not introduce tasks? Most of the time Task.Run (with lambda expression) is a much more elegant solution.
(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!
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.
Mar 16, 2016 05:28 PM
(This post was last modified: Mar 16, 2016 05:29 PM by Oliver Sykes.)
(Mar 15, 2016 11:45 PM)Cecilia 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.
This is because I just made a quick VERY BASIC thread on how multithreading works for users who are making their first bots etc.
If they can't figure this much out, they really don't need to be writing bots imo.
riperino animefanerino
(Sep 12, 2016 04:11 PM){Nexus} Wrote: If the FBI come knocking I'm gonna spew information out my ass about Aeon.
They'd have to tell me to stop giving them information.
(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.
(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.
|