Single Instance Application

C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.VisualBasic.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace PGPowerPoint_ListTool
{
    class SingleInstanceApplicationWrapper:Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        private PGPAPP app;
        public SingleInstanceApplicationWrapper()
        {
            this.IsSingleInstance = true;
        }
        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
        {
            app = new PGPAPP();
            app.Run();
            return false;
        }
        protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs)
        {
            if (eventArgs.CommandLine.Count > 0)
            {
                app.ShowDocument(eventArgs.CommandLine[0]);
            }
            else
            {
                app.MainWindow.Activate();
                MessageBox.Show(app.MainWindow, "More than 1 instance of this application is not allowed"); 
               
            }
        }
    }
}
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            MainWindow main = new MainWindow();
            MainWindow = main;
            main.Show();

            if (e.Args.Length > 0) ShowDocument(e.Args[0]);
        }

        public void ShowDocument(string str)
        {
            ((ListToolViewModel)MainWindow.DataContext).SerializeTheList(str);
        }
    }
    class StartUp
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceApplicationWrapper wrapper = new SingleInstanceApplicationWrapper();
            wrapper.Run(args);
        }
    }

posted on 2011-06-08 14:17  Y#  阅读(207)  评论(0编辑  收藏  举报

导航