|
To learn the basics and avoid common pitfalls regarding multithreading, read Joe Newcomer's article on the subject here[^].
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
|
|
|
|
|
wrote: int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hT1;
hT1 = CreateThread(NULL, 0, Thread1, NULL, 0, NULL);
WaitForSingleObject(hT1, INFINITE);CloseHandle(hT1);
cout<<"main thread"<<endl;
return 0;
}<="" blockquote="">
In this way - for example - you let the thread execute and wait for thread to finish, then back in main thread and this will finish, too.
Output will be
thread1
main thread
Nuclear launch detected
|
|
|
|
|
wrote: I expected only "main thread" would be printed out, coz the main thread quits before Thread1 can be executed.
You're still thinking of things as synchronous. Windows, and especially threads, are a fundamentally asynchronous, event-driven universe.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I don't know how to build a class of a large number. It maybe include hundreds number characters. Its behaviour are "*(x)" , "+", "-" , "/".
Somebody helps me.
Thanks alot.
Try to find how to die
|
|
|
|
|
rockfanskid wrote: I don't know how to build a class of a large number. It maybe include hundreds number characters. Its behaviour are "*(x)" , "+", "-" , "/".
Somebody helps me.
|
|
|
|
|
I did this once a long time ago in C. I just recently converted it to VC++. Which part are you having trouble with?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
So I have a program that reads a network source in over a TCP/IP connection and it sends info between 10 times a second and once every 10 seconds. Now, at first, I thought I could just use a Timer and every time it ticks, call my recv() function. However, it works until like the fifth time I call recv() and that's when it freezes the program and stops updating. I was wondering if there is any way to make the program call recv constantly, without blocking, so it can update the program whenever the network source send information, but not freeze the program when the network source doesn't send any info. Any suggestions?
Oh yeah, I'm using Visual Studio 2005 on Windows XP, and I'm using Windows Forms to write it. Let me know if I'm unclear about something.
|
|
|
|
|
Yeah, look up IO completion ports.
No blocking with high performance.
-- modified at 12:05 Sunday 11th February, 2007
ouch. a 2 vote.
Yes, I guess I mistated that since the worker thread actually blocks while waiting on GetQueuedCompletionStatus(). I guess it would have been clearer to state that the GUI thread will not block and your application will remain responsive and you will experience high performance like every enzyte customer on the planet.
My bad.
|
|
|
|
|
Instead of using a timer (inefficient!) you can use an event.
1) Create an event (CreateEvent()) which will be signalled when the socket receives data.
2) Use WSAEventSelect() and select (at least) the FD_READ flag. This sets the socket to non-
blocking mode.
3) Create a thread and in the thread proc a loop. Use WaitForSingleObject() (or multiple if
you have more events) to wait on the read event created in step 1. When the event is signalled
that means there's data received.
Remember, with TCP, you won't always receive the exact amount of bytes sent on a single send()
call from the other end. Call recv() specifying the number of bytes you are expecting and check
to make sure you got that many. Repeat (loop) until you receive the WSAEWOULDBLOCK error, then
loop again and wait for the next event.
Mark
|
|
|
|
|
Thank you for the descriptive answer, but now for the stupid question...how and where do I add a thread and event? Sorry, but I'm pretty new to the whole Visual Studio thing.
|
|
|
|
|
I forgot to mention....
If you're not comfortable with multiple threads then substitute WSAAsyncSelect() for
WSAEventSelect() in my other post. You'll receive window message so you won't need another
thread.
If you're really comfortable with multiple threads, as bob mentioned, there's IO completion ports.
Mark
|
|
|
|
|
I have a dialog based app, when I want to change window's position form the OnInitDialog() it doesn't work. The same code works fine form OnBnClickedOk()?!? How to setup window's position before it is shown?
My test code in OnInitDialog() and OnBnClickedOk():
SetWindowPos(&wndTop,0,0,0,0,SWP_NOSIZE);
Thanks in advance.
|
|
|
|
|
In your OnInitDialog() override are you calling the base class' OnInitDialog() first before calling SetWindowPos()?
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Yes, I'm calling SetWindowPos() just before final 'return TRUE;' statement. This dialog window is created form new thread in this way:
....
CMyDialog m_lister(CDialog::FromHandle(GetDesktopWindow()));
m_pMainWnd=(CWnd *)&m_lister;
m_lister.DoModal();
....
Thanks for Your help.
|
|
|
|
|
PatrykDabrowski wrote: SetWindowPos(&wndTop,0,0,0,0,SWP_NOSIZE);
Actually the above call works fine (as it should do...) inside OnInitDialog() on my system (Win2k). I made a test using the CAboutDlg
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
In my CAboutDlg it is working too, but not in main dialog...(?!?)
My CDialog derived class is created on a new thread form CWinThread::InitInstance() using code like this:
....
CListerDialog m_lister(CDialog::FromHandle(GetDesktopWindow()));
m_pMainWnd=(CWnd *)&m_lister;
m_lister.DoModal();
....
hmmm, any idea why simple SetWindowPos() call doesnt work??
Thanks for help.
|
|
|
|
|
PatrykDabrowski wrote: CListerDialog m_lister(CDialog::FromHandle(GetDesktopWindow()));
m_pMainWnd=(CWnd *)&m_lister;
m_lister.DoModal();
Hu....Wy are you doing that....
Anyway, I think you have to use ::GetDesktopWindow() insted of GetDesktopWindow() .
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
"that" - You mean, setting parent window to Desktop?? I set it to have "full thread separated" dialogs. When I pass NULL to the constructor then my 'child' window minimize when I minimize my main dialog window. When I set Desktop window as its parent, I can minimize both windows independently (plus, my 'child' window has its own entry in taskbar;)
Isn't that right?
|
|
|
|
|
PatrykDabrowski wrote: How to setup window's position before it is shown?
You could override PreCreateWindow().
In the override, call the base class PreCreateWindow() then change the x,y,cx,cy members of the
passed CREATESTRUCT.
Or not for a dialog
Mark
-- modified at 13:51 Sunday 14th January, 2007
|
|
|
|
|
MFC determines whether it should auto-center a dialog by comparing its RECT before and after your OnInitDialog() runs. (See _AfxPreInitDialog() and _AfxPostInitDialog() .) If the RECT is the same before and after, then MFC centers it. If the dialog's default position is (0,0) then your SetWindowPos() call won't move it, and it will be centered.
You can either use another position, or call ShowWindow(SW_SHOW) in OnInitDialog() so _AfxPostInitDialog() won't do its auto-centering logic.
|
|
|
|
|
WOW, thanks Nice trick with this (0,0) window position;) Now it works fine. Thanks again
|
|
|
|
|
Use MoveWindow in OnInitDialog() .
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Hi all
The following code snippet....
ifstream f_;
f_.open(fname);
while(i < records_in_file) {
f_ >> data_[i].idx_ >> ws;
f_ >> data_[i].noOfRegs_ >> ws;
i++;
}
.. is reading the following file
0001 13
0002 24
0003 209
0004 85
0005 3
0006 122
0007 28
0008 9
0009 31
0010 27
… and so on …
All seems to work for the first 7 records, but when it comes to read the 8th record (i.e. “0008 9”), the first value it reads is 0 (zero) and then it reads the 8.
I can’t figure out what is going wrong. Any ideas? Is it something to do with ASCII screwing up the input stream.
As an aside I have also tried a “getline” solution, but run into similar problems.
Cheers
Graham
|
|
|
|
|
Any 'O' char (ASCII code 79) between zeroes?
Cheers
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
No, I have copied/pasted from the above records (as they are zeroes) and also manually put in zeroes, but same result.
There are definitely 3 zeroes before the 8.
Cheers
Graham
|
|
|
|