1. Tired of not being able to see links? Want to chat with other members? Need help?

Creating emulator commands with delays

Discussion in 'Habbo Tutorials' started by Seriosk, Feb 2, 2017.

  1. Seriosk

    Seriosk Programmer;

    Hello. I am writing this mainly for @SpeedBlood ([ Want to see this link? Register ! ]) 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:
    [ Want to see this link? Register ! ]
    [ Want to see this link? Register ! ]
    [ Want to see this link? Register ! ]
     
    Chenaho, yoyok and SpreedBlood like this.
  2. SpreedBlood

    SpreedBlood Member

    Thanks alot was really helpful, and learnt new stuff, thanks alot buddy! ::

    Skickat från min FRD-L09 via Tapatalk
     
  3. Velaski

    Velaski winner

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

    SpreedBlood Member

    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
     
  5. Chenaho

    Chenaho Member

    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.
     
  6. SpreedBlood

    SpreedBlood Member

    What do you mean?

    Skickat från min FRD-L09 via Tapatalk
     
  7. Seriosk

    Seriosk Programmer;

    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.
     
  8. SpreedBlood

    SpreedBlood Member

    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
     
  9. Velaski

    Velaski winner

    That's what the thread was about mate.
     
  10. Jerry

    Jerry C:\holorp.com

    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:
    [​IMG]
    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):
    [​IMG]

    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 likes this.
  11. Seriosk

    Seriosk Programmer;

    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.
     
    Jerry likes this.
  12. Jaden

    Jaden on foe nem

    not good.

    Liam's method is better.
    --- Double Post Merged, Feb 4, 2017, Original Post Date: Feb 4, 2017 ---
    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:
    [ Want to see this link? Register ! ]
    --- Double Post Merged, Feb 4, 2017 ---
    Task, Threads, Await (Goes along with Task, and Async), and Delay (Task/Async).

    Are all methods used for asynchronous programming.
     
    Velaski, JynX and Seriosk like this.
  13. MasterJiq

    MasterJiq Member

    I used
    Code:
    System.Threading.Thread.Sleep(5000);
     
  14. SpreedBlood

    SpreedBlood Member

    Good luck using commands after that :) You're making the thread sleep.. better use task.delay and use the thread that you're given. That's why this is better [emoji14]

    Skickat från min FRD-L09 via Tapatalk
     

Share This Page

Loading...