none
single instance application with app.xaml

    Question

  • Hi,

    I need to make sure I only have one instance of a WPF application. The solutions ive found dont use app.xaml. But the problem is that Im using App.xaml to store resources that are shared by some windows.

    How can I do this? Is there a way to put a centralized resource that is visible by all the windows or is there a way to only allow one instance and maintain the App.xaml file?

    Thanks in advance.

    Best regards,
    Artur Carvalho
    Tuesday, November 13, 2007 2:16 AM

Answers

  • You can try the following two approaches:

    1. You should be able to define your own Main method, where you can create a new application and call run on it if it is the first instance. You cannot define the app.xaml as ApplicationDefinition in build action in this case and you need to call Application.InitializeComponent() and Run() in your main.

     

    2. Use your exsiting approach, set Application.Resources, for example,

    // resource.xaml is compiled as resource of the exe.

    ResourceDictionary rd = new ResourceDictionary();

    rd.Source = new Uri("/resource.xaml", UriKind.Relative);

    app.Resource = rd;

    Tuesday, November 13, 2007 3:12 AM
  • I've created a sample of single  instance application, in my sample, you can use the app.xaml as normal as the following demonstrates:

     

    Code Block

     

    <cc:WpfApplication x:Class="SingleInstanceAppDemo.App"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:cc="clr-namespace:Sheva.Windows"

        IsSingleInstance="True"

        StartupUri="Window1.xaml">

      <cc:WpfApplication.Resources>

        <Style TargetType="{x:Type TextBlock}">

          <Setter Property="Foreground" Value="White"/>

          <Setter Property="FontSize" Value="40"/>

          <Setter Property="FontWeight" Value="Bold"/>

          <Setter Property="FontFamily" Value="Verdana"/>

        </Style>

      </cc:WpfApplication.Resources>

    </cc:WpfApplication>

    namespace SingleInstanceAppDemo

    {

        /// <summary>

        /// Interaction logic for App.xaml

        /// </summary>

        public partial class App : WpfApplication

        {

        }

    }

     

    You can refer to the following link for the implementation of WpfApplication class.

    http://www.cnblogs.com/Files/sheva/SingleInstanceAppDemo.zip

     

    Hope this helps

    Thursday, November 15, 2007 4:57 AM

All replies

  • You can try the following two approaches:

    1. You should be able to define your own Main method, where you can create a new application and call run on it if it is the first instance. You cannot define the app.xaml as ApplicationDefinition in build action in this case and you need to call Application.InitializeComponent() and Run() in your main.

     

    2. Use your exsiting approach, set Application.Resources, for example,

    // resource.xaml is compiled as resource of the exe.

    ResourceDictionary rd = new ResourceDictionary();

    rd.Source = new Uri("/resource.xaml", UriKind.Relative);

    app.Resource = rd;

    Tuesday, November 13, 2007 3:12 AM
  • I've created a sample of single  instance application, in my sample, you can use the app.xaml as normal as the following demonstrates:

     

    Code Block

     

    <cc:WpfApplication x:Class="SingleInstanceAppDemo.App"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:cc="clr-namespace:Sheva.Windows"

        IsSingleInstance="True"

        StartupUri="Window1.xaml">

      <cc:WpfApplication.Resources>

        <Style TargetType="{x:Type TextBlock}">

          <Setter Property="Foreground" Value="White"/>

          <Setter Property="FontSize" Value="40"/>

          <Setter Property="FontWeight" Value="Bold"/>

          <Setter Property="FontFamily" Value="Verdana"/>

        </Style>

      </cc:WpfApplication.Resources>

    </cc:WpfApplication>

    namespace SingleInstanceAppDemo

    {

        /// <summary>

        /// Interaction logic for App.xaml

        /// </summary>

        public partial class App : WpfApplication

        {

        }

    }

     

    You can refer to the following link for the implementation of WpfApplication class.

    http://www.cnblogs.com/Files/sheva/SingleInstanceAppDemo.zip

     

    Hope this helps

    Thursday, November 15, 2007 4:57 AM
  • Hi, This works great until you try to route App events, any ideas?

    <cc:WpfApplication x:Class="SingleInstanceAppDemo.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cc="clr-namespaceTongue Tiedheva.Windows"
        IsSingleInstance="True"
                       DispatcherUnhandledException="App_DispatcherUnhandledException"
       
    Exit="Application_Exit"
        StartupUri="Window1.xaml">

            private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
            {
            }

    System.Windows.Markup.XamlParseException was unhandled
      Message="Cannot find DependencyProperty or PropertyInfo for property named 'DispatcherUnhandledException'. Property names are case sensitive.  Error at object 'SingleInstanceAppDemo.App' in markup file 'SingleInstanceAppDemo2005;component/app.xaml' Line 6 Position 20."
      Source="PresentationFramework"
      LineNumber=6
      LinePosition=20
      StackTrace:
           at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType)
           at System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException)
           at System.Windows.Markup.BamlRecordReader.ThrowException(SRID id, String parameter)
           at System.Windows.Markup.BamlRecordReader.ReadPropertyRecordBase(String attribValue, Int16 attributeId, Int16 converterTypeId)
           at System.Windows.Markup.BamlRecordReader.ReadPropertyConverterRecord(BamlPropertyWithConverterRecord bamlPropertyRecord)
           at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)
           at System.Windows.Markup.BamlRecordReader.Read(Boolean singleRecord)
           at System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment()
           at System.Windows.Markup.TreeBuilder.Parse()
           at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
           at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
           at SingleInstanceAppDemo.App.InitializeComponent() in e:\Work\Cuattro\Prototypes\SingleInstanceApp\singleinstanceappdemo\SingleInstanceAppDemo2005\App.xaml:line 1
           at SingleInstanceAppDemo.App.Main() in E:\Work\Cuattro\Prototypes\SingleInstanceApp\singleinstanceappdemo\SingleInstanceAppDemo2005\obj\Debug\App.g.cs:line 0
           at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()

    Thursday, December 20, 2007 5:13 PM
  • I am not sure why the XAML parser cannot properly figure out the custom Application instance's property info and event info.
    To workaround this, you can register the event handlers in the WpfApplication's default constructor instead:
    public partial class App : WpfApplication
    {
    public App()
    {
    this.Exit += new ExitEventHandler(App_Exit);
    }

    void App_Exit(object sender, ExitEventArgs e)
    {
    MessageBox.Show("Exit");
    }
    }

    Hope this helps

    Friday, December 21, 2007 4:05 AM
  • Hi thanks for the info.. it works for the exit event but not for the DispatcherUnhandledException event - so exceptions crash the application. I tried to put another try catch in WpfApplication.RunSingleInstanceApplication() around the base.Run(window); line, but it still does not trap it properly.  any ideas?

    app.xaml.cs:
            public App()
            {
                InitializeComponent();
                this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
                throw new Exception("");
            }
           
    private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
            {
            }


    WpfApplication:
                        try
                        {
                            base.Run(window);
    // THIS IS NOT CATCHING UNHANDLED EXCEPTIONS EITHER...
                        }
                        catch (Exception ex)
                        {
                            // try to raise the exception ourselves..
                        }


    Friday, December 21, 2007 4:31 AM
  • the DispatcherUnhandledException event works pretty well for me:

    public partial classApp: WpfApplication
    {
        publicApp()
        {
            Dispatcher.CurrentDispatcher.UnhandledException += App_DispatcherUnhandledException;
        }

        protected override voidOnStartup(StartupEventArgs e)
        {
            throw newException("I am here");
        }

        private void App_DispatcherUnhandledException(Object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            try
           
    {
            }
            catch
           
    {
            }
            finally
           
    {
                e.Handled = true;
                MessageBox.Show(e.Exception.Message);
            }
        }
    }


    Ont thing to note here is that the test exceptions thrown before the dispatcher runs will not be propagated to the DispatcherUnhandledException event, that's why in my test code, I throw the test exception in the OnStartup override.

    Hope this helps
    Friday, December 21, 2007 10:02 AM

  • is this right?

    Code Snippet
    public partial class App : Application
    {
    public App()
         {
         System.Diagnostics.Process[] process =
    System.Diagnostics.Process.GetProcessesByName(
    System.Diagnostics.Process.GetCurrentProcess().ProcessName );
    if (process.Length>1)
    {
    this.Shutdown();
    }

    }
    }


    Wednesday, April 30, 2008 10:41 AM
  • Yes, it's fine. This code is useful when you want only a single application in your commputer

     

    Saturday, May 17, 2008 3:58 AM
  •  http://dev.liferus.ru/2008/12/wpf-application-single-instance-in-one.html
    I made it by Event WaitHandle.
    I not worry about security of string Name for EventWaitHandle :( . It opt to you.
    But no more Remoting and heap of code ;)
    Tuesday, December 09, 2008 1:02 AM
  • Here my solution: http://blogs.microsoft.co.il/blogs/maxim/archive/2010/02/13/single-instance-application-manager.aspx

    Friday, February 19, 2010 10:29 AM
  • here the have analysed all approaches. you can choose the solution which fits you.

    http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application

    Friday, March 15, 2013 9:19 AM