Laden...

Single Instanz bei weiterem Aufruf List aktualisieren und an Control binden

Erstellt von csharpLeben vor 14 Jahren Letzter Beitrag vor 14 Jahren 934 Views
C
csharpLeben Themenstarter:in
7 Beiträge seit 2009
vor 14 Jahren
Single Instanz bei weiterem Aufruf List aktualisieren und an Control binden

Hallo zusammen,

ich habe hier eine Klasse Single Instanz:

#region  References

using System;
using System.Diagnostics;
using System.Threading;

#endregion

namespace SingleInstance
{
    /// <summary>
    /// Provides a lock object to prevent a program from being launched multiple times.
    /// </summary>
    public class ProcessLock : IDisposable
    {
        #region Fields

        private string applicationName;
        private Mutex mutex;
        private bool created;
        private bool disposed;

        #endregion

        #region Instance Management

        /// <summary>
        /// Initializes a new instance of the <see cref="ProcessLock"/> class.
        /// </summary>
        /// <param name="applicationName">Name of the process lock</param>
        public ProcessLock(string applicationName)
        {
            this.applicationName = applicationName;
            TryLock();
        }

        /// <summary>
        /// Disposes the process lock resources.
        /// </summary>
        public void Dispose()
        {
            if (mutex != null)
            {
                if (created)
                    Unlock();
                else
                {
                    mutex.Close();
                    mutex = null;
                }
            }

            disposed = true;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Indicates whether a process for the given name does already exist.
        /// </summary>
        public bool AlreadyExists
        {
            get
            {
                return !created;
            }
        }

        /// <summary>
        /// Indicates whether the process lock is disposed.
        /// </summary>
        public bool Disposed
        {
            get
            {
                return disposed;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Try to get the process lock again.
        /// </summary>
        public bool TryLock()
        {
            if (disposed)
                throw new ObjectDisposedException("ProcessLock");
            if (created)
                throw new InvalidOperationException();
            if (mutex != null)
                mutex.Close();
            mutex = new Mutex(true, applicationName, out created);

            return created;
        }

        /// <summary>
        /// Releases the process lock.
        /// </summary>
        public void Unlock()
        {
            if (disposed)
                throw new ObjectDisposedException("ProcessLock");
            if (!created)
                throw new InvalidOperationException();
            Debug.Assert(mutex != null);

            mutex.ReleaseMutex();
            mutex = null;
            created = false;
        }

        #endregion
    }
}

Folgender Aufruf:

     private SingleInstance() { }
        private static readonly string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            using (ProcessLock processLock = new ProcessLock(assemblyName))
            {
                if (processLock.AlreadyExists)
                {
                    //Hier ein List aktualisieren und an MainForm übergeben.
                   //LoadXMLIntoList();
                    return;
                }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
        }

Ich habe in meinem MainForm Formular ein Grid und als DataSource eine List angebunden. Nun möchte ich wenn das Programm ein weiteres mal aufgerufen wird, diese List aktualisiert wird und damit auch automtisch im Grid aktualisiert wird. Sprich das er eine XML erneut einließt.
Wie könnte man das sinnvoll umsetzen ?

F
10.010 Beiträge seit 2004
vor 14 Jahren

z.B. in dem Du die schon im FW vorhandenen Möglichkeiten genutzt hättest,
statt soetwas selber zu bauen.

WindowsFormsApplicationBase-Klasse

Singleinstance, Parameterübergabe an die uhrsprüngliche Version, SplashScreen, alles schon drin.

C
csharpLeben Themenstarter:in
7 Beiträge seit 2009
vor 14 Jahren

Ok blick ich leider nicht durch muss ich zugeben. Gibts irgendwo ein Beispiel am besten für C#? Bei dem Link nutzt er ja eine Visual Basic Assembly: Microsoft.VisualBasic.dll

C
401 Beiträge seit 2007
vor 14 Jahren

Hi,

einfach den Namespace Microsoft.VisualBasic.ApplicationServices hinzufügen (referenz im Projekt nicht vergessen) und dann eine Klasse von WindowsFormsApplicationBase ableiten, die in etwa wie folgt aussieht:


static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            MyApp app = new MyApp();
            app.Run(new Form1());
            
        }
    }

    class MyApp : WindowsFormsApplicationBase
    {
        public MyApp()
        {
            this.IsSingleInstance = true;
        }

        public void Run(Form mainForm)
        {
            this.MainForm = mainForm;
            this.Run(this.CommandLineArgs);
        }

        private void Run(ReadOnlyCollection<string> commandLineArgs)
        {

            // convert the Collection<string> to string[], so that it can be used

            // in the Run method.

            ArrayList list = new ArrayList(commandLineArgs);

            string[] commandLine = (string[])list.ToArray(typeof(string));

            this.Run(commandLine);

        }
    }

Ich hoffe das hilft dir.

Gruß

Dario