|
|
Sorry, that was a copy and paste error from 1 of my own projects.
The code in the previous message has been corrected, I created the variable HANDLE hThread = pThread->m_hThread; as a shorthand.
|
|
|
|
|
I place a breakpoint in CTest_Thread::PreTranslateMessage(MSG *pMsg) function ,but cursor never comes on this
|
|
|
|
|
I would guess that you are overriding the Run() function, in which case the default CWinThread::Run() code won't be executed.
If you look at the default implementation in C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc\thrdcore.cpp you will see that there is a message pump in it which will not be executed unless you manually execute it.
The Run() function is rarely overridden, however there are legitimate uses for it.
Depending on what you are trying to do, it may be possible to not override it, or to incorporate the message pump into your code.
Just note that you can't just exit a function when you tell it to quit... if it is waiting on data from the network, then it must receive that data or time out before the function returns
|
|
|
|
|
No i m not overriding the Run() function.
|
|
|
|
|
Do you have any code that waits on some external input before returning in the thread, such as keyboard input (scanf, _getch), network input (recv), exclusive handle (mutex, semaphore), ... that the thread might be hanging in?
If you insert a breakpoint on the switch (WaitForSingleObject(hThread, nTimeoutMs)) , then when the breakpoint is hit change the thread (Thread Window is Debug>Windows>Threads) and change to your worker thread, then view the call stack (Call Stack window is Debug>Windows>Call Stack) to see if the thread may be stuck in a function when you are trying to send it the quit message.
|
|
|
|
|
There is any minimum timeout value for WaitForSingleObject .
i mean use 5 sec for WaitForSingleObject or i can use less than 5 sec for WaitForSingleObject.
|
|
|
|
|
You can use any value you like, even 0 for return immediately but then (in this case) what is the point, or you can make it as long as you want up to INFINITE.
I chose 5000ms because I thought it was a reasonable time to wait in this case.
|
|
|
|
|
|
there is one function : TerminateThread() which is a little dangerous to use.
see the documentation of this function.
u can try the following way indeed.
create an event (CreateEvent() function)from ur main thread. and in the thread u regularly check for the state of the event using wait functions such as WaitForSingleObject(). if the state is signalled u return from the thread function.
from the main thread u set the state of the event when u want to terminate the thread.
also there is one more option to terminate that is Peek and Pump. that is u use PeekMEssage() followed by PumpMessage().
check the documentation on how to use this.
Enjoy.
|
|
|
|
|
Instead of explaining the concept here, I will redirect you to an excellent article[^]. There's a section about shutting down a thread, but the full article is really a must read if you are going to work with threads. You will learn most of the things you need in one single article and probably gain a lot of time afterwards by not doing silly mistakes.
|
|
|
|
|
In the below code I have put together a text example of a shopping cart whereby the user selects a commodity to purchase and based upon a menu selection, tells the program which leg in my switch statement to execute. After a selection is made, the program drops out of the switch statement and proceeds the the bottom of the while statement where "ordercom" tells the while statement to continue taking orders. This is obviously an un-ending loop but is coded this way to trouble shoot the issue at hand. I am testing the code with code breaks. What is happening is that after I select two menu selections or two loops of the while statement the program skips over the scanf statement.
Can someone help me with this issue?
#include <stdio.h>
#define Artichoke 1.25
#define Beets .65
#define Carrots .89
#define Discount .05
#define Shipping1 10.00
#define Shipping2 8.00
int main(void)
{
char ordercom;
char type;
float pounds;
float gross;
float poundTot = 0;
ordercom = 'Y';
while(ordercom == 'Y')
{
printf("*******************************************************************************************************************\n");
printf("Determine What You Want To Buy\n");
printf("1) (a)\t Artichokes\t 3) (c)\t Carrots\n");
printf("2) (b)\t Beets\t 4) (q)\t Quit\n");
printf("*******************************************************************************************************************\n");
printf("\n");
printf("Select The Type Of Vegetable You Want And The Pounds Of The Vegetable You Want Or (q) To Quit :");
scanf("%c ""%f",&type,£s);
switch(type)
{
case 'a' :
{
gross = pounds * Artichoke;
break;
}
case 'b' :
{
gross = pounds * Beets;
break;
}
case 'c' :
{
gross = pounds * Carrots;
break;
}
case 'q' :
{
break;
}
default :
{
break;
}
}
ordercom = 'Y';
}
}
|
|
|
|
|
Mike Certini wrote: What is happening is that after I select two menu selections or two loops of the while statement the program skips over the scanf statement.
Can someone help me with this issue?
Likely because something is in the keyboard buffer. Try using fflush() after scanf() .
"One man's wage rise is another man's price increase." - Harold Wilson
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
This is correct.
It will be the carriage return ('\n' = 0x0A) character that is left in the input buffer.
And as you suggested, fflush(stdin); should fix the problem
|
|
|
|
|
Andrew,
Excellent! Thank you for your reply. Can you answer another question? Why is my "q" selection or default not working?
|
|
|
|
|
there are 2 reasons:
1. the scanf function wont return until it gets a char and a float
2. You are only breaking out of the switch statement, not the while loop
switch (type) {
case 'q':
break;
default:
break;
}
those are simply breaking out of the switch statement. you need to tell the while loop to exit
#include <conio.h>
int main() {
while (ordercom == 'Y') {
type = _getch();
putc(type, stdout);
if (type == 'q') {
break;
}
scanf("%f",£s);
switch (type) {
default:
ordercom = 'N';
break;
}
}
}
|
|
|
|
|
Andrew,
Thanks. I am not familiar with putc() and _getch(). I will take a look into this.
Mike
|
|
|
|
|
How did u confirm they are not working ?
Try to put printf() in those breaks and check/debug.
|
|
|
|
|
Malli,
Thank you for your input. I found a number of corrections on other boards. As a result here is the corrected version:
#include <stdio.h>
#include <conio.h>
#define Artichoke 1.25
#define Beets .65
#define Carrots .89
#define Discount .05
#define Shipping1 10.00
#define Shipping2 8.00
int main(void)
{
char ordercom;
char type;
float pounds;
float gross;
float poundTot = 0;
ordercom = 'Y';
while(ordercom == 'Y')
{
printf("*******************************************************************************************************************\n");
printf("Determine What You Want To Buy\n");
printf("1) (a)\t Artichokes\t 3) (c)\t Carrots\n");
printf("2) (b)\t Beets\t 4) (q)\t Quit\n");
printf("*******************************************************************************************************************\n");
printf("\n");
printf("Select The Type Of Vegetable You Want:\n");
scanf("%c",&type);
fflush(stdin);
printf("Select The Amount Of Pounds You Want To Purchase:\n");
scanf("%f",£s);
fflush(stdin);
switch(type)
{
case 'a' :
{
gross = pounds * Artichoke;
ordercom = 'Y';
break;
}
case 'b' :
{
gross = pounds * Beets;
ordercom = 'Y';
break;
}
case 'c' :
{
gross = pounds * Carrots;
ordercom = 'Y';
break;
}
case 'q' :
{
ordercom = 'N';
break;
}
default :
{
ordercom = 'N';
break;
}
}
}
}
|
|
|
|
|
There is no harm in it, but in the cases for 'a', 'b' and 'c' you dont need to set ordercom = 'Y'; because it already has that value.
You should no longer need #include <conio.h> now that you got rid of _getch()
|
|
|
|
|
1 more thing I missed at a first glance.
For the case when the user presses q to quit, they still need to enter a float for the weight.
you should have the 2nd scanf inside an if to check for that
|
|
|
|
|
for the breaks in the switch statement, I could just see what was hapening
for the rest I placed a breakpoint on the line switch(type) so when it hit that I could see what the value of the variables were.
for the 2nd time in the loop type = '\n' (0x0A) when you hover over the variable name.
For the 'q' case for quitting I just know how scanf works, that it wont return until it gets a new line character, so another solution was needed
|
|
|
|
|
Before you start guessing, why not collect some information?
How about printing the values of type and pounds right after your scanf got them?
add a simple printf line, and you will probably know what is going on.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Luc,
I will do this on my next post. Thank you for the recommendation.
|
|
|
|
|
When I right-click an item in a list control, I'm using the following code to display a popup/context menu. That part works fine. I then want one of the items in that menu to be disabled. That part does not work.
BEGIN_MESSAGE_MAP(CMyView, CListView)
ON_UPDATE_COMMAND_UI(ID_SHOWDATA, &MyView::OnUpdateShowData)
END_MESSAGE_MAP()
void CMyView::OnRightClick( NMHDR *pNMHDR, LRESULT *pResult )
{
CPoint pt;
CMenu menu,
*pSubMenu;
menu.LoadMenu(IDR_LISTCTRL_POPUP);
pSubMenu = menu.GetSubMenu(0);
pt = GetMessagePos();
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, pt.x, pt.y, this);
*pResult = 0;
}
void CMyView::OnUpdateShowData(CCmdUI *pCmdUI)
{
pCmdUI->Enable(FALSE);
} As a matter of fact, OnUpdateShowData() does not get called until after I select the menu item, thus it has no chance to disable it. Any ideas?
Thanks.
- DC
"One man's wage rise is another man's price increase." - Harold Wilson
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|