|
In MFC worker threads don't have a message queue. You would need to use a GUI thread. I'd look at implementing a simple message queue yourself. An STL queue and a critical section should do the trick.
|
|
|
|
|
|
A situation where you want to send a message to a worker thread is very rare and in my opinion suspect. Usually you would handle such situation by signalling an event which the worker thread is waiting on, or polling a volatile declared flag.
Perhaps there is a more suitable solution for your problem.
What problem are you trying to solve by the use of a worker thread and why do you want to send messages to it?
It might be a little of topic, but it could be worth reading this[^] excellent article regarding multithreading.
--
Roger
-- modified at 4:18 Wednesday 17th January, 2007
Posted the wrong link. My apologies.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
|
|
|
|
|
Roger Stoltz wrote: polling a volatile declared flag
Having a worker thread polling is not generally a good idea, but waiting on an event, on the other hand...
I inherited some code that polled, and because the person writing it was using a sleep inside the loop, they thought it would be OK. The thread was eating around 30% of the available CPU time while it waited, until I changed it to use an event, whereupon it "magically" dropped.
Steve S
Developer for hire
|
|
|
|
|
Steve S wrote: Having a worker thread polling is not generally a good idea, but waiting on an event, on the other hand...
What I meant, which unfortunately was not very clear, was a special case for a worker thread that doesn't wait for anything. It has a specific task that consumes 100% of the thread time slices and the thread would terminate when the task has completed. If you need to cancel the task prematurely I think it would be suitable by polling (or rather checking) a flag. But of course, there's always the possibility for calling ::WaitForSingleObject() with the timeout value set to zero, but that feels like overkill.
I was hoping for the OP to post again allowing me to pick up the "thread".
Steve S wrote: I inherited some code that polled, and because the person writing it was using a sleep inside the loop, they thought it would be OK. The thread was eating around 30% of the available CPU time while it waited, until I changed it to use an event, whereupon it "magically" dropped.
::Sleep() is one of the features that are so heavily abused and misused that I lack the words for it.
Every time anyone is trying to fix a threading issue by the use of ::Sleep() the code is almost certain to be wrong and even the design could be assumed to be erroneous.
If you haven't read Joe Newcomer's article about multithreading, which I included a link to in my previous post, have a go on it. It summarizes the common pitfalls quite clearly.
But, if I'm not mistaking, I think you already know...
Have a good one Steve
--
Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
|
|
|
|
|
CMyListCtrl derives from CListCtrl, when someone using CMyListCtrl, he may change it's styles using SetWindowLong or ModifyStyle,I can overwrite ModifyStyle, but for SetWindowLong, I have nothing to do!
thanks
|
|
|
|
|
eat_union wrote: I can overwrite ModifyStyle, but for SetWindowLong, I have nothing to do!
Try these events...
WM_STYLECHANGED , WM_STYLECHANGING .
|
|
|
|
|
thanks, I got it.
|
|
|
|
|
In Visual c++ 6.0 a helpful tool was class wizard. What does one use in VS 2005 c++ to use MFC classes
merlin Vilhauer
|
|
|
|
|
You mean to hook up events? They are on the properties tab.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
You may want to have a look at this[^].
|
|
|
|
|
I am looking to write a server and a client app. I am not sure were to begin. I want to start with a chat service for windows that will connect using TCP/IP services. I am using MFC in Visual C++ 6.0. I have looked at some code on this site - but with no luck with them. I am trying to find a way to launch a server app and just connect with a client app on a network for starters.
also:
Not sure what would be best for this app-
WinSock clasess
WinInet classes
?? any other options?
??
Thanks in advance for any help.
|
|
|
|
|
|
Thank you,
This helped a lot!
|
|
|
|
|
Dosin wrote: This helped a lot!
Why don't you people vote?
Nobody can give you wiser advice than yourself. - Cicero
ப்ரம்மா
|
|
|
|
|
I ran into a bug in my app and eventually figured that it was a problem in the type casting. I wrote a short piece of code to confirm it.
float tempflt = 3.8;<br />
int tempint = (int)(tempflt*100);<br />
CString Debug;<br />
Debug.Format("%d, %f", tempint, tempflt);<br />
MessageBox(Debug);
Why is it that tempint becomes 379 and not 380?
|
|
|
|
|
A floating point value is never precise. Sorry, that's how it goes. Try including math.h and do this:
int tempint = (int )ceil(
tempflt*100);<br />
<br />
I think that's the right syntax, but the Math class has floor and ceiling methods, which round up and down. This will round your 379.xxx up to 380.00, then you cast it to int.<br />
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Christian Graus wrote: A floating point value is never precise.
How predictable is a floats actual value? Because I don't think ceil will be a general solution. What about cases when I should be rounding down?
Is double more precise? I really don't need too much precision as far as decimal points goes. The range I will be in will be -32.768 to 32.767 (the range of short divided by 1000), so I only need 3 numbers after the decimal, which is why i chose float instead of double.
-- modified at 19:35 Tuesday 16th January, 2007
|
|
|
|
|
acerunner316 wrote: What about cases when I should be rounding down?
floor
acerunner316 wrote: Is double more precise?
Yes, but not exact.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Christian Graus wrote: acerunner316 wrote:
What about cases when I should be rounding down?
floor
That's my point. I need a generic code that will accept a float and multiply by 100 and get the right integer. Without knowing how float will skew the result of the multiply operation, I won't know when ot use ceil, and when to use floor.
|
|
|
|
|
you always would need ceil. Just like adding .5 assumes that you need to round up.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
This is because the float (or double) to int cast performs a truncated conversion. The actual answer was probably more like 379.9999999999999999 but the truncation gives a result of 379.
If you want a rounded answer then do this :
int tempint = (int)( tempflt * 100 + 0.5 );
|
|
|
|
|
That's a good idea. As long as the float math operations don't skew the answers by more than 0.5, which it doesn't seem like it will do.
|
|
|
|
|
They won't, that always guarentees you'll get what you want. ( I forgot that little trick )
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
The best method of making sure you get the correct value is to add 0.5 before casting.
|
|
|
|