Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3046

Program crashes after call to StopTimer (Sleep?)

$
0
0
EDIT: OK this method won't work.... Is there a timer routine that will return control back to VB once it is called? One where I can start it running and then stop it from running? Need a different approach.

Hi, I am making a crude timer to call a TimerProc function, will be shown below, to handle closing a System Dlg box. The purpose is to press a dlg box when the VB program is frozen from an HTML click to the external application since click freezes while there is a System Dlg box open.

It is a simple timer that is a loop until the stoptimer is called, shown below. The issue is that when StopTimer is called it gets to the first message box in the function but never gets to the second one. From each call of the library function it causes an error in the VBA code. When I get to the StopTImer is when the system locks up and and crashes.

Any idea why the StopTimer freezes? To me it seems as though the "Sleep" function is the problem.

StartTimer:

Code:

void EPIQ_init(const long MyWaitTime)
{
    bTimerOn = FALSE;
    lWait = MyWaitTime;

    MessageBox(NULL, L"Made it to init", L"We made it", NULL);

    return;
}

StopTimer:

Code:

void EPIQ_StopTimer()
{
    MessageBox(NULL, L"Made it to stopTimer", L"We made it", NULL);
    bTimerOn = false;
    Sleep(lWait);
    MessageBox(NULL, L"Made it to stopTimer made it to the end", L"We made it", NULL);
    return;
}

StartTimer: Simply enables the bTimerOn boolean.

Code:

void EPIQ_StartTimer()
{
    MessageBox(NULL, L"Made it to startTimer", L"We made it", NULL);
    if (bTimerOn = false)
    {
        bTimerOn = true;
        EPIQ_SetTimer();
    }
    return;
}

The worker of the timer

Code:

void EPIQ_SetTimer()
{
    // check to see if we'd overflow result or position
    if (bTimerOn)
    {
        MessageBox(NULL, L"Made it to SetTimer", L"We made it", NULL);
        Sleep(lWait);
        EPIQ_TimerProc();
        EPIQ_SetTimer();
    }
    return;
}

The worker, but never gets here

Code:

void EPIQ_TimerProc()
{

    //create two structures to hold our Main Window handle
    //and the Button's handle
    HWND WindowHandle;
    HWND ButtonHandle;

    MessageBox(NULL, L"Made it to TimerProc", L"We made it", NULL);

    //this window's caption is "File Download", so we search for it's handle using the FindWindow API               
    WindowHandle = FindWindow(NULL, L"Message from webpage");

    SetForegroundWindow(WindowHandle);

    //the Button's Caption is "OK" and it is a "Button".  SPYXX.exe that comes with Microsoft Visual Studio will reveal this information to you
    ButtonHandle = FindWindowEx(WindowHandle, 0, L"Button", L"OK");

    //send a message to the button that you are "clicking" it.  Surprisingly C++ understands what BM_CLICK is without having to set it.  Different than VB
    SendMessage(ButtonHandle, BM_CLICK, 0, 0);
   
    return;
}

Does anyone know why the "Sleep" may be locking up the system? (At least this is what it seems to me)

VB calling code.

Code:

    If (bUseLibrary = True) Then
        hinstLib = LoadLibrary("C:\Users\epperbx\source\repos\EPIQ Util DLL\Debug\EPIQUtil.dll")
 
            EPIQ_init (1000)
            EPIQ_StartTimer

*Do Timer Proc

stop timer

Code:

    If (bUseLibrary = True) Then
   
   
        EPIQ_StopTimer
       
'        ProcAddress = 0
'        ProcAddress = GetProcAddress(hinstLib, "EPIQ_StopTimer")

        hFreeLib = FreeLibrary(hinstLib)
    End If

Anyone, see the problem?

EDIT: here are the defenisions for the called function within VBA.

Code:

'Program Utility dll functions
Public Declare Function EPIQ_init Lib "EPIQUtil" (ByVal ulWait As Long)
Public Declare Function EPIQ_ExecuteTimer Lib "EPIQUtil" ()
Public Declare Function EPIQ_SetTimer Lib "EPIQUtil" ()
Public Declare Function EPIQ_StopTimer Lib "EPIQUtil" ()
Public Declare Function EPIQ_StartTimer Lib "EPIQUtil" ()


EDIT:
It is the Sleep call that is causing the program to crash. WHY? Also, when I remove the Sleep call the DLL freezes the VB program and it is not allowed to move on. Is this because of the loop, which I think it is. Is there another timer method that won't lock up the VB program?

Viewing all articles
Browse latest Browse all 3046

Trending Articles