473,789 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Diagnost ics.Process();
proc.EnableRais ingEvents = 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.ReleaseMu tex();
}
}

static void MpgWriter(objec t data)
{
Console.WriteLi ne("c:/myprocess.exe command1,
command2");
}


Thanks again for your help.
Jan 7 '08 #1
3 2222
I think you're barking up the wrong tree.

Add:

proc.WaitForExi t();

after:

proc.Start();
"pbd22" <du*****@gmail. comwrote in message
news:48******** *************** ***********@l1g 2000hsa.googleg roups.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.Diagnost ics.Process();
proc.EnableRais ingEvents = 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.ReleaseMu tex();
}
}

static void MpgWriter(objec t data)
{
Console.WriteLi ne("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.Diagnost ics.Process();
proc.EnableRais ingEvents = 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.EnableR aisingEvents
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<ProcessSt artInfo_procq = new Queue<ProcessSt artInfo>();
bool _fProcessRunnin g;
object _objLock = new object();

void QueueAProcess(s tring strExePath, string strArgs)
{
ProcessStartInf o psi = new ProcessStartInf o(strExePath, strArgs);

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

_fProcessRunnin g = true;
}

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

void StartAProcess(P rocessStartInfo psi)
{
Process process = new Process();

process.StartIn fo = psi;
process.EnableR aisingEvents = true;
process.Exited += ProcessExitedHa ndler;

process.Start() ;
}

void ProcessExitedHa ndler(object sender, EventArgs e)
{
ProcessStartInf o psi = null;

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

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

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
4896
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
2071
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 processes are non-GUI processes. The problem that I am running into is that the Process.Start(startInfo) returns immediately to Process 1. Therefore, process 1 does not wait on its own for Process 2 to initialize. Process 2 can take a few...
16
10141
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? Thanks a lot!
9
23084
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 to a pool. If I assign only one application to a applicaton pool and have multiple worker processes assigned to that pool. Will my application be processed by many worker processes?
2
1625
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
5715
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 member initialization list. First of all, does anyone have any opinion about whether this will work fine. I think it will. I believe a copy constructor, when using a member initialization list, does a memory copy of the object's members to be copied....
35
4044
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? I've been leaning towards threads, I'm going to say why. Processes seem fairly expensive from my research so far. Each fork copies the entire contents of memory into the new process. There's also a more expensive context switch between...
1
3569
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 many of the others in the class. We haven't been exposed to any Unix and the professor hasn't, yet, said much more than the book, which is no help at this point. From what I've figured out and what I've blindly seen on the net and hoped would...
8
2198
by: Raxit | last post by:
Hi, In Mulithreaded program, using Posix api, we do pthread_mutex_lock(&Lock)
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9499
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10374
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2898
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.