稳定盈利的期货交易方法-量化趋势交易

alantop -专业量化投资者

爱好:量化投资,逆向工程,渗透
随笔 - 595, 文章 - 0, 评论 - 921, 引用 - 0
数据加载中……

利用控制台下定时器定时关机代码示例

 定时关机,用srvany安装成服务使用。



示例代码工程,基于vs2010
/Files/alantop/shutoff.rar


// shutoff.cpp : 定义控制台应用程序的入口点。
//

#include 
"stdafx.h"
#include 
<atltime.h>
#include 
<windows.h> 
#include 
<stdio.h> 
#include 
<conio.h> 

void shutdown()
{
    
//从config中读取关机参数

    CString cs 
=  "shutoff";
    CString hour 
= "hour";
    CString filename 
= ".\\config.ini";
    
int offHour =::GetPrivateProfileInt(cs,hour,0,filename);
    CString min 
= "min";
    
int offMin = ::GetPrivateProfileInt(cs,min,0,filename);

    
//判断时间并关机

    CTime t
=CTime::GetCurrentTime(); 
    CTime t1(t.GetYear(),t.GetMonth(),t.GetDay(),offHour,offMin,t.GetSecond());
    CTimeSpan ts 
= t1 - t;

    printf(
"还有%d小时%d分钟关机.\n", ts.GetHours(),ts.GetMinutes() );

    
int nHour = t.GetHour();
    
int nMin = t.GetMinute();

    
char command[] ="c:\\windows\\system32\\shutdown.exe -s -t 60";
    
if (nHour == offHour && nMin == nHour)
        system(command);
}

VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime) 

    
//printf("定时器函数开始工作\n"); 
    shutdown();


DWORD CALLBACK Thread(PVOID pvoid)    
{   
    BOOL  bRet; 
    MSG  msg; 
    PeekMessage(
&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE); 

    UINT  timerid
=::SetTimer(NULL,111,10000,TimerProc); 
    
while ((bRet = GetMessage(&msg,NULL,0,0))!=0)   
    {     
        
if (bRet==-1
        { 
            
//   handle   the   error   and   possibly   exit   
        }   
        
else 
        { 
            TranslateMessage(
&msg);     
            DispatchMessage(
&msg);     
        } 
    } 
    KillTimer(NULL,timerid);   
    printf(
"线程函数结束\n" ); 
    
return 0



int _tmain(int argc, _TCHAR* argv[])
{
    CString cs 
=  "shutoff";
    CString hour 
= "hour";
    CString filename 
= ".\\config.ini";
    
int offHour =::GetPrivateProfileInt(cs,hour,0,filename);
    CString min 
= "min";
    
int offMin = ::GetPrivateProfileInt(cs,min,0,filename);

    DWORD   dwThreadId;   
    printf(
"定时关机程序启动,计算机将在%d小时%d关机。\n",offHour, offMin );   
    HANDLE hThread 
= CreateThread(NULL,0,Thread,0,0,&dwThreadId); 
    _getch();
    
return 0;
}

 

posted on 2012-07-01 13:13 AlanTop 阅读(1881) 评论(2)  编辑 收藏 引用 所属分类: VC++

评论

# re: 利用控制台下定时器定时关机代码示例  回复  更多评论   

首先说一下,程序写的很好。但是容错处理上还应该加强。比如config.ini文件不存在的情况等等。
2012-07-02 17:19 | DavidXu

# re: 利用控制台下定时器定时关机代码示例  回复  更多评论   

采用线程与不采用线程直接通过shutdown函数进行处理有什么区别不?谢谢
2012-07-03 11:42 | 钟谢伟

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理