Microsoft Developer Network > 포럼 홈 > Windows Presentation Foundation (WPF) > single instance application with app.xaml
질문하기질문하기
 

답변됨single instance application with app.xaml

  • 2007년 11월 13일 화요일 오전 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

답변

  • 2007년 11월 13일 화요일 오전 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;

  • 2007년 11월 15일 목요일 오전 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

모든 응답

  • 2007년 11월 13일 화요일 오전 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;

  • 2007년 11월 15일 목요일 오전 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

  • 2007년 12월 20일 목요일 오후 5: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()

  • 2007년 12월 21일 금요일 오전 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

  • 2007년 12월 21일 금요일 오전 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..
                        }


  • 2007년 12월 21일 금요일 오전 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
  • 2008년 4월 30일 수요일 오전 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();
    }

    }
    }


  • 2008년 5월 17일 토요일 오전 3:58oznative 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Yes, it's fine. This code is useful when you want only a single application in your commputer

     

  • 2008년 12월 9일 화요일 오전 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 ;)