|
WARNING: I don't have VS on this machine, I've typed the code out manually and I've no way to compile it. I've verified whatever I can, but please make sure that it works fine before you use it. Use it at your own risk, keep out of reach of children, do not expose to sunlight, etc.,
Global handle variables to store the event:
HANDLE hDieNow, hImDead;
Function where you'd create the events (preferably InitDialog or something like that):
CMyDialog::OnInitDialog()
{
hDieNow = CreateEvent(NULL, TRUE, FALSE, NULL);
hImDead = CreateEvent(NULL, TRUE, FALSE, NULL);
return TRUE;
}
Thread function:
UINT ThreadFunc(LPVOID pFuncParams)
{
while(true)
{
if(WaitForSingleObject(hDieNow, 0) == WAIT_OBJECT_0)
break;
else
{
Sleep(100);
OutputDebugString(_T("Thread probe...\n"));
}
}
SetEvent(hImDead);
return false;
}
Application close handler function:
CMyDialog::OnClose()
{
SetEvent(hDieNow);
DWORD dwResult = WaitForSingleObject(hImDead, 5000);
}
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
thanks. I am using SDI application, so i will put the code in BOOL OnNewDocument() instead of OnInitDialg(). Now a general question: why can i not use ON_WM_CLOSE() to tell the small thread to terminate himself and if yes how? Thanks a lot
|
|
|
|
|
susanne1 wrote: I am using SDI application, so i will put the code in BOOL OnNewDocument() instead of OnInitDialg().
Do it wherever. You just need to create those events, before you would use it. But be sure on that, or the WaitFor... call is going to fail and you'll not get desired results.
susanne1 wrote: Now a general question: why can i not use ON_WM_CLOSE() to tell the small thread to terminate himself and if yes how?
What do you mean how? I just demonstrated that to you with an example in my previous post. Just set the event that the worker thread is spinning on (waiting to be set) and then wait for the worker thread to set the "I'm dead" event (which is kind of an acknowledgment from the thread that the cleanup is complete).
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
As you've shown this code, here's a potential solution:
- Ensure there is a
volatile LONG variable (let's call it lTerminate) visible to both your main thread's code and your second thread's code. Initialise that variable to 0 before the second thread is started - When you start the second thread, store its handle somewhere it can be seen by the code that will terminate the thread.
- The second thread's loop should look like this:
bool PrintMy Results(const CString& strMyResult)
{
while(!lTerminate && nCount < arrMyResults.GetSize())
{
nCount++;
}
} - When you want to exit, set lTerminate to 1 with an appropriate function (as I'll show you below) and wait for the thread's handle to be set, as that indicates the thread has exitted:
::InterlockedExchange(&lTerminate, 1);
::WaitForSingleObject(hSecondThread, INFINITE);
Easy as...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Sweet! Have my vote, sir.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
When asked to close, the primary thread needs to signal an event that the secondary thread is monitoring. Once signaled, the primary thread would then wait for the secondary thread to finish. This is very easy to do with two CEvent objects.
[edit]
I now see that Rajesh has an excellent example.
[/edit]
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hi all,
i m using SuspendThread to suspend the thread after this the next statement to be executed is Message box for confirmation of termination of thread,but when this line execute the control not display after this and window becomes unresponsive,i mean message box not return any value.
CWinThread* load_file;
load_file=AfxBeginThread(Load_ThreadProc,NULL,THREAD_PRIORITY_HIGHEST,0,0,NULL);
void CLoad_Modeless::OnBnClickedStopBtn()
{
if(load_file!=NULL)
{
::SuspendThread(load_file->m_hThread);
if(AfxMessageBox(("Do you really want to stop process?"),MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2) == IDYES)
{
::TerminateThread(load_file->m_hThread,0);
load_file=NULL;
}
else
::ResumeThread(load_file->m_hThread);
}
}
please help me for this
thanks in advance.
To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.
|
|
|
|
|
Don't use the SuspendThread , TerminateThread and ResumeThread functions for the type of thread management you're doing here. From the documentation on SuspendThread[^]:
This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.
There are many other explanations of the kind of problems these functions can cause if used incorrectly and the reasons why, such as the following: Why you should never suspend a thread[^]
Steve
|
|
|
|
|
As already mentioned earlier[^], the TeminateThread, SuspendThread and ResumeThread should be avoided. There are much better mechanism to handle this type of situation (events for instance).
I think people already mentioned it to you, but you should really have a look at this article[^] and understand it. Threads are not an easy thing for beginner, so instead of trying things by your own, it would be much more efficient to read a good article about it.
|
|
|
|
|
These functions do not work the way you think they do. After the thread is suspended, no more code from the thread will be executed, and it won't be scheduled any processor time until it is resumed (A thread can suspend itself, but unfortunately cannot resume itself). If your thread is suspended, some other thread has to wake it up so that it can become "schedulable" again.
What you've designed is never going to work as it is fundamentally flawed. Please read up on threading and understand how it works. There are plenty of articles at CP and you should probably read a good book if you're serious about writing multi-threaded code. After all that, there are gotchas involved in threading that you'll only experience and learn by writing code.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
I want to send varying length application specific messages between my client and server application. The problem is how should I determine the length of message when receiving it?
I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better?
May I know why and how do I prefix it? Any code examples would be really appreciated. I'm using Winsocks (version 2.2, if that helps)
And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP?
Thanks
modified on Wednesday, August 26, 2009 6:25 AM
|
|
|
|
|
Ahmed Manzoor wrote: I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better?
Prefix the length. That gives you a definite message length, which lets you detect errors better.
Say you had a termination string and the packet containing the termination string was lost. Your program would keep searching and either a) never see the terminator, so think the message never ended, or b) see the wrong terminator (maybe from the next message) and merge two messages into one.
Ahmed Manzoor wrote: And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP?
No minimum really - but you need to turn off the Nagle algorithm (see setsockopt and TCP_NODELAY[^]).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I don't understand what you mean by no minimum? Can you please explain?
|
|
|
|
|
If you turn off Nagling, then you can send a single byte in a TCP/IP packet. You can't get smaller than that (except 0, which is nothing).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
You can send one byte at a time. You do NOT need to turn off Nagle algorithm in order to be able to send single bytes.
Beside using a length prefix or a terminator, there is also a third possibility. If you send only one message per TCP connection, then close the connection gracefully[^]. The receiving peer will know when it has received the complete message and can close the socket. However, if you want to send more then one message, the first two possibilities would be better.
|
|
|
|
|
Can you please provide an example? using the recv() and send() functions?
|
|
|
|
|
Hello,
I have the following problem. I must display a printstream as readable text. Inside the stream are the signs asci 13 (Carriage Return) and asci 10 (Line Feed). If I want to display it in a CRichEditCtrl, the Control wraps the line.
If Set
SetTargetDevice(NULL, 1);
The Control makes also a word wrap at CR and LF.
I there a possibility to supress the word wrap?
best regards
thomas
|
|
|
|
|
Word wrap is different from carriage return/line feed.
CR/LF will move to the beginning of the next line.
Word wrap will move to the next line if the line does not fit in the current view.
If you do not want to move to next line in spite of having CR/LF you will have to remove the character pair from the stream.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Thanks for your reply,
but the problem is that I can't remove it form the text, because it is important to show these signs as symbol. I have my own font to show CR sign or a LF sign.
|
|
|
|
|
Then how about replacing these characters with your own symbol in the stream itself so that it will not move to the next line.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
It doesn't work the control wraps the text anyway.
|
|
|
|
|
You might need to look into the ES_MULTILINE , ES_AUTOVSCROLL , and ES_AUTOHSCROLL styles.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Thias doesen't work. I tried it.
|
|
|
|
|
You tried looking into it, or you tried implementing those styles? Wrapping in an edit control is only caused by one or two things: the characters being put in the control, and the styles of the control. Both have been suggested to you.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I just look into these styles, but how should I implement a style?
|
|
|
|