File: NonGuiThread.cpp

package info (click to toggle)
audacity 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 86,844 kB
  • sloc: ansic: 225,005; cpp: 221,240; sh: 27,327; python: 16,896; makefile: 8,186; lisp: 8,002; perl: 317; xml: 307; sed: 16
file content (84 lines) | stat: -rw-r--r-- 2,002 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*************************************************************************

  NonGuiThread.cpp

  James Crook
  (C) Audacity Developers, 2007

  wxWidgets license. See Licensing.txt

**********************************************************************//**

\class NonGuiThread
\brief NonGuiThread a thread class that allows non-GUI activities to
take place in the background without killing the GUI.

*//**********************************************************************/

#include <wx/wx.h>
#include <wx/apptrait.h>
#include "NonGuiThread.h"

bool NonGuiThread::IsLive=false;

NonGuiThread::NonGuiThread(tGenericFn pFn)
{
   mpFn = pFn;
   IsLive=true;
   mbExit = false;
}

NonGuiThread::~NonGuiThread()
{
   IsLive=false;
}

NonGuiThread::ExitCode NonGuiThread::Entry()
{
   // The while isn't needed here, but may be later if we break the function
   // up...
   while( !TestDestroy() && !mbExit  )
   {
      mbExit=true;
      (*mpFn)();
   }
   return (ExitCode)0;
}

// This one runs the function and only returns when function
// has run to completion.
void NonGuiThread::RunInThread(tGenericFn pFn)
{
   #ifdef WXMSW
   wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
   wxASSERT( traits );//"no wxAppTraits in RunInThread()?"

   void *cookie = NULL;
   // disable all app windows while waiting for the child process to finish
   cookie = traits->BeforeChildWaitLoop();
   #endif

   NonGuiThread * mpThread = new NonGuiThread(pFn);
   mpThread->Create();
   mpThread->Resume();
   wxLogDebug(wxT("Into the thread..."));
   while( mpThread->IsLive )
   {
      wxMilliSleep( 100 );
      //traits->AlwaysYield();
      wxTheApp->Yield();
   }
   #ifdef WXMSW
   traits->AfterChildWaitLoop(cookie);
   #endif
}

// This function starts the thread and returns immediately.
NonGuiThread * NonGuiThread::StartChild( tGenericFn pFn )
{
   NonGuiThread * pThread = new NonGuiThread(pFn);
   //pThread->mpFn = pFn;
   pThread->Create();
   pThread->Run();
   return pThread;
}