473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Cant put a wait on external application?

11
Hello all,

I have problems letting this application wait. I tried everything in my knowledge.
Here is the code:

Expand|Select|Wrap|Line Numbers
  1.                     System.Diagnostics.Process procdemux = new System.Diagnostics.Process();
  2.                     procdemux.StartInfo.UseShellExecute = true;
  3.                     procdemux.StartInfo.CreateNoWindow = false;
  4.                     procdemux.StartInfo.RedirectStandardOutput = false;
  5.                     procdemux.StartInfo.FileName = currentPath + "\\res\\demux.bat";
  6.                     procdemux.Start();
  7.                     procdemux.WaitForExit(3000);
  8.                     procdemux.Close();
  9.  
this code follows it up:

Expand|Select|Wrap|Line Numbers
  1.             System.Diagnostics.Process procencode = new System.Diagnostics.Process();
  2.             procencode.StartInfo.UseShellExecute = true;
  3.             procencode.StartInfo.CreateNoWindow = false;
  4.             procencode.StartInfo.RedirectStandardOutput = false;
  5.             procencode.StartInfo.FileName = currentPath + "\\res\\encode.bat";
  6.             procencode.Start();
  7.             procencode.WaitForExit(3000);
  8.             procencode.Close(); 
  9.  
Can someone please help me?

Thanks in advance!

Regards,

Dennis
Jun 4 '07 #1
12 1406
FLX
11
please, someone?

Regards,

Dennis
Jun 6 '07 #2
Atran
319 100+
Hello, I do not know this way, but I learn another way to stop your app:

Expand|Select|Wrap|Line Numbers
  1. //Stop your app for 1000 milliseconds.
  2. System.Threading.Thread.Sleep(1000);
  3.  
This code will stop your app for 1 sec.
--------------------------------------------------------
But If you mean to wait for load something, I do not know.
"TRScheel" user help me with the code I give you.
Hope this help you.
Jun 6 '07 #3
mwalts
38
Hello, I do not know this way, but I learn another way to stop your app:

Expand|Select|Wrap|Line Numbers
  1. //Stop your app for 1000 milliseconds.
  2. System.Threading.Thread.Sleep(1000);
  3.  
This code will stop your app for 1 sec.
--------------------------------------------------------
But If you mean to wait for load something, I do not know.
"TRScheel" user help me with the code I give you.
Hope this help you.
If you don't supply a time for the WaitForExit() call it will wait for as long as it takes the application to exit. Could you try explaining your problem a little bit better? I'm not sure I get what your asking.

-mwalts
Jun 6 '07 #4
Plater
7,872 Expert 4TB
You're currently only waiting 3 seconds before you are manually closing that process. Seems a bit quick?
Jun 6 '07 #5
I have a similar problem. I want to run an external app for 20 seconds and then to close.

i am using waitforexit(20000) but it isn't working.
can u tell me how to close an application after 20 seconds ???
pls help me


private void button2_Click(object sender, EventArgs e)
{

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"calc.exe";
p.Start();
p.WaitForExit(20000);


}
Jun 24 '07 #6
I have a similar problem. I want to run an external app for 20 seconds and then to close.

i am using waitforexit(20000) but it isn't working.
can u tell me how to close an application after 20 seconds ???
pls help me


private void button2_Click(object sender, EventArgs e)
{

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"calc.exe";
p.Start();
p.WaitForExit(20000);


}
This doesn't seem a good approach to me. In Win32 programming we had the WaitForSingleObject method, but in .NET....I don't know...yet.

If the application you're starting is also written by you, then you could create a small protocol using a semaphore file:
- create a semaphore file
- call the second app
- in the second app, before closing delete the semaphore file
- meanwhile in the main .NET app, check for the existence of the semaphore file every 500 ms or so:

while(File.Exists("sem.txt"))
{
Thread.Sleep(500);
}
Jun 25 '07 #7
This doesn't seem a good approach to me. In Win32 programming we had the WaitForSingleObject method, but in .NET....I don't know...yet.

If the application you're starting is also written by you, then you could create a small protocol using a semaphore file:
- create a semaphore file
- call the second app
- in the second app, before closing delete the semaphore file
- meanwhile in the main .NET app, check for the existence of the semaphore file every 500 ms or so:

while(File.Exists("sem.txt"))
{
Thread.Sleep(500);
}


The application I want to terminate after the 20 seconds is SerialMagic, so I don't have access to the code.
Jun 25 '07 #8
Plater
7,872 Expert 4TB
Try this program flow:
Start your external application (using Process)
Tell the current application to wait (Thread.CurrentThread.Sleep()?) the desired time
Kill the external program (using the Process object from earlier)

I am not sure what your problem with the initial code was.
An external program was started
It was given 3seconds of time to open and run before you closed it
Jun 26 '07 #9
TRScheel
638 Expert 512MB
Why havent I seen this thread earlier?

Anyways, this will be useful to you:

Expand|Select|Wrap|Line Numbers
  1. private static bool CheckProcess(Process process)
  2. {
  3.     bool processFinished = true;
  4.     try
  5.     {
  6.         process.Refresh();
  7.         foreach (ProcessThread thread in process.Threads)
  8.         {
  9.             if (thread.ThreadState != System.Diagnostics.ThreadState.Terminated)
  10.                 processFinished = false;
  11.         }
  12.     }
  13.     catch
  14.     {
  15.         processFinished = true;
  16.     }
  17.  
  18.     return processFinished;
  19. }
Just put that in a while loop, so something like:

Expand|Select|Wrap|Line Numbers
  1. while (!CheckProcess(process)) { }
  2.  

If it refreshes the process when its disposed of, it will throw an error, so thats why we have the catch bit just return true. Otherwise, we look to find it when its terminated (fat chance catching right on THAT moment, hence the try/catch).

You are much more likely to run process.Refresh() and have it throw an exception than get the ThreadState to == Terminated when you check. Hence, if you want to be REALLY lazy, you could just see if you could refresh the process and see if that was possible. You might want to throw in the Wait and Unknown depending if the program just sits there after its done (and play around with the output to find out when its finished). In the above example, i was running a number of scripts that exited when done, so I knew that if I couldnt talk to it, they were done.
Jun 26 '07 #10
TRScheel
638 Expert 512MB
You can put a thread.sleep(#) in the loop to lessen the CPU load of sitting there just looping. Something like 100, or 250, shouldnt be too noticable.
Jun 26 '07 #11
Here's another idea:

Do a while loop in which you check to see that the process you started is still running. If not, break out of the loop and continue on else loop again.
This way you don't have to guess how long it will take.

The Process class has all you need for this.
Jun 27 '07 #12
TRScheel
638 Expert 512MB
Here's another idea:

Do a while loop in which you check to see that the process you started is still running. If not, break out of the loop and continue on else loop again.
This way you don't have to guess how long it will take.

The Process class has all you need for this.
I fooled around with the process.HasExited but the problem exists that the program might just sit there doing nothing. Its threadstate gets stuck at unknown, wait, or something else. What's it doing? Waiting for user input? Printing? Done? Its a tough issue to take up depending on the program you are using.
Jun 27 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: S. van Beek | last post by:
Dear reader, In case an application is ussing an external model library you have to address this external model library in your application. This addressing takes place in a module of the...
4
by: Peter | last post by:
I am using CreateProcessWithLogonW to run an external application, the problem I am having is that the C# program does not wait until the external program finishes running before continuing. The...
4
by: Terry Brown | last post by:
I am trying to use WaitForExit to determine when an external application I have launched has exited (duh!). I found examples everywhere and it seemed simple and this works perfectly: Dim...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
1
by: Divyesh | last post by:
Hello, I need to call external application from my C# application. I can use Process.Start(<FileName>) for that but I also need to wait for that application to finish before I process. Any...
0
by: NvrBst | last post by:
I want to send a bunch of keys to an external window using PostMessage. I send a "KEYDOWN" message (then have to wait for the program to produce the CHAR message) then send the "KEYUP" message. ...
5
by: Nayan | last post by:
Hi, If I make a call to function which is in external library, and it goes into wait sate.. disabling my app to proceed further, how can I break this state elegantly? So far, I had to kill my...
1
by: Gunnar G | last post by:
If I wish to run an external application with exec(), does the execution of the PHP script halt until the external application has finished?
1
by: JasonT | last post by:
Hi. I have created a dll in VC++ 08 that exports a CLR windows form class. I call into this dll from an external (closed source, third-party) application to instantiate the form and embed it into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.