Click here to Skip to main content
15,922,155 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: What is this error msg? [solved] Pin
David Crow30-Sep-10 8:51
David Crow30-Sep-10 8:51 
QuestionCDialog DoModal function [Moved] Pin
prithaa30-Sep-10 1:48
prithaa30-Sep-10 1:48 
AnswerRe: CDialog DoModal function Pin
Richard MacCutchan30-Sep-10 2:27
mveRichard MacCutchan30-Sep-10 2:27 
GeneralRe: CDialog DoModal function Pin
prithaa1-Oct-10 1:42
prithaa1-Oct-10 1:42 
GeneralRe: CDialog DoModal function Pin
Richard MacCutchan1-Oct-10 2:35
mveRichard MacCutchan1-Oct-10 2:35 
GeneralRe: CDialog DoModal function Pin
prithaa1-Oct-10 17:20
prithaa1-Oct-10 17:20 
GeneralRe: CDialog DoModal function Pin
Richard MacCutchan1-Oct-10 23:05
mveRichard MacCutchan1-Oct-10 23:05 
GeneralRe: CDialog DoModal function Pin
prithaa2-Oct-10 0:17
prithaa2-Oct-10 0:17 
GeneralRe: CDialog DoModal function Pin
Richard MacCutchan2-Oct-10 1:11
mveRichard MacCutchan2-Oct-10 1:11 
GeneralRe: CDialog DoModal function Pin
prithaa2-Oct-10 2:06
prithaa2-Oct-10 2:06 
GeneralRe: CDialog DoModal function Pin
Richard MacCutchan2-Oct-10 4:12
mveRichard MacCutchan2-Oct-10 4:12 
GeneralRe: CDialog DoModal function Pin
prithaa3-Oct-10 2:09
prithaa3-Oct-10 2:09 
GeneralRe: CDialog DoModal function Pin
Richard MacCutchan3-Oct-10 3:15
mveRichard MacCutchan3-Oct-10 3:15 
GeneralRe: CDialog DoModal function Pin
prithaa3-Oct-10 23:24
prithaa3-Oct-10 23:24 
GeneralRe: CDialog DoModal function Pin
Richard MacCutchan4-Oct-10 0:44
mveRichard MacCutchan4-Oct-10 0:44 
GeneralRe: CDialog DoModal function Pin
prithaa4-Oct-10 1:18
prithaa4-Oct-10 1:18 
GeneralRe: CDialog DoModal function Pin
Richard MacCutchan4-Oct-10 3:21
mveRichard MacCutchan4-Oct-10 3:21 
GeneralRe: CDialog DoModal function Pin
prithaa4-Oct-10 5:19
prithaa4-Oct-10 5:19 
GeneralRe: CDialog DoModal function Pin
Richard MacCutchan4-Oct-10 5:41
mveRichard MacCutchan4-Oct-10 5:41 
GeneralRe: CDialog DoModal function Pin
prithaa4-Oct-10 6:20
prithaa4-Oct-10 6:20 
GeneralRe: CDialog DoModal function Pin
prithaa4-Oct-10 6:21
prithaa4-Oct-10 6:21 
QuestionAssign value to a variable after a delay Pin
manoharbalu29-Sep-10 19:32
manoharbalu29-Sep-10 19:32 
AnswerRe: Assign value to a variable after a delay Pin
Cedric Moonen29-Sep-10 20:13
Cedric Moonen29-Sep-10 20:13 
AnswerRe: Assign value to a variable after a delay [modified] Pin
Eugen Podsypalnikov29-Sep-10 20:48
Eugen Podsypalnikov29-Sep-10 20:48 
Take it Smile | :) :
#define TICK_EXPIRED(dwActTick, dwToTick) ((long)(dwToTick - dwActTick) <= 0)
#define TICK_ELAPSED(dwTime) TICK_EXPIRED(GetTickCount(), dwTime)

template <typename T>
class DelaySetVar
{
  CCriticalSection m_cLocalLock;
  T                m_value;

  CCriticalSection              m_cMapLock;
  volatile bool                 m_bPleaseExitNow;
  CMap<DWORD,DWORD,DWORD,DWORD> m_mapLiveThreads;

  class SetThreadParam
  {
    friend class DelaySetVar;

    DelaySetVar*      m_pcVarObject;
    const T           m_SetValue;
    DWORD             m_dwDelay;

  public:
    SetThreadParam(DelaySetVar* pcVarObject,
                   const T& set_value,
                   DWORD dwMsDelay)
      : m_pcVarObject(pcVarObject),
        m_SetValue(set_value),
        m_dwDelay(dwMsDelay) {};
  };

  static UINT AFX_CDECL ValueSetProc(void* pParam)
  {
    SetThreadParam* pSetParam = (SetThreadParam*) pParam;
    if (pSetParam) {
      DWORD dwTimeOut = GetTickCount() + pSetParam->m_dwDelay;
      while (!pSetParam->m_pcVarObject->m_bPleaseExitNow) {
        if (TICK_ELAPSED(dwTimeOut)) {
          CSingleLock cLock(&pSetParam->m_pcVarObject->m_cLocalLock, TRUE);
          pSetParam->m_pcVarObject->m_value = pSetParam->m_SetValue;
          break;
        }
        Sleep(1);
      }
      pSetParam->m_pcVarObject->ThreadEndEvent(GetCurrentThreadId());
      delete pSetParam;
    }
    return 0;
  };

  void ThreadBegEvent(DWORD dwThreadID)
  {
    CSingleLock cLock(&m_cMapLock, TRUE);
    m_mapLiveThreads.SetAt(dwThreadID, dwThreadID);
  };

  void ThreadEndEvent(DWORD dwThreadID)
  {
    CSingleLock cLock(&m_cMapLock, TRUE);
    m_mapLiveThreads.RemoveKey(dwThreadID);
  };

public:
  // Constructors do set the values without any delay
  DelaySetVar()
    : m_value(),
      m_bPleaseExitNow(false) {};

  DelaySetVar(const T& init)
    : m_value(init),
      m_bPleaseExitNow(false) {};

  // Destructor should wait for the exiting of all threads
  ~DelaySetVar()
  {
    m_bPleaseExitNow = true;
    while (true) {
      {
        CSingleLock cLock(&m_cMapLock, TRUE);
        if (!m_mapLiveThreads.GetCount()) {
          break;
        }
      }
      Sleep(1);
    }
  }

  // Access functions, must be synchronized
  // since the delayed sets are possible

  const T& GetValue() {
    CSingleLock cLock(&m_cLocalLock, TRUE);
    return m_value;
  };

  void SetValue(const T& set_value, DWORD dwMsDelay = 0)
  {
    if (!m_bPleaseExitNow) {
      if (dwMsDelay) {
        CWinThread* pcThread = ::AfxBeginThread(ValueSetProc,
                                                new SetThreadParam(this,
                                                                   set_value,
                                                                   dwMsDelay),
                                                0, CREATE_SUSPENDED);
        if (pcThread) {
          pcThread->m_bAutoDelete = true;
          ThreadBegEvent(pcThread->m_nThreadID);
          pcThread->ResumeThread();
        }
      } else {
        CSingleLock cLock(&m_cLocalLock, TRUE);
        m_value = set_value;
      }
    }
  };
};
Possible usage Smile | :) :
{
  DelaySetVar<int> iVar(3);           // Init without delay
  iVar.SetValue(2, 2000);             // Set with delay
  do {
    cout << iVar.GetValue() << '\n';
  } while (3 == iVar.GetValue());
  cout << iVar.GetValue() << '\n';
  iVar.SetValue(3);                   // Set without delay
  cout << iVar.GetValue() << '\n';
  iVar.SetValue(4, 2000);             // Set with delay,
                                      // But note: iVar have to die now... :)
}

Of course, the used T must support operator= Smile | :)
They sought it with thimbles, they sought it with care;
They pursued it with forks and hope;
They threatened its life with a railway-share;
They charmed it with smiles and soap. Smile | :)
modified on Thursday, September 30, 2010 6:00 AM

AnswerRe: Assign value to a variable after a delay Pin
Aescleal29-Sep-10 20:52
Aescleal29-Sep-10 20:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.