|
I don't believe it is possible for an application to spawn two threads with the same id unless a hardware or software bug caused an intermittent failure.
John
|
|
|
|
|
Thanks for your help.
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
The only way this could be possible is if one thread terminated before the other began. One possibility is to create all the threads suspended. (I'd also use the handle as the unique identifier--it's so useful for other things while the thread ID isn't.)
(Another problem is running 50 UI threads. I'd reexamine your logic there.)
|
|
|
|
|
Thanks, I just said 50 UI threads to help the example make sense.. I dont really spawn that many threads.
Thanks for the reply
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
how can I check to see if I set the processaffinity correctly?
|
|
|
|
|
Hi
The function is declared as
BOOL SetProcessAffinityMask(HANDLE hProcess, DWORD_PTR dw ProcessAffinityMask)
the return value ist nonzero if the function succeeds, if the ret-val zero call GetLastError().
An other way is:
1) call SetProcessAffinityMask(...)
2) call GetProcessAffinityMask(ProcessHandle[in], ProcessAffinityMask[out], SystemAffinityMask[out])
3) verify your set_mask with the get_mask
Regards
Frank
|
|
|
|
|
Take a look at the C++ program below, how could I produce a story from the 20 random "nonsense" sentences using the second noun of the first sentence as the first noun in the second sentence ( i.e. --> 1. The cat ran over the boy. 2. Some boy jumped into any town. 3. Any town went under some mouse. )
//Random generator of "nonsense" sentences & story
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cctype>
using std::cout;
using std::endl;
using namespace std;
const int SIZE = 1000;
//Function Prototypes
int displayMenu();
void displaySentences();
void displayStory();
int main()
{
//Declare variables for menu
int menuChoice = 0;
//display menu and get choice
menuChoice = displayMenu();
while (menuChoice != 0)
{
if (menuChoice == 1)
displaySentences();
else if (menuChoice == 2)
displayStory();
else
cout << "Invalid menu choice" << endl;
cout << endl << endl;
//end ifs
menuChoice = displayMenu();
} //end while
return 0; // indicates successful termination
} // end main
//******** End of Program defined Functions ************
int displayMenu()
{
//declare variables
int choice = 0;
//display options
cout << "Options:" << endl << endl;
cout << "0 Exit Program" << endl;
cout << "1 Display Sentences" << endl;
cout << "2 Display Story" << endl;
cout << endl;
cout << "Enter menu option: ";
cin >> choice;
cin.ignore(1); //consume Enter key
return choice;
} //end of displayMenu function
void displaySentences()
{
//initialize article array
const char *article[5] =
{ "the", "a", "one", "some", "any" };
//initialize noun array
const char *noun[10] =
{ "boy", "girl", "dog", "town", "car", "man", "woman", "cat", "mouse", "boat" };
//initialize verb array
const char *verb[5] =
{ "drove", "jumped", "ran", "walked", "skipped" };
//initialize preposition array
const char *preposition[5] =
{ "to", "from", "over", "under", "on" };
//to hold the sentence
char sentence[ SIZE ] = "";
// loop through and make 20 random sentences out of the given array
for ( int v = 1; v <= 20; ++v )
{
// uses function strcat to concatenate random sentences
strcat( sentence, article[ rand() % 5 ] );
strcat( sentence, " " );
strcat( sentence, noun[ rand() % 10 ] );
strcat( sentence, " " );
strcat( sentence, verb[ rand() % 5 ] );
strcat( sentence, " " );
strcat( sentence, preposition[ rand() % 5 ] );
strcat( sentence, " " );
strcat( sentence, article[ rand() % 5 ] );
strcat( sentence, " " );
strcat( sentence, noun[ rand() % 10 ] );
// print the current sentence
cout << static_cast< char > ( toupper( sentence[ 0 ] ) )
<< &sentence[ 1 ] << ".\n";
// reset the sentence
sentence[ 0 ] = '\0';
} // end for loop
cout << endl;
} // displaySentences function
void displayStory()
{
//initialize article array
const char *article[5] =
{ "the", "a", "one", "some", "any" };
//initialize noun array
const char *noun[10] =
{ "boy", "girl", "dog", "town", "car", "man", "woman", "cat", "mouse", "boat" };
//initialize verb array
const char *verb[5] =
{ "drove", "jumped", "ran", "walked", "skipped" };
//initialize preposition array
const char *preposition[5] =
{ "to", "from", "over", "under", "on" };
//to hold the sentence
char sentence[ SIZE ] = "";
// loop through and make 20 random sentences out of the given array
for ( int v = 0; v <= 20; ++v )
{
// uses function strcat to concatenate random sentences
strcat( sentence, article[ rand() % 5 ] );
strcat( sentence, " " );
strcat( sentence, noun[ rand() % 10 ] );
strcat( sentence, " " );
strcat( sentence, verb[ rand() % 5 ] );
strcat( sentence, " " );
strcat( sentence, preposition[ rand() % 5 ] );
strcat( sentence, " " );
strcat( sentence, article[ rand() % 5 ] );
strcat( sentence, " " );
strcat( sentence, noun[ rand() % 10 ] );
// print the current sentence
cout << static_cast< char > ( toupper( sentence[ 0 ] ) )
<< &sentence[ 1 ] << ".\n";
// reset the sentence
sentence[ 0 ] = '\0';
} // end for loop
cout << endl;
} // end displayStory function
tincup23
|
|
|
|
|
I am sorry, but I can't look at that huge amount of pure C string handling.
As this seem to be some C++-learning, you might as well try to use a string class (std::string or CString from MFC if you like).
Also, try to use the C++ std::vector class instead of static old C-arrays.
Sorry that I could not give you any algorithmical advice
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
I had a brain freeze with my original comment. I was thinking only of the word arrays. I still feel those should be left as is, though made global. However, you are right that he should use string or CString to build the resulting string. (Though, since he's going to output it immediately, I don't see why he just doesn't output the strings immediately.)
|
|
|
|
|
I never understand this type of advise. His program does not do a lot of string manipulation at all. The only string manipulation is the section where the strings are combined using strcat.
Not everything is better with std::string or CString. Old-style character arrays still work perfectly fine in a lot of cases.
|
|
|
|
|
Reinout Hillmann wrote:
I never understand this type of advise.[...]Not everything is better with std::string or CString. Old-style character arrays still work perfectly fine in a lot of cases.
But the original poster specifically stated that he wanted to learn C++. String classes (esp. std::string) are an integral part of working with C++ (as is using the STL and understanding templates).
You are right in stating that C-character arrays are fine, but they are definetly not C++, though seamlessly integrated.
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
First, you should move the word arrays to be global. There's no need to repeat them.
Second, I don't see the point of using "sentence". Why not just output the words immediately? If you must store the sentence, use string .
Third, create a variable named "lastNoun" outside the loop. Assign it a random value (rand() %10 .) Use lastNoun as the index to both nouns. Just before the second usage, assign it a new random value.
You could add a small loop to ensure that you don't reuse the same noun.
(For consistancy, you should also retain the article using the same method.)
|
|
|
|
|
Hello,
I need to retrieve Date&Time from a remote UNIX server in the poche format to synchronize mi machine using a TCP/IP conection via sockets, how can I do that programming in VC++??
Edujule
|
|
|
|
|
If that server is listening on a particular port (some use 37, others 13), it can be done. Check out time_a.timefreq.bldrdoc.gov for more.
|
|
|
|
|
Hello,
The following code seems to work perfectly fine on Win 2k and above.. I think it may have some type of memory leak on 9x machines.. Can anyone find anything wrong with the following code?
It's used to paint my tree view..
void CMyTreeView::OnPaint()
{
if(IsWindowVisible())
{
CPaintDC dc(this);
CRect rcClip, rcClient;
dc.GetClipBox(&rcClip);
GetClientRect(&rcClient);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
memDC.SelectObject(&bitmap);
CRgn rgn;
rgn.CreateRectRgnIndirect(&rcClip);
memDC.SelectClipRgn(&rgn);
rgn.DeleteObject();
CWnd::DefWindowProc(WM_PAINT, (WPARAM)memDC.m_hDC, 0);
if(m_bColor)
{
CRect border;
GetTreeCtrl().GetClientRect(&border);
memDC.FillSolidRect(0,0,1,border.bottom, RGB(255,0,0));
memDC.FillSolidRect(0,0,border.right,1, RGB(255,0,0));
memDC.FillSolidRect(border.right-2,0,2,border.bottom,RGB(200,0,0));
memDC.FillSolidRect(0,border.bottom-2,border.right,2,RGB(200,0,0));
}
HTREEITEM hItem = GetTreeCtrl().GetFirstVisibleItem();
while(hItem)
{
CRect rect;
UINT selflag = TVIS_DROPHILITED | TVIS_SELECTED;
Color_Font cf;
if (!(GetTreeCtrl().GetItemState(hItem, selflag) & selflag)
&& m_mapColorFont.Lookup(hItem, cf))
{
CFont *pFontDC;
CFont fontDC;
LOGFONT logfont;
CFont *pFont = GetFont();
pFont->GetLogFont(&logfont);
if(GetItemBold(hItem))
logfont.lfWeight = 700;
fontDC.CreateFontIndirect(&logfont);
pFontDC = memDC.SelectObject(&fontDC);
if(cf.color != (COLORREF)-1)
memDC.SetTextColor(cf.color);
CString sItem = GetTreeCtrl().GetItemText(hItem);
GetTreeCtrl().GetItemRect(hItem, &rect, TRUE);
memDC.SetBkColor(RGB(255,255,255));
memDC.TextOut(rect.left+2, rect.top+1, sItem);
memDC.SelectObject(pFontDC);
}
hItem = GetTreeCtrl().GetNextVisibleItem(hItem);
}
dc.BitBlt(rcClip.left, rcClip.top, rcClip.Width(), rcClip.Height(), &memDC,
rcClip.left, rcClip.top, SRCCOPY);
memDC.SelectObject(pOldBitmap);
pOldBitmap->DeleteObject();
memDC.DeleteDC();
}
}
BOOL CMyTreeView::OnEraseBkgnd(CDC* pDC)
{
pDC = NULL;
return TRUE;
}
Thanks,
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
Looks good to me. You don't need to (and shouldn't) call pOldBitmap->DeleteObject() because the bitmap is still selected into the device context. Shouldn't cause any problems because you're deleting the device context without doing anything else to it, but you should remove that line
A couple of other points though...
What is the reason for setting pDC to NULL in OnEraseBkgnd() ? It's not necessary and will have absolutely no effect at all
Why are you using IsWindowVisible() ? OnPaint() won't get called unless the window is visible...
And you don't need to set the clip region in your memory device context. Your BitBlt() call is only copying the parts in the clip box anyway. Speed may seem like a reason to do it, but the process of checking everything against the clip region can be slower than simply writing everything to a memory device context, particularly for lower-end graphics cards.
Hope this helps,
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Ryan Binns wrote:
You don't need to (and shouldn't) call pOldBitmap->DeleteObject()
Thanks, I'll remove that...
Ryan Binns wrote:
What is the reason for setting pDC to NULL in OnEraseBkgnd()?
I set that to NULL to get rid of the level 4 warning..
Ryan Binns wrote:
Why are you using IsWindowVisible()? OnPaint() won't get called unless the window is visible...
I'll take that out..
I'll remove the clip region too..
Thanks for all your help!
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
RobJones wrote:
I set that to NULL to get rid of the level 4 warning
Try using the UNUSED() or UNUSED_ALWAYS() macros, or simply declaring the parameter without a name:
BOOL CMyTreeView::OnEraseBkgnd(CDC* ) If a parameter has no name, the compiler assumes it isn't used, and just ignores it. I usually leave it in a comment, so I can see what it actually is if I need it later
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
I used the /*pDC*/ instead of pDC = NULL;
Thanks for all your help..
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
You're welcome
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Hi,
If we want to display a bmp onto a CView derived class's DC, why do we usually create a memory DC first and then select the bmp into that memory DC and then BitBlt the memory DC onto the CView DC?
Why not directly select the bmp into the CView DC?
Why do we go through a memory DC? Is it because it is faster to do it that way??
Regards,
Melwyn
|
|
|
|
|
Bitmaps cannot be selected into windows DCs, they can only be selected into memory DCs.
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
Hi Paul,
Thanks for the reply. It cleared a long-standing doubt of mine.
By the way, is it absolutely necessary to select atleast 1 bitmap into the memory DC before you can start drawing into the memory DC?
Thanks,
Melwyn
|
|
|
|
|
Hello
I am trying to allocate more than 1.3 Gb memory in my MFC based program.But it doesn't work with error No 8(not enough memory)
While I could do this on Non-MFC application.Anyone has a solution for this?
|
|
|
|
|
If the memory is not available, it's not available. Remember that your process only gets a 2GB address space (unless you have NT and started it with the /3GB option), and unless 1.3GB of that is contiguous, the allocation request will fail.
|
|
|
|
|