How to pause program execution for some time? - red

I'm using "Red Programming Language" version "0.6.4" on Windows and making a command line application.
I don't know much Red language and I don't understand many things. I did go over "work in progress" docs at (https://doc.red-lang.org/en/) before asking here.
I need to see the "Something, something..." on the screen for a bit (let's say 1 second) before the command prompt window closes.
Red []
; do something
print "Something, something..."
; pause 1 // not working
; sleep 1 // not working
quit
As in the code comments I've tried with pause or sleep but I get an error Script Error: sleep has no value. How to make it sleep?
Thank you.

The function you are looking for is WAIT. Try wait 1.

Using WAIT as prescribed by #MarkI above is the correct answer.
But I wanted to add below which should really be a comment but it looks nicer formatted!
The best way to explore Red/Rebol is via the console and making use of HELP
>> help pause
No matching values were found in the global context.
This means there is nothing defined for PAUSE (and ditto for help sleep).
So instead quote the word and it will search through all defined function documentation...
>> help "sleep"
No matching values were found in the global context.
>> help "pause"
No matching values were found in the global context.
Still no luck :(
OK lets try something related...
>> help "time"
... long list found items...
This now returns all functions that have some connection to "time" in their function spec/docs. In this list is WAIT function. However another HELP clearly shows it...
>> help "duration"
wait native! Waits for a duration in seconds or specified time.
Now we have it...
>> help wait
USAGE:
WAIT value
DESCRIPTION:
Waits for a duration in seconds or specified time.
WAIT is a native! value.
ARGUMENTS:
value [number! time! block! none!]
REFINEMENTS:
/all => Returns all events in a block
Hope that helps.

Related

How to stop a Matlab script but don’t kill the process? [duplicate]

This question already has answers here:
Stop and continue execution from debugger possible?
(6 answers)
Closed 8 years ago.
Strg+C stops and kills a Matlab script (at least sometimes). But is there a way to stop a Matlab, take a look at some variables and continue the calculation?
I am not talking about just setting a breakpoint. I want my script, let’s say run for couple hours come back to it hit some buttons that stops the calculations take a look at some variable and then continue the calculation.
I tried to find out if there is some shortcut key for this – I am quite sure there isn’t.
Now I was thinking about including an if-case that looks if a certain button was pressed by the user. If so there would be a useless k=0 line and a breakpoint on it. And if no one is pressing this button the loop would continue. But this is where my limited Matlab knowledge leaves me. I don’t know if there is a way to ask for a user-button press but don’t wait for a button press like in the function input. Also I just have a running script, I don’t have any GUI.
To drop to the command prompt you need the command keyboard and then type return when you have finished (you don't need a breakpoint). The tricky bit is how to trigger it. There a few options. The easiest is to open a figure window. The following code halts the process when any key is pressed.
keyDownListener=#(src,event) keyboard;
fig = figure;
drawnow
set(fig,'KeyPressFcn',keyDownListener)
for p=1:10000
%do some thing
end
You can modify this to test for a specific key since the keypress is contained within the event struct.
To use no figure gui at all its more of a problem. I'm not aware of a non blocking keyboard input method. A mex file the runs kbhit() in C might do it, but kbhit() is not standard C so it would only work on Windows. An easier option maybe to test for the presence of a file.
for p=1:100000
if exist(fullfile(pwd,'halt.tmp'),'file')
keyboard
end
%do something here
end
This drops to the debug console when halt.tmp is created in the current directory.
Other potential methods could involve using multiple threads to read 'input' (either the Parallel computer toolbox or undocumented Java code), or using http://psychtoolbox.org/ as mentioned by #bdecaf

Can you pause MATLAB? [duplicate]

This question already has answers here:
Stop and continue execution from debugger possible?
(6 answers)
Closed 6 years ago.
In MATLAB, I'm running some code which takes a while to run. I'd like to pause the code to check on some variable values. Is there a way I can do this without having to re-run the code from the beginning? I don't want to terminate the program; just pause it.
You can halt execution and give a command prompt in two ways of which I am aware:
Putting keyboard in your code where you want to stop.
Setting a breakpoint.
You can resume and stop execution with dbcont and dbquit, respectively. To step forward, use dbstep. dbstack lets you see where you are. There are many more commands. The help page for any of these will give you other suggestions.
As Dennis Jaheruddin has pointed out, dbstop also has several useful features worth trying. In particular is the ability to set conditional and global (any line meeting a criterion) breakpoints via the dbstop if syntax. For example, dbstop if error will break to a debugging command prompt on any error. One suggestion he made, which I now do, is to put dbstop if error into startup.m so that this behavior will be default when you start MATLAB. You may need to create this file in a userpath folder; edit(fullfile(regexp(userpath,'^[^;]*','match','once'),'startup.m')).
One way to achieve what you're looking for would be to use code sections (also known as code cells), where you divide your code into sections divided by lines with two percent signs (%%).
Then, in the editor, you can press ctrl+enter to execute the current code section, and ctrl+up/down to navigate between sections.
Well there is the pause command, but then you cannot check for the variable contents in the workspace because the program is running.
What you probably want is to set a breakpoint (See the Debug menu / key F12).
At a breakpoint matlab pauses the program and enters debugging mode in which you can see and edit the variables. Once finished, you can resume the program where it was paused.
I'm not sure about Windows users but if you're running Linux you can start Matlab in a terminal using
matlab -nodesktop
then once Matlab has started, cd to your project directory and start your Matlab script. Now whenever you want to pause execution you can use ctrl-Z. Then to resume type fg. I hope this helps.

Common Lisp Pause Command inside a Loop

Is it possible to have a command in common lisp which somehow temporarily pauses the execution. In matlab you can achieve this by using the pause command, in which you can also set the time to pause as well. What I want to do is that, in a loop I will print my result to the top-level then wait for x-seconds (I am going to set the x), do the next iteration, print and wait and so on.
Thank you very much in advance.
See SLEEP.
You can either use SLIME, interrupt the execution of the program and
eval an expression in frame to modify the value of some variables « by hands »
or use the exceptions system and provide a restart which will ask to the
user the new values.

Stop a script in Matlab

My question is, how do I stop a script by a pressing a GUI button? I already tried to write a code that simulates "CTRL+C" press, but it doesn't work.
I'm not sure there's a way to stop another script from being called. One alternative would be to set a global variable that's periodically checked by the script you wish to stop. If you set the value of a "stop processing" variable to true in your callback, the other script could stop if it found that it was supposed to stop.
Edit
If you'd like to have a GUI option to stop an ongoing process, I would recommend you take a look at something like STOPLOOP on the MATLAB File Exchange.
I won't write the code for you but here's a high-level way to accomplish this:
Display a waitbar with a button on it. Create a callback function for the button which sets a flag to true.
Begin computation inside of a for-loop. In the loop:
1. update the waitbar.
2. call the drawnow function so that the callback is executed properly. Remember MATLAB is single-threaded, so this is necessary or the callback will not execute until the script finishes.
3. perform any other computation
4. check for the flag set to true. if it is true, return to stop execution.
The flag could be a global variable, or a handle-based object (so that it is passed by reference).
EDIT:
This answer is not applicable for the current question.
This answer is applicable only for scripts having the first line = #!/usr/bin/matlab
use pkill without option will send a TERM signal:
pkill yourscriptname
If you really want the same signal as CTRL+C then:
pkill -3 yourscriptname
If your script still does not stop, you can use the most aggressive signal KILL:
pkill -9 yourscriptname
Of course, if you known the PID (Process IDentifier), you can simply use kill:
kill yourPID
kill -3 yourPID
kill -9 yourPID
You can have more info about signals using one of these commands:
man 7 signal
kill -l
info signal
I don't do a lot of GUIs, but for debugging purposes I would try to set the button callback to #keyboard. That is, something like:
set(handleToGuiButton,'Callback',#keyboard)
To actually stop execution you would need to somehow communicate this button press into the loop that was executing, for example via global variables, or something fancier (e.g. https://stackoverflow.com/a/8537460/931379)
But I would honestly look at the stoploop link (from another answer) before going down any of these routes.

How can I insert a time string into a GDB log?

I recently discovered that you can set breakpoints in Xcode that will print to the console and auto-continue -- meaning you can insert log statements without having to write NSLog() calls and recompile (on-the-fly logging, woot).
Only problem is that it seems to be a little limited in what you can display when doing a log.
It shows some tokens you can insert, like %B to print out some info about the current breakpoint or %H for the hit count.
I'd like to know if there's any way I can insert a time stamp in a particular format into the log line?
I tried playing with the "shell script" breakpoint action, but it told me that the date command didn't exist.... strange...
Any help would be awesome,
Thanks guys!
Read the GDB manual about Breakpoint Command Lists
You can give any breakpoint (or watchpoint or catchpoint) a series of commands to execute when your program stops due to that breakpoint. For example, you might want to print the values of certain expressions, or enable other breakpoints.
And in particular:
for example, here is how you could use breakpoint commands to print the value of x at entry to foo whenever x is positive.
break foo if x>0
commands
silent
printf "x is %d\n",x
cont
end
Does this answer your question?