Normal Topic 100% CPU load while @GetFromKeyboard() waiting (Read 1415 times)
daniel
Member
*
Offline


No personal text

Posts: 7
Location: Germany
Joined: Dec 20th, 2005
100% CPU load while @GetFromKeyboard() waiting
Dec 20th, 2005 at 9:48pm
Print Post Print Post  
Hello,

we are using Sesame for our small export company and I already did some programming on our application. I am not sure if I have detected a bug:

While running @GetFromKeyboard() command, Sesame causes a 100% CPU load on the client PC (running Win XP Pro).

After @GetFromKeyboard() captured a keystroke, the CPU load is o.k.

I am still using Sesame 1.1.2, but there is no hint in the bug fix list of Sesame 1.1.3

Thanks and regards
Daniel
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: 100% CPU load while @GetFromKeyboard() waiting
Reply #1 - Dec 20th, 2005 at 10:04pm
Print Post Print Post  
Hello Daniel,

It is not really a bug, just how it works as it is an I/O Loop. While it does peg the cpu to 100% any other program that needs to use the CPU during this interval can use the CPU.

So one client can be running a Mass Update while another client, on the same computer, is running @GetFromKeyboard() and the Mass Update Client will still be updating at roughly the same speed as if the other client was not running @GetFromKeyboard().

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Steve_in_Texas
Senior Member
*****
Offline


No personal text

Posts: 893
Location: San Antonio
Joined: Feb 21st, 2004
Re: 100% CPU load while @GetFromKeyboard() waiting
Reply #2 - Dec 21st, 2005 at 3:08am
Print Post Print Post  
I'm still wondering what this command is used for. How is it different than @promptforuserinput?

Can this command (or any other command) be used to break out of a Mass Update that is in progress?

THanks,
Steve
  
Back to top
IP Logged
 
daniel
Member
*
Offline


No personal text

Posts: 7
Location: Germany
Joined: Dec 20th, 2005
Re: 100% CPU load while @GetFromKeyboard() waiting
Reply #3 - Dec 21st, 2005 at 10:58pm
Print Post Print Post  
@Ray: Thanks for your fast reply. I am no experienced programmer and my English might be too bad to understand your explanation completely.  Undecided

So far, I did not check if the @GetFromKeyboard() command influences the speed of other applications running on the client machine. But in my opinion, it is no usual behavor that a software causes 100% CPU load while running a loop waiting for a keystroke.

Some time ago, I did some programming in PureBasic and there is a command which returns the control to the operating system for x milliseconds. This command can be integrated into loops to avoid useless high CPU load.

The CPU load is important for power management, i.e. some notebooks increase CPU speed if a high CPU load is detected.

However, I would appreciate if you would try to explain me why it is neccessary to cause a 100% CPU load just to capture a keystroke.

@Steve: @GetFromKeyboard() captures a single keystroke. For Example, you could use it to build navigation by keystrokes. The @PromptForUserInput pops-up a text box and prompts the user to type in some information into it. So far, I never had the need to interrupt mass updates by programming. It could be a good idea to open a new topic for your question.

Best regards,
Daniel (from Germany)
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: 100% CPU load while @GetFromKeyboard() waiting
Reply #4 - Dec 22nd, 2005 at 3:39am
Print Post Print Post  
Quote:
@Ray: Thanks for your fast reply. I am no experienced programmer and my English might be too bad to understand your explanation completely.  Undecided

So far, I did not check if the @GetFromKeyboard() command influences the speed of other applications running on the client machine. But in my opinion, it is no usual behavor that a software causes 100% CPU load while running a loop waiting for a keystroke.

It isn't entirely typical. It is called a "busy wait". Usually, most programs (including Sesame) use a "sleeping wait" for events. Sesame's main event loop uses a sleeping wait.
Quote:
Some time ago, I did some programming in PureBasic and there is a command which returns the control to the operating system for x milliseconds. This command can be integrated into loops to avoid useless high CPU load.

We may well add an optional timeout value to the GetFromKeyboard function, to allow just that. And, to make sure that there is a way out, should the user not ever press a key.
Quote:
However, I would appreciate if you would try to explain me why it is neccessary to cause a 100% CPU load just to capture a keystroke.

To allow events (especially screen updates) to still be processed, while the system is also waiting on the user to press a key, Sesame spawns a second event loop that "polls" for events. If the event is the keystroke, the loop is completed. If it isn't the keystroke, the event still needs to be processed, without dropping out of the busy loop. Otherwise, screen updates (for example) would be disabled while the system waits for a keystroke.

That would ordinarily be okay, if one of the internal functions in Sesame (Sesame itself) needed the keystroke. But SBasic functions are called from a callback that is itself called from the main event loop. That means that the way for the non-keystroke event to be sent back to the main event loop to be handled is by calling the main event loop recursively. We could, of course, do that. But SBasic itself is not re-entrant. So, if we were to call the main event loop, there is a high risk that subsequent events could cause another callback to be called, that might in turn, call a different SBasic "event" - causing re-entrancy in SBasic.

So, instead of calling the system event loop, we poll the event queue directly and disable the main event loop temporarily, passing the non-keyboard events to the routines that require them - bypassing the callback mechanism, protecting SBasic from re-entrant conditions. It is the rapid polling of the event queue that pushes the CPU up. Fortunately, because events are I/O bound, the event queue itself forces the process to relinquish the CPU if there is no event, allowing other processes to run nearly as rapidly as ever.

I know that this is probably a more technical explaination than we were hoping for... If Sesame were a general programming environment, like PureBasic, I would agree with you 100%. But, SBasic in Sesame is more a "responsive" environment, completely driven by the events that arrive in the Sesame windows.

In Sesame 1.1.x there is really only one command that directly interfaces directly with the low level (MSWindows or X11) events: @GetFromKeyboard. Sesame 2.0 has a number of commands that allow finer control, including one that passively polls events (completes if there are none), and another that allows an SBasic callback to be invoked based on a low level event's arrival.

Quote:
Best regards,
Daniel (from Germany)


Could you possibly use the on immediate change event on a LE instead of @GetFromKeyboard? By reading the value in the LE and then clearing it, you could find out what the user has typed without causing a busy wait. You would probably have to mark the other LEs as "read only" to prevent the user from setting focus in other LEs. But it wouldn't interfere with other events like mouse clicks, etc...
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
PianoMan
Member
Members
*
Offline


No personal text

Posts: 44
Joined: Oct 13th, 2004
Re: 100% CPU load while @GetFromKeyboard() waiting
Reply #5 - Dec 23rd, 2005 at 9:32pm
Print Post Print Post  
Mark,

I have seen three other scenarios that peg the CPU to 100%

1. Running any large report that takes minutes to complete.
2. Running any SBasic code that takes minutes to complete. (like looping through a massive array)
3. A XLookup that needs to look up massive amounts of data.

Is this what we need to expect?

Thanks,

Jon Mc
  
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: 100% CPU load while @GetFromKeyboard() waiting
Reply #6 - Dec 23rd, 2005 at 9:40pm
Print Post Print Post  
Any operation that does calculations as opposed to I/O, and is not sharing the CPU with other processes - will peg the CPU. This is correct and desirable behavior. Why calculate any more slowly than the machine is capable of?

This behavior is typical for any program on almost any OS.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
daniel
Member
*
Offline


No personal text

Posts: 7
Location: Germany
Joined: Dec 20th, 2005
Re: 100% CPU load while @GetFromKeyboard() waiting
Reply #7 - Dec 25th, 2005 at 7:38pm
Print Post Print Post  
Hello Mark,

Thank you very much for your detailed explanation. Even I am not familar with low level programming,  I got a good idea why the @GetFromKeyboard() commands causes the 100% CPU load.

Some people in our company have very few experience in using computers. For this people, it is easier to navigate by keystrokes, i.e. to press F10 to send an offer etc. By using keystroke navigation, it is also possible to display more data on the screen and to increase working speed.

So it is my target to allow the people to use my application by keyboard only. So it is not enough to parse the keystrokes while the user enters some data into the form. Do you agree?

Best regards,
Daniel (from Germany)
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: 100% CPU load while @GetFromKeyboard() waiting
Reply #8 - Dec 25th, 2005 at 7:47pm
Print Post Print Post  
Quote:
So it is not enough to parse the keystrokes while the user enters some data into the form. Do you agree?

Best regards,
Daniel (from Germany)


Depends how it is implemented. @GetFromKeyboard is very aggressive and can get in the way of normal operation. Essentially, your SBasic program will have to act as the main event loop for the application.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged