tags:

views:

658

answers:

1

Hello,

I have a pretty simple problem.

When program has been started and user tries to start another instance. That new instance needs to bring old instance to front and quit.

The solution seams pretty simple, I could take the code from http://www.codeproject.com/KB/cs/oneprocessonly.aspx and be done with it.

Fortunately/Unfortunately I'm using WPF. This means, there is no way for me to control content of Main() method.

I have found solution to this problem "How can I provide my own Main() method in my WPF application?" @ http://learnwpf.com/Posts/Post.aspx?postId=a5643949-ab80-47f9-93c8-f5e8e5782d34.

But this solution brings another problems like Expression Blend stats to panic when there is no App.xaml file.

I could use App classes OnStarted event, but I'm using WPF's splash screen resource, this would mean that extra splash screen will be displayed.

My last concern with this method is it seams not "future-proof" to me :(.

Is there any WPF-style solution to this problem?

Thank You in advance.

+5  A: 

I believe this link may help you: C# Single Instance Application

You typically detect running instances by setting a mutex in your application, and then checking for that mutex when starting the application. Sending the other application to the front is a matter of sending the correct PostMessage.

As far as having your own Main(), I agree that Expression Blend does tend to act a little weird when your App.xaml doesn't exist. In my own application, I use Expression Blend to manually edit some complex Windows, but don't do any compilation or testing in the application (have main defined in an App.cs).

If you're doing extensive UI testing using Expression Blend, I can't say you'll necessarily get far using this method. I tend to do most of my editing and testing in Visual Studio and edit the XAML directly, so this isn't as much of an issue for me. I'd consider this a limitation (or bug?) of Expression Blend personally.

Will Eddins