Subject: is there a sleep in milliseconds?
> -----Original Message-----
> From: Jaime Teng [mailto:[EMAIL PROTECTED]]
>
> Is there a sleep function that would sleep in milliseconds?
>
> Actually, I simply like to make my perl script to run using less
> of the CPU resources and like to introduce some "noop" to
> free up some CPU%. Without it, the script runs too fast, it
> saturates the database server to a point that no other process
> could access the database server.
>
Sleeping for a duration of milliseconds is performed using Win32::Sleep( x
), where x is a value in milliseconds.

A script can change its execution priority. If the script process was
created using Win32::Process then you can call into the process object's
SetPriorityClass() method.
Otherwise the script can use the script below. The script works by passing
in a process ID and a priority class. The Pid can be any valid PID or "."
for the current running process (although I don't know why you would want
the nice.pl process to change process priority only to exit. If you want to
change the priority pass in the priority value as the second parameter (if
this param is not specified then the process's current priority is
displayed). The priority can be a numeric value or "normal", "high",
"realtime", "idle".

use Win32::API::Prototype;

my( $Pid, $Priority ) = @ARGV;

$PROCESS_QUERY_INFORMATION = 0x0400;
$PROCESS_SET_INFORMATION   = 0x0200;

%PRIORITY = (
  'normal'       =>  0x00000020,
  'idle'         =>  0x00000040,
  'high'         =>  0x00000080,
  'realtime'     =>  0x00000100,
);

ApiLink( "kernel32", "HANDLE OpenProcess(
                              DWORD dwDesiredAccess,
                              BOOL bInheritHandle,
                              DWORD dwProcessId )" ) || die;
ApiLink( "kernel32", "HANDLE GetCurrentProcess(VOID)" );
ApiLink( "kernel32", "int GetPriorityClass( HANDLE hProcess )" ) || die;
ApiLink( "kernel32", "BOOL SetPriorityClass(
                              HANDLE hProcess,
                              DWORD dwPriorityClass )" ) || die;
ApiLink( "kernel32", "BOOL CloseHandle( HANDLE hObject )" );

if( "." eq $Pid )
{
  $hProcess = GetCurrentProcess();  
}
else
{
  $hProcess = OpenProcess( $PROCESS_QUERY_INFORMATION |
$PROCESS_SET_INFORMATION,
                         0,
                         $Pid );
}
if( 0x00000000 != $hProcess )
{
  my $NewPriority;
  if( "" ne $Priority )
  {
    my $PriClass = $PRIORITY{lc $Priority} || $Priority;
    print "Setting priority to $Priority...";
    $Result = SetPriorityClass( $hProcess, $PriClass );
    if( $Result )
    {
      print "success\n";
    }
    else
    {
      print "failure. Error: " . Win32::FormatMessage( Win32::GetLastError()
) . "\n";
    }
  }
  $NewPriority = GetPriorityClass( $hProcess );
  foreach my $Pri ( keys( %PRIORITY ) )
  {
    if( $PRIORITY{$Pri} == $NewPriority )
    { 
      $NewPriority = $Pri;
      last;
    }
  }
  print "Process Priority: $NewPriority\n";  
  CloseHandle( $hProcess );
}
else
{
  print "Unable to access the process. Error: " . Win32::FormatMessage(
Win32::GetLastError() ) . "\n";
}


Dave Roth
http://www.roth.net/
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to