|
|

楼主 |
发表于 2008-4-4 14:41:00
|
显示全部楼层
Re: the Los Object Set C++ library(lowest version)
[Threading sample]
/********************************************************************
created: 2008/03/31
created: 31:3:2008 13:34
filename: {SOLUTION_DIRECTORY}\WaitableTimer\WaitableTimer.cpp
file path: {SOLUTION_DIRECTORY}\WaitableTimer
file base: WaitableTimer
file ext: cpp
author: Loserwang
purpose:
This is a part of the Los Object Set C++ library.
The library is distributed under GNU LGPL LICENSE Ver.3.
For further information, read the file "GNU LGPL V3.0.txt"
in the solution's root directory.
Add {Solution directory}\Los in %INCLUDE% evironment parameter,
and {Solution directory}\Lib in %LIB%, runtime files are not necessary.
*********************************************************************/
#include <los.h>
#include "resource.h"
using namespace los::types;
using namespace los::stringtypes;
using namespace los::framework;
using namespace los::controls;
using namespace los::mathtools;
using namespace los::multithreading;
using namespace los::systeminformation;
class MyWaitableTimer : public WaitableTimer
{
public:
InheritClass(MyWaitableTimer);
virtual void OnAPC(void* param_ptr_, ulong low_, ulong high_)
{
stringstream ss;
ss << t("TimerAPCRoutine, ThreadId = ") << GetCurrentThreadId() << std::endl;
ss << t("pArg = ") << param_ptr_ << std::endl;
ss << t("ulLowPart = ") << low_ << std::endl;
ss << t("ulHighPart = ") << high_ << std::endl << std::endl;
OutputDebugString(ss.str().c_str());
}
};
class WaitThread : public Thread<Window>
{
public:
InheritClass(WaitThread);
virtual ulong Run(pointer pWnd)
{
exit.Exchange(false);
while (!exit.Get())
{
wtmr.WaitForSingleObject();
DateTime dt;
if (!s_cs.Try())
continue;
pWnd->GetChildWindow(IDC_EDIT7)->text = dt.ToString();
s_cs.Leave();
}
return 0;
}
Interlocked exit;
MyWaitableTimer wtmr;
static CriticalSection s_cs;
};
CriticalSection WaitThread::s_cs;
class WTDialog : public Dialog
{
public:
InheritClass(WTDialog);
WTDialog()
: Dialog(IDD_DIALOG1)
, edt1(IDC_EDIT1)
, edt2(IDC_EDIT2)
, edt3(IDC_EDIT3)
, edt4(IDC_EDIT4)
, edt5(IDC_EDIT5)
, edt6(IDC_EDIT6)
, edt7(IDC_EDIT7)
, btnTrans(IDC_BUTTON1)
, btnOk(IDOK)
{}
virtual bool RealTime()
{
SleepEx(0, TRUE);
return true;
}
protected:
virtual void MessageMap()
{
Base::MessageMap();
Invoke(wm_initdialog, OnInitDialog);
Invoke(btn1.clicked(), OnBtnTrans);
Invoke(btnOk.clicked(), OnOk);
Invoke(wm_close, OnClose);
}
private:
LRESULT OnInitDialog(Window& def_focus, void* data_ptr)
{
CenterWindow();
los::controls::Color clr;
InitControls(this, &edt1, &edt2, &edt3, &edt4, &edt5, &edt6, &edt6, &edt7, &btnTrans, &btnOk, 0);
edt5.text = t("Y-M-D h:m:s");
edt6.text = t("M/D/Y h:m:s");
DateTime dt(DateTime::dtUTC);
thrd.wtmr.CreateWaitableTimer(t("C951C46C-8527-48ee-A08E-B583E66375E8"));
thrd.wtmr.SetWaitableTimerAPC(dt.ToInt64(), 1000, this);
stringstream ss;
ss << t("SetWaitableTimer, ThreadId = ") << GetCurrentThreadId() << std::endl;
ss << t("pArg = ") << this << std::endl;
ss << t("ulLowPart = ") << dt.ToLargeInteger().LowPart << std::endl;
ss << t("ulHighPart = ") << dt.ToLargeInteger().HighPart << std::endl << std::endl;
OutputDebugString(ss.str().c_str());
thrd.CreateThread(this);
return Base: roc();
}
LRESULT OnBtnTrans()
{
if (edt1.text == t(""))
{
MessageBox(t(" lease input date/time string"));
return 0;
}
DateTime dt(edt1.text, DateTime::dtLocal, edt5.text);
edt2.text = dt.ToString(edt6.text);
stringstream ss;
ss << dt.ToInt64();
edt3.text = ss.str();
ss.str(t(""));
dt.ConvertToUTC();
edt4.text = dt.ToString(edt6.text);
return 0;
}
LRESULT OnOk()
{
Close(idOk);
return 0;
}
LRESULT OnClose()
{
thrd.exit.Exchange(true);
thrd.s_cs.Enter();
while (thrd.WaitForSingleObject(0) == thrd.waitTimeOut)
SwitchToThread();
thrd.s_cs.Leave();
return Base::Proc();
}
private:
Edit edt1, edt2, edt3, edt4, edt5, edt6, edt7;
Button btnTrans, btnOk;
WaitThread thrd;
};
INT WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, INT)
{
WTDialog* pDlg = new WTDialog;
pDlg->Create(0);
pDlg->ShowWindow();
pDlg->UpdateWindow();
app->Initialize(pDlg);
return app->Run(0);
}
|
|