Restricting instance to single instance?

T

Tom Kreyche

I have an 3rd party app with VBA embedded, and I want to use it simply as a
shell to call my C# application. This works fine using VBA's
CreateObject("Shell.Application") and ShellExecute.

However it opens a new instance of the C# program every time it is called,
which is undesirable. VBA is very limited in handling external calls, so is
there some way from within C# to only allow a single instance to be created.

I believe there is a way to do this with COM but ? .NET.

thanks, Tom
 
J

Jon Skeet [C# MVP]

Tom Kreyche said:
I have an 3rd party app with VBA embedded, and I want to use it simply as a
shell to call my C# application. This works fine using VBA's
CreateObject("Shell.Application") and ShellExecute.

However it opens a new instance of the C# program every time it is called,
which is undesirable. VBA is very limited in handling external calls, so is
there some way from within C# to only allow a single instance to be created.

I believe there is a way to do this with COM but ? .NET.

See http://www.pobox.com/~skeet/csharp/faq/#one.application.instance
 
G

Guest

You will have to use Mutex to keep track of the application instance
...
using System.Threading
//include a private static memeber for your main clas

private static Mutex mtx = null
....
static void Main()

mtx = new Mutex(false, "MyMutex")

//Check whether any instance is already runnin
if(!mtx.WaitOne(0, true)

MessageBox.Show("goodbye")
return

Application.Run(new Form1())


----- Tom Kreyche wrote: ----

I have an 3rd party app with VBA embedded, and I want to use it simply as
shell to call my C# application. This works fine using VBA'
CreateObject("Shell.Application") and ShellExecute

However it opens a new instance of the C# program every time it is called
which is undesirable. VBA is very limited in handling external calls, so i
there some way from within C# to only allow a single instance to be created

I believe there is a way to do this with COM but ? .NET

thanks, To
 

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