Shoutbox

So is it impossible to make a Sleep() function? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: So is it impossible to make a Sleep() function? (/showthread.php?tid=61667)

So is it impossible to make a Sleep() function? by segosa on 06-26-2006 at 10:38 PM

Someone on the IRC channel asked if JS had a Sleep() function to pause the script for x milliseconds, and apparently it doesn't. I tried to make my own function, but the problem is since it's not multithreaded it causes everything to freeze for the duration that you pause the script.

My function is the following:

code:
function Sleep(ms)
{
    var x = Interop.Call("Kernel32", "GetTickCount");
    var y = x;
    while (y + ms > x)
    {
        x = Interop.Call("Kernel32", "GetTickCount");
    }
}

Calling the Sleep() API with Interop.Call does the same.

Is it possible?
RE: So is it impossible to make a Sleep() function? by mathieumg on 06-26-2006 at 11:07 PM

Like you said, as it is not multithreaded I don't think it is possible. That's why Patchou created timers.


RE: So is it impossible to make a Sleep() function? by segosa on 06-26-2006 at 11:13 PM

Hmm yeah of course, could probably make something using timers...


RE: So is it impossible to make a Sleep() function? by CookieRevised on 06-26-2006 at 11:48 PM

Each time I needed a sleep() in programs I made, there were always better ways to do this.

What I mean is, you can almost always make a program without the use of a sleep() or the likes. The use of sleep() is almost always not needed at all and often is the result of a bad made routine... at least that's my experience of it.

(there are some uses for it of course, but they are rare)

HO


RE: So is it impossible to make a Sleep() function? by Riveck on 06-27-2006 at 12:14 AM

You could try to do something like this:

/* Global Var */
var flag = false;


/* Wherever you want */
[...]
MsgPlus.AddTimer("MyTimer",5000);
while(flag == false) {}
[...]


/* The timer */
OnEvent_Timer(TimerId)
{
    if (TimerId == "MyTimer")
    {
        flag=true;
        MsgPlus.AddTimer("MyTimer",5000);
    }
}


RE: RE: So is it impossible to make a Sleep() function? by b0rna on 06-27-2006 at 12:42 AM

quote:
Originally posted by Riveck
You could try to do something like this:

/* Global Var */
var flag = false;


/* Wherever you want */
[...]
MsgPlus.AddTimer("MyTimer",5000);
while(flag == false) {}
[...]


/* The timer */
OnEvent_Timer(TimerId)
{
    if (TimerId == "MyTimer")
    {
        flag=true;
        MsgPlus.AddTimer("MyTimer",5000);
    }
}



This wont work, i thought of this and tried several methodes to achieve this perior to your post. once you enter the while loop you cannot break early, even the onEvent timer waits for the loop to end and thus it runs indefinatly.

I really need a way to pause my script and have yet to find one.

edit: after 3 hours of trying today i think it just might be impossible.
RE: So is it impossible to make a Sleep() function? by TheBlasphemer on 06-27-2006 at 01:16 AM

Erm... why on earth loop through GetTickCount when there's a win32 Sleep function ;)?


RE: So is it impossible to make a Sleep() function? by b0rna on 06-27-2006 at 01:28 AM

show us master for i am a nub


RE: So is it impossible to make a Sleep() function? by -dt- on 06-27-2006 at 02:36 AM

why would you ever want to while loop to pause your code :( just pass a function and get the function which handles the timer to call it when it is up and you can use closures to pass variables

eg

code:
var callback;
function myCode(arg1){
    //do stuff
   
    //oh noes now i need to sleep to wait for a value
   
    //function to call when timer is up
   
    function returnCallback(arg1){
        function callback(arg){
            return arg1 * arg;
        }
    return callback;
    }
    callback = returnCallback(5);
    MsgPlus.AddTimer("MyTimer",5000);
   
}



function OnEvent_Timer(TimerId){
    if(TimerId == "MyTimer"){
    //it should show the result of 5*5
    Debug.Trace(callback(5));
    }
}





more on javascript closures here
http://jibbering.com/faq/faq_notes/closures.html
RE: RE: So is it impossible to make a Sleep() function? by segosa on 06-27-2006 at 07:11 AM

quote:
Originally posted by TheBlasphemer
Erm... why on earth loop through GetTickCount when there's a win32 Sleep function ;)?


Read my post, I said I tried that too.
RE: So is it impossible to make a Sleep() function? by ben_b on 07-14-2006 at 08:18 AM

So how could I make the following sleep for 5 seconds at the location indicated...

code:
function OnEvent_ChatWndSendMessage(Wnd,Msg) {
    if(Msg == "^toast") {
   
        var x = 0;
   
        do
            {
                Messenger.MyStatus = 2;
                Messenger.MyStatus = 3;
                x = x+1
                //Sleep here
            }
        while (x < 3);
       
    return "";
    }
}

RE: So is it impossible to make a Sleep() function? by CookieRevised on 07-14-2006 at 09:51 AM

I suggest you simply don't...


quote:
Originally posted by CookieRevised
Each time I needed a sleep() in programs I made, there were always better ways to do this.

What I mean is, you can almost always make a program without the use of a sleep() or the likes. The use of sleep() is almost always not needed at all and often is the result of a bad made routine... at least that's my experience of it.

(there are some uses for it of course, but they are rare)

In the shown routine you could easly make that without the use of a dirty sleep() function. All you need in your case is to set a timer in the "IF (Msg==blah)" function and when that timer is triggered perform the status changes once more (two times). It would be much more elegant (and doesn't halt everything else).

;)

RE: So is it impossible to make a Sleep() function? by ben_b on 07-14-2006 at 09:55 AM

Exactly... I haven't been able to get the timers to work so I was looking for a little more help on how to make the timers work... here's my current code...

code:
//Check for /command
function OnEvent_ChatWndSendMessage(ChatWnd, sMessage) {
    if(sMessage.charAt(0) == "/"){
        return parseCommands(sMessage,ChatWnd);
    }
    else{
        return sMessage;
    }
}

//Add the /command in command list
function OnGetScriptCommands(){
    var commands = '<ScriptCommands>';
        commands+='<Command>';
            commands+='<Name>toastspammer</Name>';
            commands+='<Description>Appear Offline and then back Online</Description>';
            commands+='<Parameters/>';
        commands+='</Command>';
    commands+='</ScriptCommands>';
    return commands;
}

//Parse commands
function parseCommands(sMessage,iOriginWnd) {   
    var x = 0;
   
    do
        {
            Messenger.MyStatus = 2;
            Messenger.MyStatus = 3;
            x = x+1
            //Sleep here
        }
    while (x < 3); //Number of times to sign on and off
                 //NOTICE: Messenger can't handle very many without
                 //          slowing down or possibly crashing, unless
                 //          the timer is enabled so they don't all occur
                 //          at the same time.
       
    return "";
}

RE: So is it impossible to make a Sleep() function? by sbegouin on 07-24-2006 at 10:27 PM

You can just use this DLL (see attachment) :

code:
    Interop.Call(MsgPlus.ScriptFilesPath+'\\Wait.dll', "Attente", 1000); // Wait 1000 ms
    Interop.FreeDLL(MsgPlus.ScriptFilesPath+'\\Wait.dll');

RE: RE: So is it impossible to make a Sleep() function? by segosa on 07-25-2006 at 11:18 AM

quote:
Originally posted by sbegouin
You can just use this DLL (see attachment) :

code:
    Interop.Call(MsgPlus.ScriptFilesPath+'\\Wait.dll', "Attente", 1000); // Wait 1000 ms
    Interop.FreeDLL(MsgPlus.ScriptFilesPath+'\\Wait.dll');


That just calls Sleep() which you can do with Interop.Call directly too. And it's not multithreaded, so it'll freeze WLM.
RE: So is it impossible to make a Sleep() function? by sbegouin on 07-25-2006 at 06:10 PM

This method doesn't freeze the processor (usage is at 0%) :), but during this, you can't do anything with WLM. :(


RE: RE: RE: So is it impossible to make a Sleep() function? by deAd on 07-25-2006 at 06:55 PM

quote:
Originally posted by segosa
That just calls Sleep() which you can do with Interop.Call directly too. And it's not multithreaded, so it'll freeze WLM.

He never said it froze the processor.