processes and mutexes

P

pbd22

Hi.

I have a C# program that fires an external VB6 program which writes to
a file and terminates.
It is ugly, but this is how I have to do it. I cannot change this part
of the program.

The problem I am encountering is that the former process created by
this EXE is not complete
(it runs for a few seconds) when the next one wants to start. A
classic multi-threading situation (i think).

The current code looks like this:

proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = path2exe;
proc.StartInfo.Arguments = videoname;
proc.Start();

(and, I eventually added... proc.Kill(););

A friend in another forum suggested I use Mutexes as a way of
queuing up requests - attaching to and detaching from the process as
needed.
He also suggested a timer given the longer length that the process
takes to complete.

I have never worked with mutexes before and am wondering if somebody
out there
could show me how this is done (or, at least, tell me what I am doing
wrong and where
to go).

I would appreciate some feedback on my first attempt:

static void somemethod()
{

Mutex mutex = new Mutex(false, "domain.com");

try
{
mutex.WaitOne();
Timer tmr = new Timer(MpgWriter);
tmr.Dispose();
}
finally
{
mutex.ReleaseMutex();
}


}

static void MpgWriter(object data)
{
Console.WriteLine("c:/myprocess.exe command1,
command2");
}




Thanks again for your help.
 
S

Stephany Young

I think you're barking up the wrong tree.

Add:

proc.WaitForExit();

after:

proc.Start();
 
P

Peter Duniho

I have a C# program that fires an external VB6 program which writes to
a file and terminates.
It is ugly, but this is how I have to do it. I cannot change this part
of the program.

The problem I am encountering is that the former process created by
this EXE is not complete
(it runs for a few seconds) when the next one wants to start. A
classic multi-threading situation (i think).

Multi-process is not the same as multi-thread. So no, I don't think this
is a multi-thread situation.
The current code looks like this:

proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = path2exe;
proc.StartInfo.Arguments = videoname;
proc.Start();

(and, I eventually added... proc.Kill(););

Why? Why don't you just wait for the process to complete?
A friend in another forum suggested I use Mutexes as a way of
queuing up requests - attaching to and detaching from the process as
needed.
He also suggested a timer given the longer length that the process
takes to complete.

I don't see anything that suggests that a mutex might be helpful.

If the external program were under your control, you could use a named
mutex that the program uses to synchronize itself with other instances of
the program. But you don't, so that's not relevant.

The code you posted is a little bewildering to me. You don't start your
timer, you just dispose it right away, and there's nothing in your post
that suggests you'll ever have a given thread waiting on the same mutex.

As far as how you _would_ do this...

Within the program you have that spawns these other processes, if you want
to queue the processes so that they only run one at a time, then you need
to do that yourself. You're not specific about how your program actually
decides to start one of these processes, but assuming you have some
mechanism for doing that, then you should just create a queue to hold
pending requests if there's already one active. You can subscribe to the
Process.Exited event (don't forget to set the Process.EnableRaisingEvents
property), and when the handler for that event is called, you can check
the queue and see if there's another one that needs to be started.

For example:

Queue<ProcessStartInfo> _procq = new Queue<ProcessStartInfo>();
bool _fProcessRunning;
object _objLock = new object();

void QueueAProcess(string strExePath, string strArgs)
{
ProcessStartInfo psi = new ProcessStartInfo(strExePath, strArgs);

lock(_objLock)
{
if (_fProcessRunning)
{
_procq.Add(psi);
psi = null;
}

_fProcessRunning = true;
}

if (psi != null)
{
StartAProcess(psi);
}
}

void StartAProcess(ProcessStartInfo psi)
{
Process process = new Process();

process.StartInfo = psi;
process.EnableRaisingEvents = true;
process.Exited += ProcessExitedHandler;

process.Start();
}

void ProcessExitedHandler(object sender, EventArgs e)
{
ProcessStartInfo psi = null;

lock (_objLock)
{
if (_procq.Count > 0)
{
psi = _procq.Dequeue();
}
else
{
_fProcessRunning = false;
}
}

if (psi != null)
{
StartAProcess(psi);
}
}

The code above makes no attempt to timeout the process. That would be a
completely separate design requirement, but so far you haven't provided
any suggestion as to why you might want a timer. Assuming you want the
processes to only run one at a time, the above will do that.

Pete
 
P

pbd22

Pete,

That bit of code is exactly what I needed.
Indeed, the processes should que and run
one at a time, sequentially. I tried this in
my program and it does the job. I need
to get my head wrapped fully around the
concepts at play, but it works and I am
indebted.

Thanks a bunch.
 

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