Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.

C++
#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;
}
Posted
Updated 1-Nov-11 5:20am
v3
Comments
CodingLover 1-Nov-11 0:38am    
Is this written by you?
Member 7766180 1-Nov-11 0:47am    
No, that is why I am asking for help.

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")
 
Share this answer
 
Comments
Member 7766180 1-Nov-11 11:46am    
Thank you Chuck. Will try as soon as I get back.
Member 7766180 3-Nov-11 11:27am    
OK, This works. Sorry couldn't get back sooner, Internet out for a bit, (the big winter storm). Here is what I have.

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

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
rename("c:\\PASS\\airport.zip", "c:\\Users\\DS\\Downloads\\airport.zip");
return 0;
}

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

Thank you Chuck.
Chuck O'Toole 3-Nov-11 11:47am    
Nope, that doesn't do what you asked. What is does is create the thread which immediately renames the file. The main code then waits 5 seconds before exiting. So you don't achieve the "wait 5 seconds before renaming the file"part of what you want.

Second, as a stand alone program, this has an undesirable side effect. When the main program exits, it will take the created thread with it, in other words, the thread does not survive beyond the exit of the main program. So, if you move the Sleep() to the thread as I suggested, you won't see the desired rename in this test program. If this is the entire program, then you have issues with how you want to delay the rename and have the main program exit.

If this is part of some larger program, then the problems are only in this test program.

I cannot give you the right solution as I do not really know your intent, I'm only pointing out things for you to watch out and/or plan for.
Member 7766180 3-Nov-11 12:36pm    
This is part of a larger program. When the program runs I want it to wait five seconds then move the file and exit. Right now, in the stand alone version it's moving the file and closing the console in five seconds, but I don't know if the file is being moved right away or waiting five seconds and then moving.
Chuck O'Toole 3-Nov-11 12:38pm    
Play computer with a pencil and paper. Follow the exact steps from the time you say "CreateProcess". Do you see anything in the "ThreadProc" that waits 5 seconds?
You can comment out the #include <iostream> Apart from that you need to keep the rest.
 
Share this answer
 
v2
Comments
Member 7766180 1-Nov-11 1:11am    
Hmmm! So is there a shorter way to write a timer that will move this file in lets say 5 aeconds?
CodingLover 1-Nov-11 1:17am    
You can write the same in many ways. But moving the file in 5 seconds, as you said is about the configuration parameter of your code.
XML
#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;
}
 
Share this answer
 
Comments
CodingLover 1-Nov-11 1:17am    
Nice, but not exist even after the file move. Isn't it? :)
Member 7766180 1-Nov-11 11:17am    
Here is the thing, First I need to set it to 5 seconds and second I need it to run while other code is running in the main program. In other words I don't want my main program to hang while this executes. So basically once this code is triggered, it waits five seconds, then moves the file without interferring with anything else, can this and is this being done here? Thank you.
Malli_S 2-Nov-11 0:59am    
CodingLover, Couldn't get your point.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900