Re: How to set a timer for a workthread?
Quote:
Originally Posted by tjuzhangrui
Thanks!
Your method that processing the message is valuable for me.
You're wellcome. I'm glad that my sample helps you.
Quote:
Originally Posted by MikeAThon
This won't work reliably, since the pointer being posted in the message will not necessarily point to anything meaningful by the time the message is handled. For example:
Code:
//...
{
CString str;
str.Format("Hi from a thread");
PostTheMessage( str ):
}
// str now goes out of scope and is destroyed ...
Since str has gone out of scope, and has been destroyed, by the time that the message handler is executed, the LPARAM of the handler will be pointing to invalid memory. Bad things follow.
Mike
Mike, I have posted here a part of my program that works fine on a Windows CE application. Perhaps it's not perfect, but I met no problems on that application (it's a real time application ;) ).
I invite you to post here your best solution. :rolleyes:
Re: How to set a timer for a workthread?
Quote:
Originally Posted by Maximus_X
Mike, I have posted here a part of my program that works fine on a Windows CE application. Perhaps it's not perfect, but I met no problems on that application (it's a real time application ;) ).
I invite you to post here your best solution. :rolleyes:
You have been lucky as it happened for the memory address to not be reused/acquired by the time your thread processed the message. What you've done here is very similar to returning he address of a local function variable.
The correct solution is to allocate a char buffer, send that to the thread and have the thread delete it when finished.
Re: How to set a timer for a workthread?
Quote:
Originally Posted by PadexArt
You have been lucky as it happened for the memory address to not be reused/acquired by the time your thread processed the message. What you've done here is very similar to returning he address of a local function variable.
The correct solution is to allocate a char buffer, send that to the thread and have the thread delete it when finished.
Thanks!We can simply create the buffer on the heap to avoid the mistake.
But there's another question. A thread allocated the memory, and another free the memory.This is against the advice that "Allocate and free memory in the same module", it's that considerable in practice?
Re: How to set a timer for a workthread?
Quote:
Originally Posted by tjuzhangrui
This is against the advice that "Allocate and free memory in the same module", it's that considerable in practice?
That rule refers to modules (such as DLLs) and not threads. With a DLL you can have manny complications as the language in which it was written may be completelly different but this is not the case with threads.
Re: How to set a timer for a workthread?
Quote:
Originally Posted by Maximus_X
Mike, I have posted here a part of my program that works fine on a Windows CE application. Perhaps it's not perfect, but I met no problems on that application (it's a real time application ;) ).
I invite you to post here your best solution. :rolleyes:
Indeed, as pointed out by PadexArt, you have been lucky. He gives a good analogy that your technique is similar to a function's return of a pointer to a local stack variable.
Your technique might have worked so far because of a quirk in the way it was used. If you call if like so, with a literal string in the parameter:
Code:
PostTheMessage( _T("Hi from a thread") );
then it might work for the reason that the memory for the literal string will remain intact throughout the life of the program. But that's pure luck.
Mike
Re: How to set a timer for a workthread?
MikeAThon and PadexArt thanks for your sugestions.
I appreciate it. ;)
Looking again on my code, I have remember that the serial connection it was made in the main thread and in the second thread I just used ReadFile() and so on (the second thread function it's static UINT CmyAppDlg::SerialReadThread( LPVOID lParam )).
Regards.
Re: How to set a timer for a workthread?
Quote:
Originally Posted by Maximus_X
... Looking again on my code, I have remember that the serial connection it was made in the main thread and in the second thread I just used ReadFile() and so on (the second thread function it's static UINT CmyAppDlg::SerialReadThread( LPVOID lParam )).
Regards.
Well, I'm a bit confused, for the reason that your post seems really unrelated to the points that PadexArt and I were trying to make. It doesn't matter how the thread was started, or the signature of the thread function. If you are trying to PostMessage a char* across threads, as it appears you are doing to post the string obtained from ReadFile, then it's always necessary to ensure that the char* remains valid and unique until some indeterminate time in the future when the message handler is actually executed.
But if you're satisfied with the outcome, then that's good enough for me.
Mike
Re: How to set a timer for a workthread?
Quote:
Originally Posted by MikeAThon
Well, I'm a bit confused, for the reason that your post seems really unrelated to the points that PadexArt and I were trying to make. It doesn't matter how the thread was started, or the signature of the thread function. If you are trying to PostMessage a char* across threads, as it appears you are doing to post the string obtained from ReadFile, then it's always necessary to ensure that the char* remains valid and unique until some indeterminate time in the future when the message handler is actually executed.
But if you're satisfied with the outcome, then that's good enough for me.
Mike
Well Mike, thanks again for your advice. I appreciate it. I understand what you meed.
I'll keep in my mind and I'll apply it on the code.
Regards,
Silviu.