single instance application with app.xamlHi,<br><br>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. <br><br>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?<br><br>Thanks in advance.<br><br>Best regards,<br>Artur Carvalho<br>© 2009 Microsoft Corporation. All rights reserved.Tue, 09 Dec 2008 01:24:47 Ze321cc4b-e2f3-474e-9575-bacbd2e83a60http://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#e321cc4b-e2f3-474e-9575-bacbd2e83a60http://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#e321cc4b-e2f3-474e-9575-bacbd2e83a60Artur Belicohttp://social.msdn.microsoft.com/Profile/en-US/?user=Artur%20Belicosingle instance application with app.xamlHi,<br><br>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. <br><br>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?<br><br>Thanks in advance.<br><br>Best regards,<br>Artur Carvalho<br>Tue, 13 Nov 2007 02:16:48 Z2007-11-16T02:50:34Zhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#bc981d05-0140-4c59-b196-883c7f355a4bhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#bc981d05-0140-4c59-b196-883c7f355a4bHua Wang - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Hua%20Wang%20-%20MSFTsingle instance application with app.xaml<p align=left><font face=Arial size=2>You can try the following two approaches:</font></p> <p align=left>1. <font face=Arial size=2>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.</font></p> <p align=left> </p> <p align=left>2. Use your exsiting approach, set Application.Resources, for example,</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>// resource.xaml is compiled as resource of the exe.</p> <p align=left>ResourceDictionary rd = new ResourceDictionary();</p> <p align=left>rd.Source = new Uri(&quot;/resource.xaml&quot;, UriKind.Relative);</p> <p align=left>app.Resource = rd;</p></blockquote>Tue, 13 Nov 2007 03:12:53 Z2007-11-16T02:50:26Zhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#53c19d23-4aad-45ed-9859-074f4bf9f6f6http://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#53c19d23-4aad-45ed-9859-074f4bf9f6f6Marco Zhouhttp://social.msdn.microsoft.com/Profile/en-US/?user=Marco%20Zhousingle instance application with app.xaml<p><font face=Verdana>I've created a sample of single  instance application, in my sample, you can use the app.xaml as normal as the following demonstrates:</font></p> <p align=left><font face=Verdana></font> </p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><font face=Verdana></font><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%"><font face=Verdana>Code Block</font></span></div> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><font face=Verdana></font></span> </p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">&lt;</span><span style="font-size:10pt;color:#a31515;font-family:'Verdana','sans-serif'">cc:WpfApplication</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"> </span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">x:Class</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">SingleInstanceAppDemo.App</span>&quot;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">    </span></span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">xmlns</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">http://schemas.microsoft.com/winfx/2006/xaml/presentation</span>&quot;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">    </span></span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">xmlns:x</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">http://schemas.microsoft.com/winfx/2006/xaml</span>&quot;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">    </span></span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">xmlns:cc</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">clr-namespace:Sheva.Windows</span>&quot;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">    </span></span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">IsSingleInstance</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">True</span>&quot;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">    </span></span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">StartupUri</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">Window1.xaml</span>&quot;<span style="color:blue">&gt;</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">  </span>&lt;</span><span style="font-size:10pt;color:#a31515;font-family:'Verdana','sans-serif'">cc:WpfApplication.Resources</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">&gt;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">    </span>&lt;</span><span style="font-size:10pt;color:#a31515;font-family:'Verdana','sans-serif'">Style</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"> </span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">TargetType</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">{x:Type TextBlock}</span>&quot;<span style="color:blue">&gt;</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">      </span>&lt;</span><span style="font-size:10pt;color:#a31515;font-family:'Verdana','sans-serif'">Setter</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"> </span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">Property</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">Foreground</span>&quot;<span style="color:blue"> </span><span style="color:red">Value</span><span style="color:blue">=</span>&quot;<span style="color:blue">White</span>&quot;<span style="color:blue">/&gt;</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">      </span>&lt;</span><span style="font-size:10pt;color:#a31515;font-family:'Verdana','sans-serif'">Setter</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"> </span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">Property</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">FontSize</span>&quot;<span style="color:blue"> </span><span style="color:red">Value</span><span style="color:blue">=</span>&quot;<span style="color:blue">40</span>&quot;<span style="color:blue">/&gt;</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">      </span>&lt;</span><span style="font-size:10pt;color:#a31515;font-family:'Verdana','sans-serif'">Setter</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"> </span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">Property</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">FontWeight</span>&quot;<span style="color:blue"> </span><span style="color:red">Value</span><span style="color:blue">=</span>&quot;<span style="color:blue">Bold</span>&quot;<span style="color:blue">/&gt;</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">      </span>&lt;</span><span style="font-size:10pt;color:#a31515;font-family:'Verdana','sans-serif'">Setter</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"> </span><span style="font-size:10pt;color:red;font-family:'Verdana','sans-serif'">Property</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">=</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'">&quot;<span style="color:blue">FontFamily</span>&quot;<span style="color:blue"> </span><span style="color:red">Value</span><span style="color:blue">=</span>&quot;<span style="color:blue">Verdana</span>&quot;<span style="color:blue">/&gt;</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">    </span>&lt;/</span><span style="font-size:10pt;color:#a31515;font-family:'Verdana','sans-serif'">Style</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">&gt;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal" align=left><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'"><span style="">  </span>&lt;/</span><span style="font-size:10pt;color:#a31515;font-family:'Verdana','sans-serif'">cc:WpfApplication.Resources</span><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">&gt;</span></p> <p class=MsoNormal style="margin:0cm 0cm 10pt" align=left><span style="font-size:10pt;color:blue;line-height:115%;font-family:'Verdana','sans-serif'">&lt;/</span><span style="font-size:10pt;color:#a31515;line-height:115%;font-family:'Verdana','sans-serif'">cc:WpfApplication</span><span style="font-size:10pt;color:blue;line-height:115%;font-family:'Verdana','sans-serif'">&gt;</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal"><span style="font-size:10pt;color:blue;font-family:'Verdana','sans-serif'">namespace</span><span style="font-size:10pt;font-family:'Verdana','sans-serif'"> SingleInstanceAppDemo</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal"><span style="font-size:10pt;font-family:'Verdana','sans-serif'">{</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal"><span style="font-size:10pt;font-family:'Verdana','sans-serif'"><span style="">    </span><span style="color:gray">///</span><span style="color:green"> </span><span style="color:gray">&lt;summary&gt;</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal"><span style="font-size:10pt;font-family:'Verdana','sans-serif'"><span style="">    </span><span style="color:gray">///</span><span style="color:green"> Interaction logic for App.xaml</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal"><span style="font-size:10pt;font-family:'Verdana','sans-serif'"><span style="">    </span><span style="color:gray">///</span><span style="color:green"> </span><span style="color:gray">&lt;/summary&gt;</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal"><span style="font-size:10pt;font-family:'Verdana','sans-serif'"><span style="">    </span><span style="color:blue">public</span> <span style="color:blue">partial</span> <span style="color:blue">class</span> <span style="color:#2b91af">App</span> : <span style="color:#2b91af">WpfApplication</span></span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal"><span style="font-size:10pt;font-family:'Verdana','sans-serif'"><span style="">    </span>{</span></p> <p class=MsoNormal style="margin:0cm 0cm 0pt;line-height:normal"><span style="font-size:10pt;font-family:'Verdana','sans-serif'"><span style="">    </span>}</span></p> <p class=MsoNormal style="margin:0cm 0cm 10pt"><span style="font-size:10pt;line-height:115%;font-family:'Verdana','sans-serif'">}</span></p></div></div> <p align=left></span> </p> <p><font face=Verdana>You can refer to the following link for the implementation of WpfApplication class.</font></p> <p align=left><font face=Verdana><a title="http://www.cnblogs.com/Files/sheva/SingleInstanceAppDemo.zip" href="http://www.cnblogs.com/Files/sheva/SingleInstanceAppDemo.zip">http://www.cnblogs.com/Files/sheva/SingleInstanceAppDemo.zip</a></font></p> <p align=left> </p> <p align=left><font face=Verdana>Hope this helps</font></p>Thu, 15 Nov 2007 04:57:00 Z2007-11-16T02:50:34Zhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#d4c53a86-23fe-4684-b11f-9ebe1cb18cb7http://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#d4c53a86-23fe-4684-b11f-9ebe1cb18cb7dan blanchardhttp://social.msdn.microsoft.com/Profile/en-US/?user=dan%20blanchardsingle instance application with app.xamlHi, This works great until you try to route App events, any ideas?<br><br>&lt;cc:WpfApplication x:Class=&quot;SingleInstanceAppDemo.App&quot;<br>    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;<br>    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;<br>    xmlns:cc=&quot;clr-namespace<img alt="Tongue Tied" src="http://forums.microsoft.com/MSDN/emoticons/emotion-7.gif">heva.Windows&quot;<br>    IsSingleInstance=&quot;True&quot;<br>                   <span style="font-weight:bold">DispatcherUnhandledException=&quot;App_DispatcherUnhandledException&quot;<br>    </span>Exit=&quot;Application_Exit&quot;<span style="font-weight:bold"><br></span>    StartupUri=&quot;Window1.xaml&quot;&gt;<br><br>        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)<br>        {<br>        }<br><br>System.Windows.Markup.XamlParseException was unhandled<br>  Message=&quot;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.&quot;<br>  Source=&quot;PresentationFramework&quot;<br>  LineNumber=6<br>  LinePosition=20<br>  StackTrace:<br>       at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType)<br>       at System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException)<br>       at System.Windows.Markup.BamlRecordReader.ThrowException(SRID id, String parameter)<br>       at System.Windows.Markup.BamlRecordReader.ReadPropertyRecordBase(String attribValue, Int16 attributeId, Int16 converterTypeId)<br>       at System.Windows.Markup.BamlRecordReader.ReadPropertyConverterRecord(BamlPropertyWithConverterRecord bamlPropertyRecord)<br>       at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)<br>       at System.Windows.Markup.BamlRecordReader.Read(Boolean singleRecord)<br>       at System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment()<br>       at System.Windows.Markup.TreeBuilder.Parse()<br>       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)<br>       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)<br>       at SingleInstanceAppDemo.App.InitializeComponent() in e:\Work\Cuattro\Prototypes\SingleInstanceApp\singleinstanceappdemo\SingleInstanceAppDemo2005\App.xaml:line 1<br>       at SingleInstanceAppDemo.App.Main() in E:\Work\Cuattro\Prototypes\SingleInstanceApp\singleinstanceappdemo\SingleInstanceAppDemo2005\obj\Debug\App.g.cs:line 0<br>       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)<br>       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)<br>       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()<br>       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)<br>       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)<br>       at System.Threading.ThreadHelper.ThreadStart()<br><br>Thu, 20 Dec 2007 17:13:59 Z2007-12-20T17:13:59Zhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#a0e291c2-31a6-47e8-ac80-e6e3039dd4f7http://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#a0e291c2-31a6-47e8-ac80-e6e3039dd4f7Marco Zhouhttp://social.msdn.microsoft.com/Profile/en-US/?user=Marco%20Zhousingle instance application with app.xaml<font size=2><span style="font-family:Verdana">I am not sure why the XAML parser cannot properly figure out the custom Application instance's property info and event info.</span><br style="font-family:Verdana"><span style="font-family:Verdana">To workaround this, you can register the event handlers in the WpfApplication's default constructor instead:</span><br style="font-family:Verdana"></font><pre class=code style="font-family:Verdana"><font size=2><span style="color:blue">public partial class </span><span style="color:rgb(43,145,175)">App </span>: <span style="color:rgb(43,145,175)">WpfApplication<br></span>{<br> <span style="color:blue">public </span>App()<br> {<br> <span style="color:blue">this</span>.Exit += <span style="color:blue">new </span><span style="color:rgb(43,145,175)">ExitEventHandler</span>(App_Exit);<br> }<br><br> <span style="color:blue">void </span>App_Exit(<span style="color:blue">object </span>sender, <span style="color:rgb(43,145,175)">ExitEventArgs </span>e)<br> {<br> <span style="color:rgb(43,145,175)">MessageBox</span>.Show(<span style="color:rgb(163,21,21)">&quot;Exit&quot;</span>);<br> }<br>}<br><br>Hope this helps<br></font></pre><font size=2><br style="font-family:Verdana"></font>Fri, 21 Dec 2007 04:05:31 Z2007-12-21T04:05:31Zhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#d8c68dd4-3f1c-4cd1-8579-c82d378c483fhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#d8c68dd4-3f1c-4cd1-8579-c82d378c483fdan blanchardhttp://social.msdn.microsoft.com/Profile/en-US/?user=dan%20blanchardsingle instance application with app.xamlHi 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?<br><br>app.xaml.cs:<br>        public App()<br>        {<br>            InitializeComponent();<br>            this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);<br>            throw new Exception(&quot;&quot;);<br>        }<br>        <br>private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)<br>        {<br>        }<br><br><br>WpfApplication:<br>                    try<br>                    {<br>                        base.Run(window);<br> <div style="margin-left:120px">// THIS IS NOT CATCHING UNHANDLED EXCEPTIONS EITHER...<br></div>                    }<br>                    catch (Exception ex)<br>                    {<br>                        // try to raise the exception ourselves..<br>                    }<br><br><br>Fri, 21 Dec 2007 04:31:51 Z2007-12-21T04:31:51Zhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#663aaa7e-5000-4ed2-af90-48e56b3fb032http://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#663aaa7e-5000-4ed2-af90-48e56b3fb032Marco Zhouhttp://social.msdn.microsoft.com/Profile/en-US/?user=Marco%20Zhousingle instance application with app.xaml<p>the DispatcherUnhandledException event works pretty well for me:</p> <p><span style="color:blue">public partial class</span><span style="color:rgb(43,145,175)">App</span>: <span style="color:rgb(43,145,175)">WpfApplication<br></span>{<br>    <span style="color:blue">public</span>App()<br>    {<br>        <span style="color:rgb(43,145,175)">Dispatcher</span>.CurrentDispatcher.UnhandledException += App_DispatcherUnhandledException;<br>    }<br><br>    <span style="color:blue">protected override void</span>OnStartup(<span style="color:rgb(43,145,175)">StartupEventArgs </span>e)<br>    {<br>       <span style="background-color:rgb(255,255,0)"> </span><span style="color:blue;background-color:rgb(255,255,0)">throw new</span><span style="color:rgb(43,145,175);background-color:rgb(255,255,0)">Exception</span><span style="background-color:rgb(255,255,0)">(</span><span style="color:rgb(163,21,21);background-color:rgb(255,255,0)">&quot;I am here&quot;</span><span style="background-color:rgb(255,255,0)">);</span><br>    }<br><br>    <span style="color:blue">private void </span>App_DispatcherUnhandledException(<span style="color:rgb(43,145,175)">Object </span>sender, <span style="color:rgb(43,145,175)">DispatcherUnhandledExceptionEventArgs </span>e)<br>    {<br>        <span style="color:blue">try<br>        </span>{<br>        }<br>        <span style="color:blue">catch<br>        </span>{<br>        }<br>        <span style="color:blue">finally<br>        </span>{<br>           <span style="background-color:rgb(255,255,0)"> e.Handled = </span><span style="color:blue;background-color:rgb(255,255,0)">true</span><span style="background-color:rgb(255,255,0)">;</span><br>            <span style="color:rgb(43,145,175)">MessageBox</span>.Show(e.Exception.Message);<br>        }<br>    }<br>}</p> <p><br></p>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.<br><br>Hope this helpsFri, 21 Dec 2007 10:02:54 Z2007-12-21T10:02:54Zhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#d116d906-7796-483f-a1f6-3af252c48e38http://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#d116d906-7796-483f-a1f6-3af252c48e38th3Nu11http://social.msdn.microsoft.com/Profile/en-US/?user=th3Nu11single instance application with app.xaml<br>is this right?<br><br> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div>public partial class App : Application<br>{<br> <div style="margin-left:40px">public App()<br></div>     {<br> <div style="margin-left:40px">     System.Diagnostics.Process[] process = <br></div> <div style="margin-left:120px">System.Diagnostics.Process.GetProcessesByName(<br></div> <div style="margin-left:160px">System.Diagnostics.Process.GetCurrentProcess().ProcessName );<br></div> <div style="margin-left:80px">if (process.Length&gt;1)<br></div> <div style="margin-left:80px">{<br></div> <div style="margin-left:120px">this.Shutdown();<br></div> <div style="margin-left:80px">}<br></div><br> <div style="margin-left:40px">}<br></div>} <p><br></p></div></div>Wed, 30 Apr 2008 10:41:49 Z2008-04-30T10:41:49Zhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#3fac0a07-9fa8-44e5-b918-649a644d9fc3http://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#3fac0a07-9fa8-44e5-b918-649a644d9fc3oznativehttp://social.msdn.microsoft.com/Profile/en-US/?user=oznativesingle instance application with app.xamlYes, it's fine. This code is useful when you want only a single application in your commputer <p align=left><font face=Arial size=2></font> </p>Sat, 17 May 2008 03:58:55 Z2008-05-17T03:58:55Zhttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#95963d01-8c39-4b3f-9e69-dc06a037630chttp://social.msdn.microsoft.com/Forums/en/wpf/thread/e321cc4b-e2f3-474e-9575-bacbd2e83a60#95963d01-8c39-4b3f-9e69-dc06a037630cIl-ya Tret-yakovhttp://social.msdn.microsoft.com/Profile/en-US/?user=Il-ya%20Tret-yakovsingle instance application with app.xaml <a href="http://dev.liferus.ru/2008/12/wpf-application-single-instance-in-one.html">http://dev.liferus.ru/2008/12/wpf-application-single-instance-in-one.html</a><br>I made it by Event WaitHandle.<br>I not worry about security of string Name for EventWaitHandle :( . It opt to you.<br>But no more Remoting and heap of code ;)Tue, 09 Dec 2008 01:02:53 Z2008-12-09T01:24:44Z