473,796 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Many instances of the same program

I need to run more then 1 instance of the same program but also I need to
know which instance is running.
In C++ code was:
int nInstance = 0;
char szAppName[32];
m_dwMutexReturn = ERROR_ALREADY_E XISTS;
while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
MAXINSTANCES ))
{
nInstance++;
itoa( nInstance, szTemp, 10 );
strcpy( szAppName, "ProductEng ine" );
strcat( szAppName, szTemp );
m_hMutex = CreateMutex( NULL, TRUE, szAppName );
m_dwMutexReturn = GetLastError();
}

if ( nInstance == MAXINSTANCES )
m_sState->nInstance = 1;
else
m_sState->nInstance = nInstance;
but I can't port this code to C#.
Somebody knows how to do it?

Nov 8 '07 #1
6 2767
Bronislav,

There are a number of examples of how to do this on the web. Have you
taken a look at the Mutex class? It will give you everything you need to do
to replicate this behavior.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:2D******** *************** ***********@mic rosoft.com...
>I need to run more then 1 instance of the same program but also I need to
know which instance is running.
In C++ code was:
int nInstance = 0;
char szAppName[32];
m_dwMutexReturn = ERROR_ALREADY_E XISTS;
while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
MAXINSTANCES ))
{
nInstance++;
itoa( nInstance, szTemp, 10 );
strcpy( szAppName, "ProductEng ine" );
strcat( szAppName, szTemp );
m_hMutex = CreateMutex( NULL, TRUE, szAppName );
m_dwMutexReturn = GetLastError();
}

if ( nInstance == MAXINSTANCES )
m_sState->nInstance = 1;
else
m_sState->nInstance = nInstance;
but I can't port this code to C#.
Somebody knows how to do it?

Nov 8 '07 #2
The Mutex class in my understanding is not complete analog of the CreateMutex
API
because it not allowed to create difference instances of the programm with
the different name and I need exactly this.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Bronislav,

There are a number of examples of how to do this on the web. Have you
taken a look at the Mutex class? It will give you everything you need to do
to replicate this behavior.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:2D******** *************** ***********@mic rosoft.com...
I need to run more then 1 instance of the same program but also I need to
know which instance is running.
In C++ code was:
int nInstance = 0;
char szAppName[32];
m_dwMutexReturn = ERROR_ALREADY_E XISTS;
while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
MAXINSTANCES ))
{
nInstance++;
itoa( nInstance, szTemp, 10 );
strcpy( szAppName, "ProductEng ine" );
strcat( szAppName, szTemp );
m_hMutex = CreateMutex( NULL, TRUE, szAppName );
m_dwMutexReturn = GetLastError();
}

if ( nInstance == MAXINSTANCES )
m_sState->nInstance = 1;
else
m_sState->nInstance = nInstance;
but I can't port this code to C#.
Somebody knows how to do it?


Nov 8 '07 #3
Bronislav,

You can call the static OpenExisting method on the Mutex class to get a
named mutex instance. If it doesn't exist, then you can catch the
WaitHandleCanno tBeOpenedExcept ion instance that is thrown and then create a
new one.

Now, you do run the risk of running into a race condition here, as two
instances of the app can try to open a mutex that doesn't exist, which is
different from the call to CreateMutex. If you want to avoid this race
condition, declare the CreateMutex API function and call it through the
P/Invoke layer, getting the handle. Then, create a new Mutex instance, and
assign the handle to that if you want to use it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:7F******** *************** ***********@mic rosoft.com...
The Mutex class in my understanding is not complete analog of the
CreateMutex
API
because it not allowed to create difference instances of the programm with
the different name and I need exactly this.

"Nicholas Paldino [.NET/C# MVP]" wrote:
>Bronislav,

There are a number of examples of how to do this on the web. Have
you
taken a look at the Mutex class? It will give you everything you need to
do
to replicate this behavior.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:2D******* *************** ************@mi crosoft.com...
>I need to run more then 1 instance of the same program but also I need
to
know which instance is running.
In C++ code was:
int nInstance = 0;
char szAppName[32];
m_dwMutexReturn = ERROR_ALREADY_E XISTS;
while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
MAXINSTANCES ))
{
nInstance++;
itoa( nInstance, szTemp, 10 );
strcpy( szAppName, "ProductEng ine" );
strcat( szAppName, szTemp );
m_hMutex = CreateMutex( NULL, TRUE, szAppName );
m_dwMutexReturn = GetLastError();
}

if ( nInstance == MAXINSTANCES )
m_sState->nInstance = 1;
else
m_sState->nInstance = nInstance;
but I can't port this code to C#.
Somebody knows how to do it?



Nov 8 '07 #4
I tried to do exactly that but I cannot find how to call CreateMutex through
p-invoke
I tried both declaretions

/*
[DllImport("kern el32.dll", SetLastError=tr ue)]
private static extern int CreateMutexA(
ref SECURITY_ATTRIB UTES lpMutexAttribut es,
int bInitialOwner,
string lpName);
*/
[DllImport("kern el32.dll", SetLastError=tr ue)]
private static extern System.IntPtr CreateMutexA(
out int lpMutexAttribut es,
bool bInitialOwner,
string lpName);
but when I call CreateMutex
lhndlRetHandle = CreateMutexA(nu ll, true, lstrAppName);
It give me error that first parameter cannot be NULL
Thanks.
"Nicholas Paldino [.NET/C# MVP]" wrote:
Bronislav,

You can call the static OpenExisting method on the Mutex class to get a
named mutex instance. If it doesn't exist, then you can catch the
WaitHandleCanno tBeOpenedExcept ion instance that is thrown and then create a
new one.

Now, you do run the risk of running into a race condition here, as two
instances of the app can try to open a mutex that doesn't exist, which is
different from the call to CreateMutex. If you want to avoid this race
condition, declare the CreateMutex API function and call it through the
P/Invoke layer, getting the handle. Then, create a new Mutex instance, and
assign the handle to that if you want to use it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:7F******** *************** ***********@mic rosoft.com...
The Mutex class in my understanding is not complete analog of the
CreateMutex
API
because it not allowed to create difference instances of the programm with
the different name and I need exactly this.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Bronislav,

There are a number of examples of how to do this on the web. Have
you
taken a look at the Mutex class? It will give you everything you need to
do
to replicate this behavior.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:2D******** *************** ***********@mic rosoft.com...
I need to run more then 1 instance of the same program but also I need
to
know which instance is running.
In C++ code was:
int nInstance = 0;
char szAppName[32];
m_dwMutexReturn = ERROR_ALREADY_E XISTS;
while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
MAXINSTANCES ))
{
nInstance++;
itoa( nInstance, szTemp, 10 );
strcpy( szAppName, "ProductEng ine" );
strcat( szAppName, szTemp );
m_hMutex = CreateMutex( NULL, TRUE, szAppName );
m_dwMutexReturn = GetLastError();
}

if ( nInstance == MAXINSTANCES )
m_sState->nInstance = 1;
else
m_sState->nInstance = nInstance;
but I can't port this code to C#.
Somebody knows how to do it?



Nov 8 '07 #5
Well, you can declare lpMutexAttribut es as IntPtr if you are going to
pass null. Also, you should declare the bInitialOwner parameter as bool,
and you should indicate the version of the function you want to use in the
DllImport attribute and you should set the return type to IntPtr (since it
is a handle). Putting it all together, you should have:

[DllImport("kern el32.dll", SetLastError=Tr ue, CharSet=CharSet .Auto)]
private static extern IntPtr CreateMutex(Int Ptr lpMutexAttribut es, bool
bInitialOwner, string lpName);

Then, you can pass IntPtr.Zero to the first parameter.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:0C******** *************** ***********@mic rosoft.com...
>I tried to do exactly that but I cannot find how to call CreateMutex
through
p-invoke
I tried both declaretions

/*
[DllImport("kern el32.dll", SetLastError=tr ue)]
private static extern int CreateMutexA(
ref SECURITY_ATTRIB UTES lpMutexAttribut es,
int bInitialOwner,
string lpName);
*/
[DllImport("kern el32.dll", SetLastError=tr ue)]
private static extern System.IntPtr CreateMutexA(
out int lpMutexAttribut es,
bool bInitialOwner,
string lpName);
but when I call CreateMutex
lhndlRetHandle = CreateMutexA(nu ll, true, lstrAppName);
It give me error that first parameter cannot be NULL
Thanks.
"Nicholas Paldino [.NET/C# MVP]" wrote:
>Bronislav,

You can call the static OpenExisting method on the Mutex class to get
a
named mutex instance. If it doesn't exist, then you can catch the
WaitHandleCann otBeOpenedExcep tion instance that is thrown and then create
a
new one.

Now, you do run the risk of running into a race condition here, as
two
instances of the app can try to open a mutex that doesn't exist, which is
different from the call to CreateMutex. If you want to avoid this race
condition, declare the CreateMutex API function and call it through the
P/Invoke layer, getting the handle. Then, create a new Mutex instance,
and
assign the handle to that if you want to use it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:7F******* *************** ************@mi crosoft.com...
The Mutex class in my understanding is not complete analog of the
CreateMutex
API
because it not allowed to create difference instances of the programm
with
the different name and I need exactly this.

"Nicholas Paldino [.NET/C# MVP]" wrote:

Bronislav,

There are a number of examples of how to do this on the web. Have
you
taken a look at the Mutex class? It will give you everything you need
to
do
to replicate this behavior.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:2D******* *************** ************@mi crosoft.com...
I need to run more then 1 instance of the same program but also I
need
to
know which instance is running.
In C++ code was:
int nInstance = 0;
char szAppName[32];
m_dwMutexReturn = ERROR_ALREADY_E XISTS;
while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
MAXINSTANCES ))
{
nInstance++;
itoa( nInstance, szTemp, 10 );
strcpy( szAppName, "ProductEng ine" );
strcat( szAppName, szTemp );
m_hMutex = CreateMutex( NULL, TRUE, szAppName );
m_dwMutexReturn = GetLastError();
}

if ( nInstance == MAXINSTANCES )
m_sState->nInstance = 1;
else
m_sState->nInstance = nInstance;
but I can't port this code to C#.
Somebody knows how to do it?




Nov 8 '07 #6
Thanks

"Nicholas Paldino [.NET/C# MVP]" wrote:
Well, you can declare lpMutexAttribut es as IntPtr if you are going to
pass null. Also, you should declare the bInitialOwner parameter as bool,
and you should indicate the version of the function you want to use in the
DllImport attribute and you should set the return type to IntPtr (since it
is a handle). Putting it all together, you should have:

[DllImport("kern el32.dll", SetLastError=Tr ue, CharSet=CharSet .Auto)]
private static extern IntPtr CreateMutex(Int Ptr lpMutexAttribut es, bool
bInitialOwner, string lpName);

Then, you can pass IntPtr.Zero to the first parameter.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:0C******** *************** ***********@mic rosoft.com...
I tried to do exactly that but I cannot find how to call CreateMutex
through
p-invoke
I tried both declaretions

/*
[DllImport("kern el32.dll", SetLastError=tr ue)]
private static extern int CreateMutexA(
ref SECURITY_ATTRIB UTES lpMutexAttribut es,
int bInitialOwner,
string lpName);
*/
[DllImport("kern el32.dll", SetLastError=tr ue)]
private static extern System.IntPtr CreateMutexA(
out int lpMutexAttribut es,
bool bInitialOwner,
string lpName);
but when I call CreateMutex
lhndlRetHandle = CreateMutexA(nu ll, true, lstrAppName);
It give me error that first parameter cannot be NULL
Thanks.
"Nicholas Paldino [.NET/C# MVP]" wrote:
Bronislav,

You can call the static OpenExisting method on the Mutex class to get
a
named mutex instance. If it doesn't exist, then you can catch the
WaitHandleCanno tBeOpenedExcept ion instance that is thrown and then create
a
new one.

Now, you do run the risk of running into a race condition here, as
two
instances of the app can try to open a mutex that doesn't exist, which is
different from the call to CreateMutex. If you want to avoid this race
condition, declare the CreateMutex API function and call it through the
P/Invoke layer, getting the handle. Then, create a new Mutex instance,
and
assign the handle to that if you want to use it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:7F******** *************** ***********@mic rosoft.com...
The Mutex class in my understanding is not complete analog of the
CreateMutex
API
because it not allowed to create difference instances of the programm
with
the different name and I need exactly this.

"Nicholas Paldino [.NET/C# MVP]" wrote:

Bronislav,

There are a number of examples of how to do this on the web. Have
you
taken a look at the Mutex class? It will give you everything you need
to
do
to replicate this behavior.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bronislav" <Br*******@disc ussions.microso ft.comwrote in message
news:2D******** *************** ***********@mic rosoft.com...
I need to run more then 1 instance of the same program but also I
need
to
know which instance is running.
In C++ code was:
int nInstance = 0;
char szAppName[32];
m_dwMutexReturn = ERROR_ALREADY_E XISTS;
while (( m_dwMutexReturn == ERROR_ALREADY_E XISTS ) && ( nInstance <
MAXINSTANCES ))
{
nInstance++;
itoa( nInstance, szTemp, 10 );
strcpy( szAppName, "ProductEng ine" );
strcat( szAppName, szTemp );
m_hMutex = CreateMutex( NULL, TRUE, szAppName );
m_dwMutexReturn = GetLastError();
}

if ( nInstance == MAXINSTANCES )
m_sState->nInstance = 1;
else
m_sState->nInstance = nInstance;
but I can't port this code to C#.
Somebody knows how to do it?




Nov 8 '07 #7

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

Similar topics

5
3850
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the application in the title bar. For example, the user starts the application, and the title bar says "Control Panel". The user starts another instance of the application, and it knows that one instance is already running, so the title bar of the...
11
1795
by: thechaosengine | last post by:
Hi all, I have a very general but quite significant question about objects. My question is, when should I create them? I know thats a crap question so let me explain a bit further. Lets take an example of user management against a database. Things I might like to do include:
9
6642
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to use the common example, I have a situation where I am implementing many types of Shapes. My current way of thinking is, well, since they are all the same type, let's just put them all in the same file. The include file would be "shapes.h" and it...
4
3690
by: Chad Myers | last post by:
I'm instrumenting my app with a few performance counters and I'd like to ask you all for some advice on how to handle performance counter instances. I have a class library that is a base library for most of our ..NET applications. It provides configuration, logging, exception management/publishing, data access, etc. I have my counters, but I'm curious how I should handle instances. Right now, the instance name is exename-procid like...
1
2983
by: sunil | last post by:
Hello All. I have written a program as an exe that performs some kind of order processing. The program is first configured and then started manually. I have have multiple instances of this program with their own ini file and multiple instances can run simultaneouly. Now what i want to convert the whole thing into a windows ervice that is self starting and reads the configuration from the .ini file(its own) and can be run separately as a...
10
1556
by: Alan G Isaac | last post by:
My class MyClass reuses many default parameters with a small number of changes in each instance. For various reasons I decided to put all the parameters in a separate Params class, instances of which reset the default values based on keyword arguments, like this: class Params: def __init__(self,**kwargs): #set lots of default values
9
1990
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app there is one instance of the program running but several simultaneous clients connecting to and 'using' it. When I think about this I'm wondering what this may add to what needs to be considered for scope and lifetime... is a scenario created where...
1
3809
by: R.A.M. | last post by:
Hello, I have started programming ASP.NET application using SQL Server 2005 Express Edition. The problem is that when C# code tries to access aspnetdb database, for example: System.Security.Principal.IPrincipal user = HttpContext.Current.User; if (user.IsInRole("Kierownik")) // HERE PROBLEM ....then the following run-time error occurs:
7
1972
by: sreeni | last post by:
I have a ordinary C++ class with lot of datamembers used internally between methods of the same class. Is there any other elegant way to maintain the private datamembers. Please give your suggestions.
0
9530
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,...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
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
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
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.