|
I am not using MFC; just pure win32 at the moment. Is there something like a font dialog box? If there is then how to I call it and set my font after disposing the dialog...?
|
|
|
|
|
Use the ChooseFont() API to show the common font chooser dialog.
|
|
|
|
|
Font Dialog Box[^]
CreateFontIndirect() can be used to create a font from the LOGFONT obtained with the dialog.
For setting a control's font there's WM_SETFONT.
SelectObject() can select a font into a device context.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
hi, i wrote a mfc dialog that's running a worker thread i want to terminate it from outside.
in CMyTestDlg: public CDialog{
static bool STOP;//a shared variable to signal the thread function
static UINT __cdecl Start();
CWinThread* pThread
....
in the cpp file, i have a OnBnClickedButtonStart() and OnBnClickedButtonStop()function to start and stop the thread
bool CMyTestDlg::Stop=false;
void CMyTestDlg::OnBnClickedButtonStart(){
pThread=AfxBeginThread(Start,this,-1,0,CREATE_SUSPENDED);
pThread->m_bAutoDelete=FALSE;
pThread->ResumeThread();
}
void CMyTestDlg::OnBnClickedButtonStop()
{
STOP=true;
if(::WaitForSingleObject(pThread->m_hThread,2000)==WAIT_OBJECT_0){
delete pThread;
m_ListBox_MSG.AddString(_T("Thread terminated succesfully"));
//message showed in a list box
}
else{
::TerminateThread(pThread->m_hThread,0);
m_ListBox_MSG.AddString(_T("Thread is not normally terminated!"));
//message showed in a list box
}
}
UINT __cdecl CMyTestDlg::Start(LPVOID pParam){
CMyTestDlg* me=(CMyTestDlg*)pParam;
while(STOP==false){
//looping here...
}
return 0;
}
when i stop the thread, from the message showed in the listbox, i found that the thread is always terminated "dangeroursly" which means the ::TerminateThread()function ran.
What could be the possible cause?
|
|
|
|
|
What happens if you wait longer?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
i tried for 10s, still doesn't terminate properly.
the looping is fairly simple, should take that long
|
|
|
|
|
another thing,in the thread function, i add the msg to listbox before return
UINT __cdecl CMyTestDlg::Start(LPVOID pParam){
CMyTestDlg* me=(CMyTestDlg*)pParam;
while(STOP==false){
//looping here...
}
me->m_ListBox_MSG.AddString(_T("ERROR!"));
return 0;
}
if the thread is terminated properly, this msg should show, how ever it didn't, this possibly means that the loop didn't exit...what's wrong?
|
|
|
|
|
How about setting the wait time to INFINITE and putting a breakpoint at the end of the thread proc
(maybe on the "return 0;").
Does the breakpoint ever get hit? If not, why?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
just tried your suggestion, the "return 0" never get hit, and dialog is stuck ,something weird is going on here
|
|
|
|
|
Mark Salsbery wrote: What happens if you wait longer?
They call your number and you pick up your Filet-O-Fish
|
|
|
|
|
lol,seriously, i think my thread loop didn't exit even i made STOp=true; what's going here?
|
|
|
|
|
alberthyc wrote: what's going here?
Sadly the same thing that goes on here everyday
|
|
|
|
|
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am guessing that you might have to make your STOP boolean a volatile.
I am guessing that the compiler has optimised it out of the while condition because it is not changed by the thread.
|
|
|
|
|
|
try...
//in header
volatile long m_iLock;
//global on top of cpp file
static volatile long *piLock;
//in OnBnClickedButtonStart
m_iLock =1;
piLock = &m_iLock;
AfxBeginThread(...
in the thread..
while(InterlockedCompareExchange(piLock,1,1))
{
//work
}
AfxMessageBox("Ending"...
And change m_bAutoDelete to true.
Tell me the results.
-Randor
|
|
|
|
|
sorry, what does this do??
|
|
|
|
|
I don't really use CWinThread but using my own thread class this works
class TestThread : public base_w32thread
{
private:
bool _brun;
protected:
virtual void run()
{
while(_brun)
{
TRACE("Thread is running\r\n");
Sleep(2000);
}
}
public:
void stop()
{
_brun = false;
}
};
// from the CMainDlg.h class declaration
protected:
HICON m_hIcon;
TestThread _thread;
// from the CMainDlg.cpp implementation
void CMainDlg::OnBnClickedButtonstart()
{
_thread.start();
}
void CMainDlg::OnBnClickedButtonstop()
{
_thread.stop();
if( WAIT_OBJECT_0 == ::WaitForSingleObject( _thread, 3000))
AfxMessageBox(_T("Thread exited"));
else
AfxMessageBox(_T("This did not happen"));
}
|
|
|
|
|
|
Mark Salsbery wrote: I had a filet-o-fish for lunch!
The down side to my adaptation is I don't think I could eat one now
|
|
|
|
|
interesting.....
if i really can't get the thread working, i'll try this method
|
|
|
|
|
I tried your code in a simple dialog project and it works.
In your original post you state that it is looping with no explanation as to what it is doing.
I put an intensive processor loop in there and tried and got a termination using the TerminateThread which is what you describe.
Because your WaitSingleObject specifies that you should only wait 2 seconds for the object to be signalled, it may be because your thread function is not terminating within 2 seconds it is returning a WAIT_TIMEOUT and as you only check for a WAIT_OBJECT_0 you assume that something has failed which is incorrect.
All that has happened in effect is the you got tired of waiting for it to complete and decide to kill it prematurely by incorrectly assuming that something must be wrong.
If your thread is not being signalled within the 2 seconds you specify I would assume that the problem lies in your thread function in that it takes too long to complete each pass through the while loop.
However without knowing what goes on in the loop it would be difficult to provide further guidance.
|
|
|
|
|
i tired for 10 seconds and i have a counter in my loop which starts from zero and with in few seconds, the loop looped about 1000 times, so i guess i shouldn't take that long to terminate
|
|
|
|
|
I think if he tried the code I posted, the wait timeout would not occur. Essentially what is probably happening is that the value of the BOOL is being retrieved from L1 or L2 cache because the thread is doing nothing else to the stack/registers. The atomic operation would prevent the cache hit.
-Randor (David Delaune)
|
|
|
|
|
tried your code,but i don't really understand this part:
while(InterlockedCompareExchange(piLock,1,1))
what does it do? and I need to manually terminate the thread, but this while loop doesn't seem to do that...
|
|
|
|