Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help creating a new thread and timer countown

Posted on 2006-06-03
6
Medium Priority
?
355 Views
Last Modified: 2013-11-20
Here is what I want to do in a Dialog Based Microsoft Visual C++ project:

 1) I want a function to execute every 10 minutes or sooner if the user clicks a button.
 2) I want the function to execute at start up, then start the 10 minute countdown to the next execution
 3) I want the dialog to show a countdown so the user knows how long it has been since the last execution
 4) I want the timer to start after the dialog is displayed (I think it needs to run in a seperate thread).

I am a solid php/asp programmer.  I have some experience with small c++ programs, but not so much with visual c++.  This is giving me huge headaches.  

I'd like some suggestions on how to display a countdown on the dialog.  I'm also not sure how to start the timer AFTER OnInitDialog, so the dialog displays AND the timer starts. Maybe a thread?

I have tried to start a new thread in the OnInitDialog, but haven't had any luck getting it to work.  I think the problem is that I can't run a thread on a member function.  I'm not sure how I can do this....need help.

HERE IS THE ERROR I'M GETTING:

error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (__cdecl *)(void *)'


Here is my code so far:


// nannynetworkcppDlg.cpp : implementation file
//

#include "stdafx.h"
#include <process.h>
#include "nannynetworkcpp.h"
#include "nannynetworkcppDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNannynetworkcppDlg dialog

CNannynetworkcppDlg::CNannynetworkcppDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CNannynetworkcppDlg::IDD, pParent)
{
      //{{AFX_DATA_INIT(CNannynetworkcppDlg)
            // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
      // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
      m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CNannynetworkcppDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CNannynetworkcppDlg)
            // NOTE: the ClassWizard will add DDX and DDV calls here
      //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNannynetworkcppDlg, CDialog)
      //{{AFX_MSG_MAP(CNannynetworkcppDlg)
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      ON_WM_LBUTTONDOWN()
      ON_BN_CLICKED(IDOK, OnUpload)
      ON_BN_CLICKED(IDUPLOAD, OnUpload)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNannynetworkcppDlg message handlers

BOOL CNannynetworkcppDlg::OnInitDialog()
{
      CDialog::OnInitDialog();

      // Set the icon for this dialog.  The framework does this automatically
      //  when the application's main window is not a dialog
      SetIcon(m_hIcon, TRUE);                  // Set big icon
      SetIcon(m_hIcon, FALSE);            // Set small icon
      
      // previous failed attempts:
      //static HANDLE hThrd;
        //hThrd = (HANDLE) _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) CNannynetworkcppDlg::runTimer, NULL, 0, 0);
      //hThrd = (HANDLE) CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) CNannynetworkcppDlg::runTimer, NULL, 0, 0);

        // current failed attempt
      CWinThread* pWinThread = AfxBeginThread(WorkerThreadProc);

        //call on startup
      CNannynetworkcppDlg::OnUpload();
      return true;
}

void CNannynetworkcppDlg::OnPaint()
{
      if (IsIconic())
      {
            CPaintDC dc(this); // device context for painting

            SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

            // Center icon in client rectangle
            int cxIcon = GetSystemMetrics(SM_CXICON);
            int cyIcon = GetSystemMetrics(SM_CYICON);
            CRect rect;
            GetClientRect(&rect);
            int x = (rect.Width() - cxIcon + 1) / 2;
            int y = (rect.Height() - cyIcon + 1) / 2;

            // Draw the icon
            dc.DrawIcon(x, y, m_hIcon);
      }
      else
      {
            CDialog::OnPaint();
      }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNannynetworkcppDlg::OnQueryDragIcon()
{
      return (HCURSOR) m_hIcon;
}

void CNannynetworkcppDlg::OnUpload()
{
      // TODO: Add your control notification handler code here
      MessageBox("uploaded", "" , MB_OK);
}

static UINT __cdecl  WorkerThreadProc( LPVOID Param )
{
      Sleep(10000);

        // can't call member funtion from here anyways...
      CNannynetworkcppDlg::OnUpload();
}

unsigned __stdcall CNannynetworkcppDlg::runTimer(LPVOID dummy)
{
      // TODO: Add your control notification handler code here
      dummy;
      Sleep(10000);
      CNannynetworkcppDlg::OnUpload();
      return NULL;
}


Any suggestions???



0
Comment
Question by:WF_Whistler
  • 3
  • 3
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16826141
Worker thread function must be static or global, this is the reason your code is not compiled.
However, according to your description, you don't need multithreading in this program. This can be solved using timer.
Add SetTimer call to OnInitDialog. Timer resolition should be equal to progeress interval: for example, if you need to update "Time remains" every second, resolution should be 1000 ms.
Add WM_TIMER handler to the dialog. In this function do all you need: update progress information, call function when it is time to do this.
If user executes this function pressing some button, execute it and reset time variable to 0.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16826144
Add class member:
DWORD m_dwTimeElapsed;
to the dialog. Set it initially to 0. Increment it in OnTimer function and use it for progress indication. Reset it to 0 when periodical function is executed.
0
 

Author Comment

by:WF_Whistler
ID: 16826551
Sweet.  I really didn't want to use threads.  They seem very complicated.

Are you able to give me a code example of the timer you speak of?  Sorry, I'm not a great c++ programmer.

Thanks!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 48

Accepted Solution

by:
AlexFM earned 1000 total points
ID: 16826587
h_file:

DWORD m_dwUploadTime;     // add this class member

cpp-file:

#define TIMER_ID 101

BEGIN_MESSAGE_MAP(CNannynetworkcppDlg, CDialog)
     //{{AFX_MSG_MAP(CNannynetworkcppDlg)
     ON_WM_TIMER()                                           // add this line - you can do this using MFC Wizard (add WM_TIMER message handler)
     ...
     //}}AFX_MSG_MAP
END_MESSAGE_MAP()


BOOL CNannynetworkcppDlg::OnInitDialog()
{
     CDialog::OnInitDialog();

     // ...

     //call on startup
     OnUpload();
     m_dwUploadTime = GetTickCount();   // add this line
     SetTimer(ID_TIMER, 1000, NULL);    // add this line

     return true;
}


void CNannynetworkcppDlg::OnTimer(UINT_PTR nIDEvent)
{
    if ( nIDEvent == ID_TIMER )
    {
           if ( GetTickCount() - m_dwUploadTime > 10 * 60 * 1000 )
           {
                 // 10 minutes elapsed from last upload
                 OnUpload();
                 m_dwUploadTime = GetTickCount();
           }
           else
           {
                 // Show here progress information: time elapsed from last Upload call
                 // You have this value in GetTickCount() - m_dwUploadTime (in ms)
                 // ...
           }
    }

    CDialog::OnTimer(nIDEvent);
}

void CNannynetworkcppDlg::OnButtonUpload()         // run by user command
{
       OnUpload();
       m_dwUploadTime = GetTickCount();
}
0
 

Author Comment

by:WF_Whistler
ID: 16828563
Thanks!  I'll see if I can get this working into my code tonight or tomorrow!
0
 

Author Comment

by:WF_Whistler
ID: 16829872
Worked great.  Thank you very much

Chris
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question