Pausing an execution of a program

G

greg

Hi,

Can anyone help me with the following issue:
How can I pause the execution of a program until a given file is
created (by another process) in a specified directory?

Any ideas would be highly appreciated.

Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

Greg,

Short answer, you can't, not unless the program exposes a mechanism by
which to do so. Most programs won't do this though, as it is an easy way to
mess with program execution.

What are you trying to do?
 
V

VistaDB

Can anyone help me with the following issue:
How can I pause the execution of a program until a given file is
created (by another process) in a specified directory?

Any ideas would be highly appreciated.


Why not have something wait until the file exists, THEN call the
program to go get it?

If the app is command line driven you can wait for the file, and then
send off the app to process it.
 
G

greg

Greg,

Short answer, you can't, not unless the program exposes a mechanism by
which to do so. Most programs won't do this though, as it is an easy way to
mess with program execution.

What are you trying to do?

Here is the general picture. From Main I am starting a process which
is supposed to do some calculations and ouput a file. Since I need the
file for the rest of the program, I am using Process.WaitForExit().
However, the process started in Main can restart itself and if that
happens the rest of the code in Main is executed. This is bad since
there is not outputted file yet. Thus, what I was hoping to achieve is
use something that will halt the program until a file is created - a
handler?
 
K

KWienhold

Using FileWatcher seems to be a good approach. However, I need the
rest of the program to pause until a create-filename event occurs. Is
there another way of doing this besides using breaking from an
infinite while loop? Please tell me that there is, I am a firm
believer of the existence of elegant code :)

Thanks

Its a little difficult to tell without knowing the structure of your
program, but in general you should be able to move the code that
follows your call to the other application into the eventhandler for
the appropriate FileWatcher-event, thusly executing it when the file
was created.

hth,
Kevin Wienhold
 
H

harifajri

Its a little difficult to tell without knowing the structure of your
program, but in general you should be able to move the code that
follows your call to the other application into the eventhandler for
the appropriate FileWatcher-event, thusly executing it when the file
was created.

hth,
Kevin Wienhold

hi greg, I think you should use Multi Threading mechanism.

using System.Threading;
....
Thread mythread = new Thread(new ThreadStart(myCalculation));
mythread.Start();
// you can set time out, of how many seconds/minutes you can wait,
// or you can wait forever until mythread

while (true)
{
//Console.Sleep(10000) // check every 10 seconds
if ( ! mythread.IsAlive || times is up){
break;
}
... do something
}
 
P

Peter Duniho

Using FileWatcher seems to be a good approach. However, I need the
rest of the program to pause until a create-filename event occurs. Is
there another way of doing this besides using breaking from an
infinite while loop? Please tell me that there is, I am a firm
believer of the existence of elegant code :)

Define "pause". Typically, a GUI application isn't doing anything until
the user tells it to. There are exceptions, of course. Some applications
act as network clients or servers, for example, and even without direct
user interaction have certain ongoing processing. However, they are just
exceptions.

More importantly, it's not really possible to tell you how to "pause" your
application without knowing more about it. A typical GUI application
would not need "pausing", because it's not doing anything anyway. What is
it exactly that you mean by "pause", and what would your application be
doing if it were not "paused"?

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top