Seriosk
Programmer;
- Oct 29, 2016
- 256
- 105
Hello. I am writing this mainly for @SpeedBlood (
Table of Contents
Thread.Sleep
I'm not sure if many of you even know what Thread.Sleep does, I guess most of you think it just waits a few seconds before actually executing your code? Wrong. Thread.Sleep Suspends/Blocks your whole thread for how ever long you tell it. This means the user executing the command (if your Session has a thread each Session) will be paused and unable to execute anything, including packets. Some emulators may vary from this but this is how most emulators work in terms of threading, and multi threading operations.
Task.Delay
What is Task.Delay? Task.Delay basically tels your code to hold on a second and wait before executing your code. The different between Task.Delay and Thread.Sleep is that Task.Delay doesn't effect anything except for it delays the code you tell it to. It's just like as if you added a timer, and put that code in a timer callback (It's not exactly like this but this is how a noob would understand it).
Why is Task.Delay Better?
It's not (in all cases), sometimes Thread.Sleep is good for certain thinks depending on what you exactly want it to do, but if you just want it for a command, Task.Delay is a better alternative for what most people are trying to do. I am NOT saying Thread.Sleep should be replaced all the time with Task.Delay, you just need to understand when to use each one.
Use Thread.Sleep when you want to block the current thread.
Use Task.Delay when you want a logical delay without blocking the current thread.
How to use Task.Delay
Obviously with this and even Thread.Sleep, with any kind of delay, there is chance of bugs. I remember I was in a situation about 2 years ago where I started using delays and it was on a :kiss command, once the delay had finished and the Target needed the love enable removed what happens if the Target had logged out during that delay? It's just a good idea to ensure you handle things like this when using delays.
Some websites that may help you out:
You must be registered for see links
) because he sent me a private message last night asking me how to create commands with delays, and wondered if Thread.Sleep was a good way of doing it. In this tutorial I'll be explaining why Thread.Sleep isn't the best, what is better, and how to use something better.Table of Contents
- In-depth of Thread.Sleep
- Task.Delay
- How to use Task.Delay
Thread.Sleep
I'm not sure if many of you even know what Thread.Sleep does, I guess most of you think it just waits a few seconds before actually executing your code? Wrong. Thread.Sleep Suspends/Blocks your whole thread for how ever long you tell it. This means the user executing the command (if your Session has a thread each Session) will be paused and unable to execute anything, including packets. Some emulators may vary from this but this is how most emulators work in terms of threading, and multi threading operations.
Task.Delay
What is Task.Delay? Task.Delay basically tels your code to hold on a second and wait before executing your code. The different between Task.Delay and Thread.Sleep is that Task.Delay doesn't effect anything except for it delays the code you tell it to. It's just like as if you added a timer, and put that code in a timer callback (It's not exactly like this but this is how a noob would understand it).
Why is Task.Delay Better?
It's not (in all cases), sometimes Thread.Sleep is good for certain thinks depending on what you exactly want it to do, but if you just want it for a command, Task.Delay is a better alternative for what most people are trying to do. I am NOT saying Thread.Sleep should be replaced all the time with Task.Delay, you just need to understand when to use each one.
Use Thread.Sleep when you want to block the current thread.
Use Task.Delay when you want a logical delay without blocking the current thread.
How to use Task.Delay
Waiting 5 seconds between each action:
Separating each action by 1 second:
Code:
Task.Run(async delegate
{
// do your first part of code here
await Task.Delay(5000);
// do your second part of code here
});
Separating each action by 1 second:
Code:
Task.Run(async delegate
{
// do your first part of code here
await Task.Delay(1000);
// do your second part of code here
await Task.Delay(1000);
// do your third part of code here
await Task.Delay(1000);
// do your fourth part of code here
});
Obviously with this and even Thread.Sleep, with any kind of delay, there is chance of bugs. I remember I was in a situation about 2 years ago where I started using delays and it was on a :kiss command, once the delay had finished and the Target needed the love enable removed what happens if the Target had logged out during that delay? It's just a good idea to ensure you handle things like this when using delays.
Some websites that may help you out:
You must be registered for see links
You must be registered for see links
You must be registered for see links