• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
.NET Framework Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > single instance application with app.xaml
Ask a questionAsk a question
Search Forums:
  • Search Windows Presentation Foundation (WPF) Forum Search Windows Presentation Foundation (WPF) Forum
  • Search All .NET Development Forums Search All .NET Development Forums
  • Search All MSDN Forums Search All MSDN Forums
 

Answersingle instance application with app.xaml

  • Tuesday, November 13, 2007 2:16 AMArtur Belico Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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
    • ReplyReply
    • QuoteQuote
     

Answers

  • Tuesday, November 13, 2007 3:12 AMHua Wang - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vote As Helpful
    0

    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;

    • ReplyReply
    • QuoteQuote
     
  • Thursday, November 15, 2007 4:57 AMMarco ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vote As Helpful
    0

    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

    • ReplyReply
    • QuoteQuote
     

All Replies

  • Thursday, December 20, 2007 5:13 PMdan blanchard Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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()

    • ReplyReply
    • QuoteQuote
     
  • Friday, December 21, 2007 4:05 AMMarco ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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

    • ReplyReply
    • QuoteQuote
     
  • Friday, December 21, 2007 4:31 AMdan blanchard Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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..
                        }


    • ReplyReply
    • QuoteQuote
     
  • Friday, December 21, 2007 10:02 AMMarco ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    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
    • ReplyReply
    • QuoteQuote
     
  • Wednesday, April 30, 2008 10:41 AMth3Nu11 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    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();
    }

    }
    }


    • ReplyReply
    • QuoteQuote
     
  • Saturday, May 17, 2008 3:58 AMoznative Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Yes, it's fine. This code is useful when you want only a single application in your commputer

     

    • ReplyReply
    • QuoteQuote
     
  • Tuesday, December 09, 2008 1:02 AMIliya_Tretyakov Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
     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 ;)
    • Edited byIliya_Tretyakov Tuesday, December 09, 2008 1:24 AMDetailed
    •  
    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement