|
Hi,
I have created threads using CreateThread (6 parameters) format in vc++ as following
DWORD ThreadId_1,ThreadId_2;
CreateThread(NULL,0,StartThread1,CREATE_SUSPENDED,0,&ThreadId_1);
CreateThread(NULL,0,StartThread2,CREATE_SUSPENDED,0,&ThreadId_2);
where StartThread1 and StartThread2 are the thread functions correspondingly. 'this' refers to my present main application.
I would like to suspend and resume the threads at any point of time, but I didn't understand how to use SuspendThread and ResumeThread functions. I would really appreciate if someone can help me on this (how to suspend, resume and close threads whereever I need) using the above example.
thanks,
-Pav
-- modified at 17:09 Wednesday 4th January, 2006
|
|
|
|
|
If you call C-run time functions in any of your thread functions, you should be using beginthreadex instead of CreateThread.
If you are using MFC, you should use AfxBeginThread instead of beginthreadex.
Having said that, use the thread handle to ResumeThread to get it started again. You can check return value to decide if it was supposed to actually resume or not. If the return value is zero, the specified thread was not suspended. If the return value is 1, the specified thread was suspended but was restarted. If the return value is greater than 1, the specified thread is still suspended.
Use the handle to the thread to SuspendThread. Be mindful of this warning:
Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.
Marriage slows down your coding, a baby slows it down even more!
|
|
|
|
|
Hi Blake,
thanks for ur reply. I am not able to figure out which one is the thread handle, while I am using my thread name ThreadId_1 (assuming its the handle), it is giving syntax error.
Actually in my program I need to make anyone of the threads to be active at any time depending on the user input. So i need to suspend and resume them accordingly. I am not getting the correct syntax on using those functions. Can you gimme some sample syntax code for my example. I am using these threads in my MFC application, but these threads wont handle any windows, but only to do the processing.
I did have another version of my program with AfxBeginThread, but there too I need to know the syntax.
thanks,
-Pav
|
|
|
|
|
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
);
In other words, the value RETURNED from CreateThread is what is used in the calls to Resume and Suspend.
Or, you can get the thread handle using OpenThread, if you know the thread identifier.
HANDLE OpenThread(
DWORD dwDesiredAccess,
BOOL bInheritHandle,
DWORD dwThreadId
);
Be sure to close the handle you opened if oyu use this second approach.
I usually retain the thread handle returnd from CreateThread instead of constantly opening and closing the thread handle.
Marriage slows down your coding, a baby slows it down even more!
|
|
|
|
|
Marriage slows down your coding, a baby slows it down even more!
thats something really true ,
actually marriage changes lots of things??
Vikas Amin
Embin Technology
Bombay
vikas.amin@embin.com
|
|
|
|
|
Hi!
For some reason, when I'm calling SetBkColor on a device context, the color is just not changing. I've used this function dozens of times in other programs I've written and it's always worked. I have no idea why it isn't working now.
So my code looks like this:
SetBkColor(the_HDC, RGB(100, 255, 255));
TextOut(the_HDC, 100, 100, text, strlen(text));
The text draws fine, but it's always black. The variable 'text' is frequently changed, and always draws correctly, but the color does not change from RGB(0,0,0)
I'm not using double-buffering in this program so the HDC drawn to is the one the user is looking at. What could be wrong?
Thanks!
Kelly Ryan
|
|
|
|
|
SetBkColor sets the background color. Have you seen SetTextColor?
--
I've killed again, haven't I?
|
|
|
|
|
.... I guess I shouldn't be coding on 2 hours sleep with the flu.....
Thanks... I've only used the darned thing like 50 times...
Geesh... I need to go to bed, sorry for wasting your time.
Kelly Ryan
|
|
|
|
|
No problem!
Heck, I spent two days this week looking for some kind of weird regex parser/token extractor combo, then realized "strspn" did all I needed... and I'm healthy, well-rested, and just coming off a week of vacation!
--
I've killed again, haven't I?
|
|
|
|
|
I need C++ code to solve exercises below:
• First, read the text file and show 8 choices for an operator.
• 1) Make a list for first “N” word of frequency repeated.
• 2) Make a list for even “M” word that close to each other.
• 3) Word count.
• 4) Paragraph count.
• 5) Witch paragraph is the largest one – show it by first 3 letter and number of that paragraph.
• 6) Sentence count – each sentences are up to “Dot point”.
• 7) Get a word from operator and after search, show how many used.
• 8) Get a list of words and after search, show how many used by percent.
I am Pouya Vatanpour, from Iran and I study in a college of computer science.
I used Deitel book – programming with C++ 2editin - up to chapter six.
I am waiting for your help and so many thanks if you tell me more ASAP.
Best Regards,
vatanpour@msn.com / vatanpoor@gmail.com
Pouya
|
|
|
|
|
I really hate to tell you this, but you will get more help if you try doing your own homework first. Then ask specific questions.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
|
|
|
|
|
The least you could do is to not word it like a homework assignment. Show some initiative, and then ask specific questions. You'll get tons more help that way. See here for more.
"The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb
|
|
|
|
|
What puzzles me is why people opt for a Computer Science course and then don't want to write code.
|
|
|
|
|
Nishant Sivakumar wrote: What puzzles me is why people opt for a Computer Science course and then don't want to write code.
They think learning computer programming is easy and earns big bucks.
-Prakash
|
|
|
|
|
pvatanpour wrote: Witch paragraph is the largest one
How do you know she's a witch ?
Sorry, I don't usually make fun of people who can't spell, especially people who don't speak English. However, more than anything, you need to do your own homework, and you need to learn to look things up on the web. There are a million sites only a google away with all the info you need to do this stuff.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
And, should he suffer the paragraph to die?
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
I'm trying to detect if I currently have a connection to the network either over LAN or wireless. If I use IsNetworkAlive, it returns TRUE even if the wireless card is removed from the machine, there is no cable in the wired adapter and there are no active dialup connections. If I disable the wired LAN adapter from control panel, the function works as expected.
Any ideas on how I can test for network TCP/IP connectivity without disabling any adapters? IsDestinationReachable won't work for me because the application will be run behind a firewall.
Thanks
modified 12-Jul-20 21:01pm.
|
|
|
|
|
Is this of any help?
"The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb
|
|
|
|
|
That's what I've been using for a reference, none of the other methods really seem to apply to me.
modified 12-Jul-20 21:01pm.
|
|
|
|
|
How can I do to include ActiveX Controls to a MFC DLL Project, because when I do that in a dialog and execute this, the dialog isn`t show, thanks
|
|
|
|
|
|
|
# Improve performance and ability to work directly with hardware
# Fit into the real world
This two are more important for me as i work in Embedded domain
Vikas Amin
Embin Technology
Bombay
vikas.amin@embin.com
|
|
|
|
|
|
Hello there,
I have a dialog based application which has two classes, CMainWindow and CChildWindow. I want to create the ChildWindow from the MainWindow, from inside a thread.
My code snippet.
static UINT CMainWindow::_ShowChildWindow(LPVOID parameter)
{
CMainWindow *mainWind = (CMainWindow*) parameter;
mainWind->ShowChildWindow();
return 0;
}
CMainWindow::ShowChildWindow()
{
m_childwindow = new CChildWindow();
m_childwindow->Create(IDD_CHILDWINDOW);
m_childwindow->ShowWindow(SW_SHOW);
}
CMainWindow::OnButtonShow()
{
AfxBeginThread(_ShowChildWindow,this);
}
The newly created window just blinks and vanishes.
Please help me!
Regards
|
|
|
|
|