|
Hi
I started 2 processes in my app. and now every one second i want to see if these processes are still running. Coud anyone please tell me how i can get the status of a process to see whether it is running or has been terminated?
thanks
|
|
|
|
|
tcuong wrote:
now every one second i want to see if these processes are still running
Why 'every one second'? Can't you create a thread which waits for process termination with WaitForSingleObject, then posts a message?
Tomasz Sowinski -- http://www.shooltz.com
Never argue with an idiot, he'll bring you to his level and beat you with experience.
|
|
|
|
|
this is to monitor if the processes are running or not? It is just an excercise to get myselft familiar with Win32 programming.
First i create 5 process namely notepad.exe . Then i have to monitor that how many of these processes are running every 1 second. I've attempted many ways to do it but i failed. This is how i did it.
<br />
void main()<br />
{<br />
STARTUPINFO startUpInfo[5];<br />
PROCESS_INFORMATION proInfo[5];<br />
BOOL success;<br />
HANDLE proHandle[5];<br />
INT i;<br />
INT runningProcess = 5;<br />
CHAR appName[100];<br />
<br />
<br />
<br />
<br />
strcpy(appName, "c:\\windows\\notepad.exe");<br />
<br />
<br />
<br />
<br />
for(i = 0; i <5; i++)<br />
GetStartupInfo(&startUpInfo[i]);<br />
<br />
<br />
for(i = 0; i < 5; i++)<br />
{<br />
success = CreateProcess(appName, 0, 0, 0, FALSE, 0, 0,0,&startUpInfo[i], &proInfo[i]);<br />
<br />
if(!success)<br />
cout <<" Error creating process: "<< GetLastError() <<endl;<br />
<br />
cout << "The process ID is " << proInfo[i].dwProcessId << endl;<br />
}<br />
<br />
for(i = 0; i <5; i++)<br />
{<br />
proHandle[i] = proInfo[i].hProcess;<br />
}<br />
<br />
while (WaitForMultipleObjects(5, proHandle, TRUE, 1000) == WAIT_TIMEOUT)<br />
{<br />
for(i = 0; i < 5; i++)<br />
{<br />
if(proInfo[i].hProcess == NULL)<br />
runningProcess--;<br />
<br />
}<br />
<br />
<br />
printf(" %d ", runningProcess); <br />
I know that this code may look silly to some of you but instead of laughing, please guide me to the right direction. Thanks
|
|
|
|
|
tcuong wrote:
instead of laughing, please guide me to the right direction.
Nobody laughs at you. Seriously.
If you insist on using WaitForMultipleObjects:
- proInfo[i] will not be set to NULL when process terminates
- setting 3rd argument to TRUE causes WaitForMultipleObjects to exit only if timeout occured or *all* processes have terminated. This is obviously not what you're looking for.
Tomasz Sowinski -- http://www.shooltz.com
Never argue with an idiot, he'll bring you to his level and beat you with experience.
|
|
|
|
|
tcuong wrote:
while (WaitForMultipleObjects(5, proHandle, TRUE, 1000) == WAIT_TIMEOUT)
{
for(i = 0; i < 5; i++)
{
if(proInfo[i].hProcess == NULL)
runningProcess--;
}
printf(" %d ", runningProcess);
}
This code looks neither silly nor anybody wants to laugh on you, really!
First I thought the problem is the TRUE parameter for bWaitAll in your call to WaitForMultipleObjects(), (which is a common mistake) but then I realized that you want to loop until all handles reached signaled state (a process or thread handle becomes signaled if it terminates). Quite clever!
Your problem is, that a signaled handle is not set to NULL, it is still present. Therfore your process counter is never decremented. If you change the line
if(proInfo[i].hProcess == NULL)
runningProcess--;
to
if( WaitForSingleObject( proInfo[i].hProcess, 0 ) == WAIT_OBJECT_0 )
runningProcess--;
it should work. (Note that I just wrote the above from scratch and did not test it )
However, your description intents that you want to check the status exactly every 1 second. The above checks it at least every 1 second. This is because the WaitForMultipleObjects() returns if either a handle becomes signaled or the timeout elapsed. Of course, this is a negligible detail in almost any case, but hey, you wrote you want to learn how to solve it with Win32 API
One idea is to use Sleep() instead of WaitForMultipleObjects():
while ( runningProcess > 0)
{
for(i = 0; i < 5; i++)
{
if( WaitForSingleObject( proInfo[i].hProcess, 0 ) == WAIT_OBJECT_0 )
runningProcess--;
}
printf(" %d ", runningProcess);
Sleep( 1000 );
}
BTW: If you bracket your code postings to the forum with <pre> and </pre> tags, the formatting will survive. This would makes it much more comfortable to read it.
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
Daniel! This is a big help! I really appreciate it! Thank you very much!
Best Regard
|
|
|
|
|
Hi Daniel! I've got it. I just post it up here to let you know that with your help. FULL credit to you!!!
BOOL flag[5];
for(i = 0; i < 5; i++)
flag[i] = true;
while (WaitForMultipleObjects(5, proHandle, TRUE, 1000) == WAIT_TIMEOUT)
{
if( WaitForSingleObject( proInfo[0].hProcess, 0 ) == WAIT_OBJECT_0 && flag[0] == true)
{
runningProcess--;
flag[0] = false;
}
if( WaitForSingleObject( proInfo[1].hProcess, 0 ) == WAIT_OBJECT_0 && flag[1] == true)
{
runningProcess--;
flag[1] = false;
}
if( WaitForSingleObject( proInfo[2].hProcess, 0 ) == WAIT_OBJECT_0 && flag[2] == true)
{
runningProcess--;
flag[2] = false;
}
if( WaitForSingleObject( proInfo[3].hProcess, 0 ) == WAIT_OBJECT_0 && flag[3] == true)
{
runningProcess--;
flag[3] = false;
}
if( WaitForSingleObject( proInfo[4].hProcess, 0 ) == WAIT_OBJECT_0 && flag[4] == true)
{
runningProcess--;
flag[4] = false;
}
printf(" %d ", runningProcess);
}
This might look lengthy and messy for a moment but i think i can tight it in a loop!! Thanks!!!!
Also thanks to Tomasz !! I greatly appreciate it!!!
|
|
|
|
|
And this is the code after i've shortened it!!!
while (WaitForMultipleObjects(5, proHandle, TRUE, 1000) == WAIT_TIMEOUT)
{
for(i = 0; i< 5; i++)
{
if( WaitForSingleObject( proInfo[i].hProcess, 0 ) == WAIT_OBJECT_0 && flag[i] == true)
{
runningProcess--;
flag[i] = false;
}
}
printf(" %d ", runningProcess);
}
Thanks for the tips Daniel (remember the pre tag)
|
|
|
|
|
Could someone give me a clear definition of serialization. The way I see it is that it is a method to transfer data from memory to file. I may be completely wrong here and I would really like to know the correct meaning. Thanks.
Nick Parker
|
|
|
|
|
Nick Parker wrote:
The way I see it is that it is a method to transfer data from memory to file.
'From' side: usually, you have an 'object graph' - objects related to each other (parent/child relationships, etc). You need to 'flatten' this structure somehow, because you'll need a serial stream of bytes. See 'To' side.
'To' side: not necessarily file. Just sequential stream of bytes - it may be file, network, another memory location, telepatic transceiver or whatever.
Tomasz Sowinski -- http://www.shooltz.com
Never argue with an idiot, he'll bring you to his level and beat you with experience.
|
|
|
|
|
Ok, I think I understand what you are saying. This provides a method to transport the data (generically speaking) from point A to point B, is that correct?
Tomasz Sowinski wrote:
'From' side: usually, you have an 'object graph' - objects related to each other (parent/child relationships, etc). You need to 'flatten' this structure somehow, because you'll need a serial stream of bytes. See 'To' side.
Could you explain this a little more?
Nick Parker
|
|
|
|
|
I'm assuming that you want me to explain the 'object graph' thing.
Imagine that you have CDocument derived class. It usually contains some data. They may be integers, strings, arrays etc. Serializing them is easy - you're just writing/reading their binary representation.
Now - what happens when you have a pointer in your document? What if it points to an object which has a 'backpointer' to document? These objects connected together are called 'object graph'. Any serialization mechanism must be ready to deal with such situation. Two most important points are:
- you may have polymorphic objects and pointers of type 'CYourBaseObject *'. But pointers point to CYourConcreteType1 or CYetAnotherTypeDerivedFromYourBaseObject. On the deserialization side, you want the real type to be preserved.
- you may more than one pointer to the same object. Both sides of operation must be smart enough to serialize object data only once.
Luckily, as an user of MFC or .NET, you don't need to worry about such issues.
Tomasz Sowinski -- http://www.shooltz.com
Never argue with an idiot, he'll bring you to his level and beat you with experience.
|
|
|
|
|
This is a flow chart of me trying to follow what you just said:
Tomasz Sowinski wrote:
Imagine that you have CDocument derived class. It usually contains some data. They may be integers, strings, arrays etc. Serializing them is easy - you're just writing/reading their binary representation.
Understood.
Tomasz Sowinski wrote:
Now - what happens when you have a pointer in your document?
Starting to get lost...
Tomasz Sowinski wrote:
What if it points to an object which has a 'backpointer' to document?
Fully lost now.
Tomasz Sowinski wrote:
These objects connected together are called 'object graph'.
Okay.
Tomasz Sowinski wrote:
Luckily, as an user of MFC or .NET, you don't need to worry about such issues.
Thank goodness, I thought I was going to have to be lost again.
Nick Parker
|
|
|
|
|
Nick Parker wrote:
Now - what happens when you have a pointer in your document?
Starting to get lost...
Quick example:
You have MFC doc/view app. Your doc class is CWidgetDoc. You've defined a CWidget class, which is a base for CBlueWidget, CTriangleWidget and CStereoWidget.
CWidget class has the following member variable:
CWidgetDoc *m_pParentDoc;
In your CWidgetDoc, you store unlimited number of widgets. You're using std::vector for that:
std::vector<CWidget *> m_widgets;
Your users work with the app and create blue/triangle/stereo widgets. m_widgets grows and contains pointers to different types of objects; all they have in common is the base class, CWidget.
There are pointers going from document to widget. There are pointers going from widgets to document. This is simple object graph - with complicated program, you'd have more tangled structure.
Tomasz Sowinski -- http://www.shooltz.com
Never argue with an idiot, he'll bring you to his level and beat you with experience.
|
|
|
|
|
Tomasz Sowinski wrote:
There are pointers going from document to widget. There are pointers going from widgets to document. This is simple object graph - with complicated program, you'd have more tangled structure.
Gotcha, thanks Tomasz
Nick Parker
|
|
|
|
|
Serialization (to make somthing serial) is transforming an (arbitrary complex) data structure from a random access data store (like memory) into a sequential stream of bytes (like a disk file). Deserialization consequently is the other way round.
Memory is a random access data store. This means that you can think of it as a big array of bytes and you can access any byte in this array through an index. This index is usually called the "address" of the byte. Any non-trivial data structure (like a tree) is spread over the memory, consisting of chunks which are connected to each other via pointers.
However, if you need to transmit your data through a serial line, like a network connection, you have to send it "byte per byte". This is called serialisation.
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
I am going to use the clone method to make a duplicate.THe rs is supposed to be on that can support bookmarks. How do we find out if it can or not?
Do the two recordsets in question both have to be createInstance 'd?
Thanks,
ns
|
|
|
|
|
Hello everybody:
I have a Dialog with a combo box. I supposed to pick a value from the combo box and then when I close the dialog and open the same dialog again, the value that I was selected before closing the dialog is gone. Is there a way of keeping those values when opening and closing a dialog? Any answer is more than welcome.
Thanks and have a good day,
Luis E. Cuadrado
)
|
|
|
|
|
maybe you should use GetCurSel before destructing CDialog object and SetCurSel after creating CDialog again?
nobody is perfect
|
|
|
|
|
Are you using DDX_ routines in DoDataExchange, or rather you've done data transfer yourself?
Tomasz Sowinski -- http://www.shooltz.com
Never argue with an idiot, he'll bring you to his level and beat you with experience.
|
|
|
|
|
Hello:
Thanks for the reply. DDX_routines? I think I heard about those but I don't think I'm using those in my application. I was going to do data transfer my self but looks like DDX_ routines is the best way, I guess. Is there any resource over the internet that I can find about DDX_routines?
Thanks,
Luis E. Cuadrado
)
|
|
|
|
|
Ok, I'll try to keep it simple. Did you use ClassWizard to associate some variable with combo box? Or you're just calling Get/SetCurSel?
Tomasz Sowinski -- http://www.shooltz.com
Never argue with an idiot, he'll bring you to his level and beat you with experience.
|
|
|
|
|
Yes, I used ClassWizard to add a variable to the ComboBox. With that variable I call GetCurSel(). Sorry if my questions sound kind of newbie. I'm starting with MFC not long ago ).
Thanks,
Luis E. Cuadrado
)
|
|
|
|
|
Luis E. Cuadrado wrote:
With that variable I call GetCurSel().
Variable is 'control' (CComboBox) or 'value' (int)?
Tomasz Sowinski -- http://www.shooltz.com
Never argue with an idiot, he'll bring you to his level and beat you with experience.
|
|
|
|
|
I want to convert emf to jpeg,png or gif.
So pl refer some urls. so i need dll
|
|
|
|