Program freezes with Sleep() - now i need some advice about a multi-threaded app 
Author Message
 Program freezes with Sleep() - now i need some advice about a multi-threaded app


Quote:
> OK i have an MFC app which is meant to do something every x hours
(roughtly
> 40 hours).  The program starts and the program runs for the first time
then
> it enters a sleep(40 hours in milliseconds) function.  At this point the
GUI
> becomes unresponsive - even to the point of being unable to minimize it to
> the system tray.

Why dont you use timers? You start the timer, after your period of time you
get a message and do your job.
Look at SetTimer() and KillTimer()

Rudolf



Sun, 17 Apr 2005 21:28:53 GMT  
 Program freezes with Sleep() - now i need some advice about a multi-threaded app
Wow, thats a stunning bit of code.  Thanks for taking the effort for
replying.
Is this method any better/worse than the SetTimer/KillTimer API's mentioned
above.

I'd like some indicator before I start coding tomorrow. <-- Planning, design
i've heard of those :-/

Max

Hope this thread gets archived on Google.


Quote:
> Hi Max



> >OK i have an MFC app which is meant to do something every x hours
(roughtly
> >40 hours).  The program starts and the program runs for the first time
then
> >it enters a sleep(40 hours in milliseconds) function.  At this point the
GUI
> >becomes unresponsive - even to the point of being unable to minimize it
to
> >the system tray.

> >I've been advised that a multi-thread concept in the code might be the
best
> >idea.

> Maybe this will help you:

> const UINT WM_TIME_ENDS = WM_APP + 1;

> class CLongTimer
> {
> public:
>                CLongTimer  ();
>                ~CLongTimer ();

>   void         Attach      (HWND hWnd);
>   void         StartTimer  (INT hours);

> private:
>   static UINT  ThreadProc  (LPVOID);
>   CWinThread*  mThread;
>   HANDLE       mStopThread;
>   HWND         mParent;
>   INT          mHours;
>   bool         mRunning;
> };

> CLongTimer::CLongTimer ()
> :  mThread     (NULL),
>    mStopThread (NULL),
>    mHwndParent (NULL),
>    mHours      (0),
>    mRunning    (false)
> {
>   mStopThread = CreateEvent (NULL, FALSE, FALSE, NULL);
>   assert (mStopThread != NULL);
> }

> CLongTimer::~CLongTimer ()
> {
>   if (mThread != NULL)
>   {
>     if (mRunning)
>     {
>       SetEvent (mStopThread);
>       WaitForSingleObject (mThread->m_hThread, INFINITE);
>     }
>     delete mThread;
>     mThread = NULL;
>   }
>   CloseHandle (mStopThread);
> }

> void CLongTimer::Attach (HWND mHwnd)
> {
>   mHwndParent = mHwnd;
> }

> void CLongTimer::StartTimer (UINT hours)
> {
>   if (mThread != NULL)
>   {
>     if (mRunning)
>       return;
>     else
>     {
>       delete mThread;
>       mThread = NULL;
>     }
>   }

>   mHours = hours;
>   assert (mHwndParent != NULL);

>   if (mThread == NULL)
>   {
>     mThread = AfxBeginThread (ThreadProc, this);
>     assert (mThread != NULL);
>   }
> }

> UINT CLongTimer::ThreadProc (LPVOID ptr)
> {
>   CTimer* me = static_cast<CTimer*> (ptr);
>   AfxGetThread()->m_bAutoDelete = FALSE;
>   me->mRunning = true;

>   while (true)
>   {
>     DWORD res = WaitForSingleObject (me->mStopThread, 3600000);
>     if (res == WAIT_OBJECT_0)
>       break;

>     me->mHours--;
>     if (me->mHours <= 0)
>     {
>       ::PostMessage (me->mHwndParent, WM_TIME_ENDS, 0, 0);
>       break;
>     }
>   }

>   me->mRunning = false;
>   return 0;
> }

> Within you GUI you need a handler for the message "WM_TIME_ENDS"
> (OnTimeEnds) and a member of the class CLongTimer (m_Timer).

> At some place you do:
>   m_Timer.Attach (GetSafeHwnd ());
>   m_Timer.StartTimer (40);

> After approx. 40 hours the handler OnTimeEnds will be called. Until
> then your GUI run's free.

> HTH

> Best Regards
> Walter

> PS. I wrote the code within the newsreader, maybe there are some
> errors ;-)



Sun, 17 Apr 2005 23:07:50 GMT  
 Program freezes with Sleep() - now i need some advice about a multi-threaded app
So you mean do my processing.  Start the timer with it set for 40 hours.
Then after 40 hours presumably Kill timer is called and here i could post a
message, say the same message that is generated when the user presses the
GO! button?

Do i understand this is what you are indicating?

Thanks
Max


Quote:


> > OK i have an MFC app which is meant to do something every x hours
> (roughtly
> > 40 hours).  The program starts and the program runs for the first time
> then
> > it enters a sleep(40 hours in milliseconds) function.  At this point the
> GUI
> > becomes unresponsive - even to the point of being unable to minimize it
to
> > the system tray.

> Why dont you use timers? You start the timer, after your period of time
you
> get a message and do your job.
> Look at SetTimer() and KillTimer()

> Rudolf



Sun, 17 Apr 2005 23:04:41 GMT  
 Program freezes with Sleep() - now i need some advice about a multi-threaded app
Hi Max



Quote:
>OK i have an MFC app which is meant to do something every x hours (roughtly
>40 hours).  The program starts and the program runs for the first time then
>it enters a sleep(40 hours in milliseconds) function.  At this point the GUI
>becomes unresponsive - even to the point of being unable to minimize it to
>the system tray.

>I've been advised that a multi-thread concept in the code might be the best
>idea.

Maybe this will help you:

const UINT WM_TIME_ENDS = WM_APP + 1;

class CLongTimer
{
public:
               CLongTimer  ();
               ~CLongTimer ();

  void         Attach      (HWND hWnd);
  void         StartTimer  (INT hours);

private:
  static UINT  ThreadProc  (LPVOID);
  CWinThread*  mThread;
  HANDLE       mStopThread;
  HWND         mParent;
  INT          mHours;
  bool         mRunning;

Quote:
};

CLongTimer::CLongTimer ()
:  mThread     (NULL),
   mStopThread (NULL),
   mHwndParent (NULL),
   mHours      (0),
   mRunning    (false)
{
  mStopThread = CreateEvent (NULL, FALSE, FALSE, NULL);
  assert (mStopThread != NULL);

Quote:
}

CLongTimer::~CLongTimer ()
{
  if (mThread != NULL)
  {
    if (mRunning)
    {
      SetEvent (mStopThread);
      WaitForSingleObject (mThread->m_hThread, INFINITE);
    }
    delete mThread;
    mThread = NULL;
  }
  CloseHandle (mStopThread);

Quote:
}

void CLongTimer::Attach (HWND mHwnd)
{
  mHwndParent = mHwnd;

Quote:
}

void CLongTimer::StartTimer (UINT hours)
{
  if (mThread != NULL)
  {
    if (mRunning)
      return;
    else
    {
      delete mThread;
      mThread = NULL;
    }
  }

  mHours = hours;
  assert (mHwndParent != NULL);

  if (mThread == NULL)
  {
    mThread = AfxBeginThread (ThreadProc, this);
    assert (mThread != NULL);
  }

Quote:
}

UINT CLongTimer::ThreadProc (LPVOID ptr)
{
  CTimer* me = static_cast<CTimer*> (ptr);
  AfxGetThread()->m_bAutoDelete = FALSE;
  me->mRunning = true;

  while (true)
  {
    DWORD res = WaitForSingleObject (me->mStopThread, 3600000);
    if (res == WAIT_OBJECT_0)
      break;

    me->mHours--;
    if (me->mHours <= 0)
    {
      ::PostMessage (me->mHwndParent, WM_TIME_ENDS, 0, 0);
      break;
    }
  }

  me->mRunning = false;
  return 0;

Quote:
}

Within you GUI you need a handler for the message "WM_TIME_ENDS"
(OnTimeEnds) and a member of the class CLongTimer (m_Timer).

At some place you do:
  m_Timer.Attach (GetSafeHwnd ());
  m_Timer.StartTimer (40);

After approx. 40 hours the handler OnTimeEnds will be called. Until
then your GUI run's free.

HTH

Best Regards
Walter

PS. I wrote the code within the newsreader, maybe there are some
errors ;-)



Sun, 17 Apr 2005 22:06:26 GMT  
 Program freezes with Sleep() - now i need some advice about a multi-threaded app
Hi Max



Quote:
>Is this method any better/worse than the SetTimer/KillTimer API's mentioned
>above.

It's not better or worse, it's only another approach ;-)

You asked for multithreading, so I thought you are interested in a
multithreading approach and this example is an easy start into
multithreading.

If you are really interested, the check out the book:

Win32 Multithreaded Programming
http://www.amazon.com/exec/obidos/tg/detail/-/1565922964/102-0186638-...

You may also download the sources from the book:
http://www.bearcanyon.com/mtbook/errata.htm#source

Best Regards
Walter



Mon, 18 Apr 2005 00:54:39 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. MFC UI Thread doesnt start when COM method called in multi-threaded MFC dialog App

2. MFC UI threads - application freezes when returning to main app

3. Return value in multi-threaded app?

4. Debugger for Multi-Threaded app.

5. How to build a Multi-Threaded App

6. DAO and Multi-threaded apps.

7. Visual Studio 6 locks up when debugging multi-thread app

8. How to build a Multi-Threaded App

9. Single file write with multi-threaded app

10. How to build a Multi-Threaded App

11. well designed multi-threading app

12. distributed multi-threaded App...

 

 
Powered by phpBB® Forum Software