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 ?