Zachariah Rabatah
Zachariah Rabatah

Reputation: 312

Win32 C++ using HWND in separate file/thread to start timer

Background:

Application Type: Win32 Application

Language: C++ (with C functions as well)

Problem: Want to use main Window Handle in another file.

Update 1: Using a TCP server in another thread. This server receives information from a client and then needs to start a timer in the program.

Project Layout:

Main File: main.cpp/main.h which has WinMain, WndProc, etc.

Other Generated Files: Resource.h, main.rc, stdafx.h etc generated by Visual Studio

Self Made Files: functions.cpp/functions.h & calculation.cpp/calculation.h

Update 1: Server thread is in the main.cpp file and the call to start the timer is made on the server thread. I also updated some of the code to more accurately reflect what I have.

Info:

Can I call SetTimer(hwnd, TIMER_INT, TIMER_INTERVAL, NULL) in the calculation.cpp file in some way and make the TIMER_INT timer trigger in the WndProc for WM_TIMER?

So for example (of course foo is defined in calculation.h, etc. for other functions).

//calculation.cpp
void foo(HWND hwnd)
{
    SetTimer(hwnd, TIMER_INT, TIMER_INTERVAL, NULL);
}

//functions.cpp

void ThreadStart()
{
    /* This code initializes a working server that is visible to main.cpp */
    /* The Server socket and Accept socket are extern for main.cpp */
}

//main.cpp
HWND hwnd;
int WinMain(...)
{
    //... Set hwnd here
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, ...)
{
    static PARAMS params; //Thread params
    switch(message)
    {

    case WM_CREATE:
        //This initializes a blocking Server (which works)
        params.hwnd = hWnd;
        params.bContinue = TRUE;
        _beginthread(ThreadServer, 0, &params);
        break;

    case WM_TIMER:
        case TIMER_INT:
            MessageBox(NULL, L"Timer was triggered from foo", L"FOO", NULL);
            //continuous messageboxes will appear based on TIMER_INTERVAL if it works...
            break;
        break;

     }
}

void ThreadServer(PVOID pvoid)
{
    ThreadStart(); //calls accept() until client connects
    while(1)
    {
        memset(&RecvBuffer[0], 0, 512 * sizeof(RecvBuffer[0])); //Clear recv
        TCPServer.iRecv = recv(AcceptSocket, RecvBuffer, iRecvBuffer, 0);

        if(strlen(RecvBuffer) > 1){
            memset(&SendBuffer[0], 0, 512 * sizeof(SendBuffer[0]));
                //Clears SendBuffer
            std::string retString = "";
            retString = process(RecvBuffer); //processes RecvBuffer

                if(condition == true){
                    foo(hwnd);
                }

            if(strlen(retString.c_str()) > 0){
                TCPServer.iSend = send(AcceptSocket, retString.c_str(), strlen(retString.c_str()), 0);
            }else{
                retString = "";
                TCPServer.iSend = send(AcceptSocket, retString.c_str(), strlen(retString.c_str()), 0);
            }

            if(TCPServer.iSend == SOCKET_ERROR){
                break;
            }
        }
        //Determine if socket fails and breaks if failure occurs
        //*
        memset(&SendBuffer[0], 0, 512 * sizeof(SendBuffer[0]));
        TCPServer.iSend = send(AcceptSocket, SendBuffer, iSendBuffer, 0);
            if(TCPServer.iSend == SOCKET_ERROR){
             break;
        }//*/

        Sleep(1);

    }
}

The issue is trying to pass a reference to hwnd to calculation.cpp from the server thread. I can pass hwnd to the function foo(HWND), but the timer does not set. Is there a way to set a timer in a separate thread or is this not possible? Is there any other workaround to this with using winsock and a server?

Upvotes: 0

Views: 843

Answers (1)

Rita Han
Rita Han

Reputation: 9710

As the document state that you can't create a timer for a window from a different thread. For you the different thread is the server thread.

Maybe you can post WM_TIMER message (PostMessage) to the main thread from the server thread when the timer timeout.

Or you need Synchronization Objects for threads synchronicity.

Upvotes: 1

Related Questions