MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > single instance application with app.xaml
發問發問
 

已答覆single instance application with app.xaml

  • Tuesday, 13 November, 2007 2:16Artur Belico 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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, 13 November, 2007 3:12Hua Wang - MSFT 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    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;

  • Thursday, 15 November, 2007 4:57Marco Zhou 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    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

所有回覆

  • Tuesday, 13 November, 2007 3:12Hua Wang - MSFT 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    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;

  • Thursday, 15 November, 2007 4:57Marco Zhou 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    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, 20 December, 2007 17:13dan blanchard 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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()

  • Friday, 21 December, 2007 4:05Marco Zhou 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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, 21 December, 2007 4:31dan blanchard 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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, 21 December, 2007 10:02Marco Zhou 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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
  • Wednesday, 30 April, 2008 10:41th3Nu11 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

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

    }
    }


  • Saturday, 17 May, 2008 3:58oznative 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Yes, it's fine. This code is useful when you want only a single application in your commputer

     

  • Tuesday, 9 December, 2008 1:02Il-ya Tret-yakov 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
     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 ;)