Poser une questionPoser une question
 

Traitéesingle instance application with app.xaml

  • mardi 13 novembre 2007 02:16Artur Belico Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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

Réponses

  • mardi 13 novembre 2007 03:12Hua Wang - MSFT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    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;

  • jeudi 15 novembre 2007 04:57Marco Zhou Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    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

Toutes les réponses

  • mardi 13 novembre 2007 03:12Hua Wang - MSFT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    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;

  • jeudi 15 novembre 2007 04:57Marco Zhou Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    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

  • jeudi 20 décembre 2007 17:13dan blanchard Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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()

  • vendredi 21 décembre 2007 04:05Marco Zhou Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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

  • vendredi 21 décembre 2007 04:31dan blanchard Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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..
                        }


  • vendredi 21 décembre 2007 10:02Marco Zhou Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    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
  • mercredi 30 avril 2008 10:41th3Nu11 Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

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

    }
    }


  • samedi 17 mai 2008 03:58oznative Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Yes, it's fine. This code is useful when you want only a single application in your commputer

     

  • mardi 9 décembre 2008 01:02Il-ya Tret-yakov Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
     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 ;)