プロセス間で共有できるシングルトン クラスが必要です。そのため、当然 .NET 4.0 で Mutex を使用しました。
これで、同じアプリケーションの 2 つのインスタンスが実行されました。1 つはこのシングルトンの "Name" プロパティを変更し、他のアプリケーションがこの変更を取得することだけが必要です。ただし、最初のインスタンスが Mutex を解放した後でも、2 番目のインスタンスは WaitOne 呼び出しを成功させることができません。以下のコードでなぜ、何が間違っているのだろうか? エントリ ポイントはメソッド RunSingletonAcrossProcesses() であることに注意してください。
これが私のシングルトンクラスです
public class SingletonAcrossProcesses
{
private static SingletonAcrossProcesses _instance;
private static Mutex _mutex = new Mutex(true, @"Global\" + "sivablogz.wordpress.com SingletonMutexDemo");
protected SingletonAcrossProcesses()
{
}
public static SingletonAcrossProcesses GetInstance()
{
if (_mutex.WaitOne(TimeSpan.Zero, true))
{
if (_instance == null)
{
_instance = new SingletonAcrossProcesses();
}
_mutex.ReleaseMutex();
}
return _instance;
}
public string Name { get; set; }
public static void RunSingletonAcrossProcesses()
{
Console.WriteLine("Press any key to Instantiate the singleton...");
Console.ReadLine();
SingletonAcrossProcesses instance = SingletonAcrossProcesses.GetInstance();
if (instance != null)
{
if (String.IsNullOrEmpty(instance.Name))
{
Console.WriteLine("Enter a name for the instance...");
instance.Name = Console.ReadLine();
}
Console.WriteLine("The Instance name is: " + instance.Name);
}
else
Console.WriteLine("The Singleton Instance could not be obtained.");
Console.WriteLine("Press any key to terminate...");
Console.ReadLine();
}
}
前もって感謝します、
シヴァ