|
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
|
|
|
|
|
Before you call pSubMenu->TrackPopupMenu, you could call pSubMenu->EnableMenuItem[^] to initialise the menu as you please.
modified 13-Sep-18 21:01pm.
|
|
|
|
|
That was it. Thanks.
"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
|
|
|
|
|
If you want to use your regular OnUpdateBlaBla functions, you could do something like
CCmdUI cmdUI;
cmdUI.m_pMenu = pPopup;
cmdUI.m_pSubMenu = NULL;
cmdUI.m_nIndexMax = pPopup->GetMenuItemCount();
for (int i = 0; i < pPopup->GetMenuItemCount(); ++i)
{
cmdUI.m_nID = pPopup->GetMenuItemID(i);
if (cmdUI.m_nID != ID_SEPARATOR)
{
cmdUI.m_nIndex = i;
cmdUI.DoUpdate(this, cmdUI.m_nID < 0xF000);
}
}
before calling TrackPopupMenu. This makes life easier if you have the same command in another menu and or toolbar button. Then you only need to maintain the logic in the OnUpdateBlaBla functions.
|
|
|
|
|
Niklas Lindquist wrote: cmdUI.DoUpdate(this, cmdUI.m_nID < 0xF000);
What is DoUpdate() ?
"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
|
|
|
|
|
DoUpdate is an undocumented public member function of CCmdUI which makes it all happen. It will internally call your OnUpdateBlaBla function. That is how the framework uses it. I have used this construct for context menus in custom controls (runnng DoUpdate(GetParent(), ...) when there seemed to be no way of getting the automatic handling by the framework to kick in. The snippet comes from within an MFC class, but I can no longer remember which, but it works for me.
home
modified on Monday, January 17, 2011 4:15 AM
|
|
|
|
|
Well, there is always a lot of ways to skin a cat.
I would use my MEMC approach (Maximum Effect Minimum Code).
Your code does not use MFC update command UI mechanism, since you tell framework not to. Your code tells the framework to handle the popup menu directly in a view. View does not handle command update UI.
Change one line (or one expression) in your code to:
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, pt.x, pt.y, AfxGetMainWnd());
This will start proper menu handles search and UI updates.
You eliminate a need to handle disabling menu items before calling TrackPopupMenu; morover, if you add menu items and handlers you will never have to change a code that disables menu items before displaying popup.
JohnCz
MS C++ MVP
|
|
|
|
|
I'm looking for some VS c++ code that 1) preallocates a file 2) opens a preallocated file for sequential writing starting at the beginning of the file 3) writes(appends) ascii message strings to the file. Thanks
|
|
|
|
|
I'm not sure exactly what you mena by point 1, but opening a file for appending is quite straightforward, and writing text is even more so. Take a look at the STL file stream classes[^] or use the Createfile[^] and associated functions.
I must get a clever new signature for 2011.
|
|
|
|
|
You need to seek to what you want to be the end of the pre-allocated space, then set the EOF marker, then seek back to the start so you are writing from the beginning of the file, like so
HANDLE hFile = CreateFile("C:\\test", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
SetFilePointer(hFile, 4 * 1024 * 1024, NULL, SEEK_SET);
SetEndOfFile(hFile);
SetFilePointer(hFile, 0, NULL, SEEK_SET);
CloseHandle(hFile);
}
Edit:
I missed part 3...
quite simply seek to the end of the data in the file to append to it.
SetFilePointer(hFile, 0, NULL, SEEK_END);
SetFilePointer(hFile, nDataLength, NULL, SEEK_SET);
Writing to a file is pretty straight forward. You just need a buffer and the length of data you are writing
LPSTR szWrite = "hello world";
DWORD nLength = strlen(szWrite);
DWORD nBytesWritten;
WriteFile(hFile, szWrite, nLength, &nBytesWritten, NULL);
CloseHandle(hFile);
If you are writing strings in unicode you need to multiply the length by sizeof(wchar_t) or sizeof(TCHAR)
Finally, All file writes (and reads) are sequential for all APIs that I know of. It moves the file pointer to the end of the data you have just written (or read)
modified on Wednesday, January 12, 2011 11:23 AM
|
|
|
|