Solved

Wake Computer from standby

Posted on 2004-04-14
4
2,475 Views
Last Modified: 2013-12-03
I want to add programmatically a new task to the task scheduler, which has the option wake computer enabled. I already know how to add a task to the scheduler but I'm not able to eneable the option wake computer. Does anyone how I can do this? Please give me an examlpe in any of the folleowing languages: C++, C#, Delphi, VB, VBScript, Batch-file

Alternatively, I could also live with some other code which wakes the computer at a certain time from standby (using other timers).

0
Comment
Question by:maverick65
  • 3
4 Comments
 
LVL 3

Expert Comment

by:MikProg
ID: 10840931
The only reason to add task to task sheduler is that: User want manage tasks shedule only by task sheduler. When I try to solve same problem the choice was Waitable Timers. Task sheduler REQUIRES user MUST have logon password. But I want shedule tasks send computer to sleep or suspend mode without logging off. AND I NEVER USE PASSWORD on my computer.
The choice Waitable Timers (see Windows API help ). Now I'm on distance from my source codes. But if you interested notice me about and I send you source example.
0
 
LVL 4

Author Comment

by:maverick65
ID: 10841535
I think that you are on the right way. I already have the example from MSDN, but further source code would be helpful.
0
 
LVL 3

Expert Comment

by:MikProg
ID: 10841573
Wait awhile. I will be back monday. If I'll be late I loose my points :(
0
 
LVL 3

Accepted Solution

by:
MikProg earned 500 total points
ID: 10856677
Here it is. It is part of project that never ends so source is not clean. The main Idea is covered by 2 proc's TimerAPCProc and Execute. As you can see main program must create one thread per waitable event, althought another approach is to use WaitForSingle/MultipleObjects.

unit iuStartAlerter;

interface

uses
  Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};

type
  AlertThread = class(TThread)
  private
    FAwakeningAt: TSystemTime;
    Fth: Thandle;
    FSheduleObject: pointer;
    procedure SetName;
    procedure SetAwakeningAt(const Value: TSystemTime);
    procedure SetSheduleObject(const Value: pointer);
  protected
    procedure Execute; override;
  public
    property th: Thandle read Fth;
    property AwakeningAt: TSystemTime read FAwakeningAt write SetAwakeningAt;
    property SheduleObject: pointer read FSheduleObject write SetSheduleObject;
    procedure MakeAction;
  end;

implementation

uses
  iuStartForm, SysUtils;

{$IFDEF MSWINDOWS}
type
  TThreadNameInfo = record
    FType: LongWord;     // must be 0x1000
    FName: PChar;        // pointer to name (in user address space)
    FThreadID: LongWord; // thread ID (-1 indicates caller thread)
    FFlags: LongWord;    // reserved for future use, must be zero
  end;
{$ENDIF}

{ AlertThread }

procedure TimerAPCProc(Subj: pointer;
  dwTimerLowValue: DWORD; dwTimerHighValue: DWORD); stdcall
begin
  with AlertThread(Subj) do
    Synchronize(MakeAction);
end;

procedure AlertThread.SetName;
{$IFDEF MSWINDOWS}
var
  ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
  ThreadNameInfo.FType := $1000;
  ThreadNameInfo.FName := 'iuStartAlerter';
  ThreadNameInfo.FThreadID := $FFFFFFFF;
  ThreadNameInfo.FFlags := 0;

  try
    RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
  except
  end;
{$ENDIF}
end;

procedure AlertThread.SetSheduleObject(const Value: pointer);
begin
  FSheduleObject := Value;
end;

procedure AlertThread.MakeAction;
begin
  Form1.Startiu();
end;

procedure AlertThread.SetAwakeningAt(const Value: TSystemTime);
begin
  FAwakeningAt := Value;
end;

procedure AlertThread.Execute;
var
  ft, ft1: TFileTime;
  st: TSystemTime;
  c: Int64;
begin
  SetName;
  th:=CreateWaitableTimer(nil,True,'Local\iuStartAlerter');
  SystemTimeToFileTime(FAwakeningAt,ft);
  DateTimeToSystemTime(now,st);
  SystemTimeToFileTime(st,ft1);
  c:=Int64(ft1)-Int64(ft);
  if SetWaitableTimer(th,c,0,@timerAPCProc,Self,True) then
    while not Terminated do
      SleepEx(1000,True);
  CloseHandle(th);
  while not Terminated do
    SleepEx(1000,True);
end;

end.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Event ID 10010 3 69
wordpress limitations 4 124
How to deal with "Consider using Code First Migrations to update the database" ? 7 323
Best book to learn C++ 4 84
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question