Creating emulator commands with delays

Seriosk

Programmer;
Oct 29, 2016
256
105
Hello. I am writing this mainly for @SpeedBlood ( ) 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:
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:


 

Velaski

winner
Aug 4, 2015
562
165
Oh shit! This is why I love Liam! I've been using Thread.Sleep most of the time, but now I know. Thanks <3
 

NeedForSpreed

Member
May 18, 2014
326
71
Oh shit! This is why I love Liam! I've been using Thread.Sleep most of the time, but now I know. Thanks <3
Thread.Sleep isn't a good way of handling stuff... good luck doing another command after [emoji14] But Task.Delay is a much better way of handling the commands. And yes thanks Liam for this tutorial :)

Skickat från min FRD-L09 via Tapatalk
 

Chenaho

Member
Feb 9, 2016
45
9
So what are you trying to say about the love effect, where the user disconnect during the delay? Could you clarify, because I don't quite understand what you mean there.
 

Seriosk

Programmer;
Oct 29, 2016
256
105
So what are you trying to say about the love effect, where the user disconnect during the delay? Could you clarify, because I don't quite understand what you mean there.
Its one of them things that if you don't understand then you don't need to know, it'll come with knowledge. Don't confuse your mind with it.
 

NeedForSpreed

Member
May 18, 2014
326
71
Its one of them things that if you don't understand then you don't need to know, it'll come with knowledge. Don't confuse your mind with it.
I'll just create a few commands and make a tutorial for people who want to use task.delay [emoji14] Thanks alot bro

Skickat från min FRD-L09 via Tapatalk
 

Velaski

winner
Aug 4, 2015
562
165
Thread.Sleep isn't a good way of handling stuff... good luck doing another command after [emoji14] But Task.Delay is a much better way of handling the commands. And yes thanks Liam for this tutorial :)

Skickat från min FRD-L09 via Tapatalk
That's what the thread was about mate.
 

Jerry

not rly active lol
Jul 8, 2013
1,957
522
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.
Thread.Sleep isn't a good way of handling stuff...
That's because you don't know how to pause sleep a thread for a specific code properly.

I create a new thread to use Thread.Sleep to avoid sleeping the whole thread, for example:
7a4d42e13360430a928484d7d5abd354.png

By using this method, it does not suspends/block your whole thread or sleeps the whole environment when using this method.

If you don't use "new threads", the whole/main thread will most likely be blocked for a specific interval (in milliseconds):
2faa2a6c16b845ca8f306dd0563a8ad7.png


Just clarifying because it looked like to me that you don't know how to use the threading properly, but now you do. (Tried to word it for y'all to understand). Using Task.Delay works too in this situation, but I create a new thread to sleep a thread so this method is irrelevant to me.

So ya, hope this helps.
 

Seriosk

Programmer;
Oct 29, 2016
256
105
That's because you don't know how to pause sleep a thread for a specific code properly.

I create a new thread to use Thread.Sleep to avoid sleeping the whole thread, for example:
7a4d42e13360430a928484d7d5abd354.png

By using this method, it does not suspends/block your whole thread or sleeps the whole environment when using this method.

If you don't use "new threads", the whole/main thread will most likely be blocked for a specific interval (in milliseconds):
2faa2a6c16b845ca8f306dd0563a8ad7.png


Just clarifying because it looked like to me that you don't know how to use the threading properly, but now you do. (Tried to word it for y'all to understand). Using Task.Delay works too in this situation, but I create a new thread to sleep a thread so this method is irrelevant to me.

So ya, hope this helps.
Not sure whos benifit this was for but yeah you're right. The main thread is known as the UI thread but obviously I was talking in terms for people putting it inside the UI thread for commands which is what most people do then wonder why their thread has suddenly frozen. There are many ways to delay..

  • A timer with a or multiple callbacks
  • Tasks
  • Threads
  • Asynchronous programming
  • Await
  • Delay
But thank you for summing things up with the new Thread(method) option.
 

Jaden

not so active
Aug 24, 2014
886
263
That's because you don't know how to pause sleep a thread for a specific code properly.

I create a new thread to use Thread.Sleep to avoid sleeping the whole thread, for example:
7a4d42e13360430a928484d7d5abd354.png

By using this method, it does not suspends/block your whole thread or sleeps the whole environment when using this method.

If you don't use "new threads", the whole/main thread will most likely be blocked for a specific interval (in milliseconds):
2faa2a6c16b845ca8f306dd0563a8ad7.png


Just clarifying because it looked like to me that you don't know how to use the threading properly, but now you do. (Tried to word it for y'all to understand). Using Task.Delay works too in this situation, but I create a new thread to sleep a thread so this method is irrelevant to me.

So ya, hope this helps.
not good.

Liam's method is better.
 
All you're doing when you schedule a task is queuing it to run on from a thread (or multiple) provided thread pool (implicitly stated).

Using Sledmore's Plus Edit, you're already given a thread (from an explicit thread pool) for each session (ProcessComponent.cs), the best method would be to take advantage of the thread's cycle (void Run) (seeing as nothing important really happens in it) and add your code there.

example:

 
Not sure whos benifit this was for but yeah you're right. The main thread is known as the UI thread but obviously I was talking in terms for people putting it inside the UI thread for commands which is what most people do then wonder why their thread has suddenly frozen. There are many ways to delay..

  • A timer with a or multiple callbacks
  • Tasks
  • Threads
  • Asynchronous programming
  • Await
  • Delay
But thank you for summing things up with the new Thread(method) option.
Task, Threads, Await (Goes along with Task, and Async), and Delay (Task/Async).

Are all methods used for asynchronous programming.
 

Users who are viewing this thread

Top