减少代码\独立计时器 [英] Reducing Code \ Independent Timer

查看:93
本文介绍了减少代码\独立计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想摆脱所有不必要的代码.我只想在计时器上移动文件.我不需要消息我可以在这段代码中摆脱什么呢?全部都可以,我只想将其修剪掉.这段代码是否也独立于我在程序中可能拥有的其他代码而工作,还是使程序停止运行直到执行?谢谢.

I would like to get rid of any unneccesary code. I only want to move the file on the timer. I don''t need a message. What can I get rid of in this code to do so? It''s all working I just want to trim it down. Also does this code work independently of other code that I may have in a program, or does it stall the program until it executes? Thank you.

#include "stdafx.h"
#include <time.h>
#include <windows.h>
#include <iostream>

VOID CALLBACK TimerProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_TIMER message
UINT_PTR idEvent, // timer identifier
DWORD dwTime // current system time
)
{
//printf("Die, ****in' world\n");
rename("c:\\PASS\\airport.zip", "c:\\Users\\DS\\Downloads\\airport.zip");
}

DWORD WINAPI ThreadProc(
LPVOID lpParameter // thread data
)
{
UINT uiTimer = ::SetTimer(NULL, 12354, 100, TimerProc);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
    DispatchMessage(&msg);
    }
return 0;
}

int main()
{
HANDLE hThread = ::CreateThread(NULL, NULL, ThreadProc, NULL, NULL, NULL);
CloseHandle(hThread);
::Sleep(10000);
return 0;
}

推荐答案

不要让Windows计时器和消息泵使您的生活复杂化,您不需要它们.

如果您创建一个单独的线程来进行重命名",则只需让该线程使用Sleep(5000)等待5秒钟,然后再执行它的工作即可.就这么简单.

基本上,您复制的代码会创建一个线程,该线程启动计时器以通知事件操作例程该计时器已过期.由于您没有编写代码,因此将调试数天.保留"CreateThread"部分和"ThreadProc()"定义,将其他所有内容都丢弃(好吧,不要扔掉"includes")
Don''t complicate your life with Windows Timers and Message Pumps, you don''t need them.

If you create a separate thread to do the "rename", then just have that thread use Sleep(5000) to wait 5 seconds before doing it''s work. Simple as that.

Basically, the code you copied creates a thread that starts a timer to notify an event action routine that the timer has expired. You''ll be debugging that for days since you didn''t write it. Keep the "CreateThread" part and the "ThreadProc()" definition, throw everything else away (well, not the "includes")


您可以注释掉#include< iostream>除此之外,您还需要保留其余部分.
You can comment out the #include <iostream> Apart from that you need to keep the rest.


#include "stdafx.h"
#include <time.h>
#include <windows.h>
#include <iostream>

VOID CALLBACK TimerProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_TIMER message
UINT_PTR idEvent, // timer identifier
DWORD dwTime // current system time
)
{
    //printf("Die, ****in' world\n");
    rename("c:\\PASS\\airport.zip", "c:\\Users\\DS\\Downloads\\airport.zip");
}

int main()
{

    UINT uiTimer = ::SetTimer(NULL, 12354, 100, TimerProc);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        DispatchMessage(&msg);
    }

    return 0;
}


这篇关于减少代码\独立计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆