CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2009
    Posts
    32

    Program crashes after call to StopTimer (Sleep?)

    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?
    Last edited by funkmonkey; March 9th, 2025 at 04:13 PM.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,631

    Re: Program crashes after call to StopTimer (Sleep?)

    Just take a look at the demo I prepared. Maybe it can help you, because all the previous variants you came up with was a mess.

    The idea of the demo is the following. The popper.exe imitates your web page producing message boxes. The clicker.exe is to invoke periodically a finding-clicking routine. All the demo is in C/C++, but this is not important, as I followed your initial idea of the C++ code in dll. You build all the demo by running make.bat in VS Command Prompt. And you run both executables in separate console windows each and watch message boxes coming and going.

    As I said, I hope this helps.

    funk.zip
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured