473,320 Members | 2,189 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,320 software developers and data experts.

processes and mutexes


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.
Jan 7 '08 #1
3 2199
I think you're barking up the wrong tree.

Add:

proc.WaitForExit();

after:

proc.Start();
"pbd22" <du*****@gmail.comwrote in message
news:48**********************************@l1g2000h sa.googlegroups.com...
>
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.
Jan 7 '08 #2
On Sun, 06 Jan 2008 17:04:22 -0800, pbd22 <du*****@gmail.comwrote:
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
Jan 7 '08 #3
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.

Jan 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Dirk Reske | last post by:
Hello, why doesn't this code work correctly? private int GetCpuUsage(Process proc) { DateTime time1,time2; TimeSpan timediff; double cpu1,cpu2,cpudiff;
5
by: Chris B | last post by:
I have the following situation: Process 1 creates Process 2 (using Process.Start(startInfo) Process 1 needs to wait until Process 2 is initialized before Process 1 can continue to execute Both...
16
by: Elad | last post by:
Hi, I have an application that is made up of several executables. I need all these executables to use the same instance of an object. What is the best, most efficient way to approach this? ...
9
by: Abhishek Srivastava | last post by:
Hello All, In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process per appliction pool. Each worker process is dedicated...
2
by: UJ | last post by:
Is there a way to get a list of all the mutexes that have already been defined? TIA - Jeff.
7
by: drawoh | last post by:
Hi All, I have a class that creates a thread, a mutex and a condition variable in its constructor. I am writing a copy constructor for this class in C++. I am doing a simple copy using the...
35
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? ...
1
by: jazon | last post by:
Let me start by saying this for an Operating Systems class. No, I don't expect the work to be done for me. The assignment is as follows: To be honest, I feel like a fish out of water, like...
8
by: Raxit | last post by:
Hi, In Mulithreaded program, using Posix api, we do pthread_mutex_lock(&Lock)
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.