Windows Develop Bookmark and Share   
 index > Windows Forms General > Controlling process of multiple application instances of same app
 

Controlling process of multiple application instances of same app


I am looking for ideas how to implemement this behaviour

I have a C#.net 2005 windows application.
The application has a calculator to calculate outstanding amount for a particular customer order.
But the problem is...calculator will crash if ran for same customer in multiple instances of the application

for ex
This will run fine
If instance of window application is opened and run to find outstanding amount for Cust1.

This will fail
Open 2 instances of same app and run to find outstanding amount for same customer Cust1.
(I know calculator can be fixed..but that is not the option)

One way I thought is create a database table and insert a flag in table so that
only one instance for a particular customer will run. But the problem is if the
first instance fails(for cust1) then calculator will never run for that customer(Cust1).
Flag value in the database table will never change.

Any other ideas?

Pintoo Khaira  Monday, February 11, 2008 8:51 PM

Hi Pintoo,

Based on my understanding, you'd like your application to be a singleton application, i.e. only one instance of your application is run at the same time. If I'm off base, please feel free to let me know.

In VB.NET, we could make a application single by selecting the 'Make single instance application' option in the Project Designer.


However, there's no such an option in C#. Fortunately, .NET2.0 has provided Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase class, which provides properties, methods, and events related to the current application and has a property called IsSingleInstance.

We could derive a class from WindowsFormsApplicationBase class and set the IsSingleInstance property to true.

The following is a sample. It requires that you add a reference to Microsoft.VisualBasic in the project.

using System.Deployment.Application;

public class Program:Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Program prog = new Program();
prog.MainForm = new Form1();
prog.Run(new string[]{});
}

public Program()
{
this.IsSingleInstance = true;
}
}

Hope this helps.

If youhave any question, please feel free to let me know.

Linda

Linda Liu  Wednesday, February 13, 2008 10:20 AM

Linda, Thanks for you insight.

The application wont be a singleton application. Users can open many instances of app and run for different customers.

For ex.

User can open 3 instances of app and run for Cust1, Cust2 and Cust3 without any problem

But the problem is users cannot open 2 instances and run cust1 in both the instances.

Thanks

pintoo

Pintoo Khaira  Wednesday, February 13, 2008 11:24 AM

Hi Pintoo,

Thank you for your reply!

I suggest that you record all the logging users in the database to solve the problem. For example, if a user runs an instance ofthe application and log in as Cust1, you may record Cust1 in the database. If the user runs another instance of the application and tries to log in as Cust1 again, you may check the database to see if there has already beenCust1 in it. If yes, reject the logging in.

Hope this helps.

Linda

Linda Liu  Wednesday, February 20, 2008 10:29 AM

There are more ways to solve this problem without resorting to using single instance for the application.

But the other ways come at a cost.

There are several ways to do protection on variables when doing multithreading, and some of these can cross application boundaries. There are 2 ways I know to do this. A semaphore and a mutex. The point is that these are named resources.

If customer1 hasa name and you wish to access it, First lock the mutex based on the customers name. Suppose anyone has already done that for the same customer, you will have to wait until the mutex is released. Once you are done with the customer you have to release the mutex. Anyone who is waiting for the mutex will get it, if there is more than one, only one will be the lucky one.

You can also specify timeouts or do a peek and then tell the user please try again later if you don't want to lockup the user.

The api is like follows :

Mutex (Boolean, String)

WaitHandle.WaitOne ()

This should get you up and running.

The difference between a mutex and a semaphore is that a mutex is a binary semaphore, it is locked or it is free.

A semaphore is actually a counting semaphore, for your problem you need to use the mutex.

regards,

Philip

Philip_Stuyck__  Friday, February 22, 2008 6:34 PM

You can use google to search for other answers

Custom Search

More Threads

• Format Value on LostFocus
• Pass variable between forms
• List View and c#
• game of life kinda grid...
• Possible Bug of the TableLayoutPanel?
• Howto: expose the Items property of a MenuStrip as a property of a class containing the MenuStrip(with designer support)
• Strikeout text
• ToolstripControlHost custom control not showing up in designer
• VB2008 MDI Application connected SQL server 2005 via ODBC
• Move Listbox1 Items to Listbox2