ユーザーがアプリケーションアイコンをダブルクリックしたときに、このアプリケーションの別のインスタンスがまだ実行されていないことを確認したいと思います。
My.Applicationについて読みましたが、どうしたらよいかわかりません。
ユーザーがアプリケーションアイコンをダブルクリックしたときに、このアプリケーションの別のインスタンスがまだ実行されていないことを確認したいと思います。
My.Applicationについて読みましたが、どうしたらよいかわかりません。
これは私が使用したものです...(.NET 2.0のC#)
[STAThread]
private static void Main(string[] args)
{
//this follows best practices on
//ensuring that this is a single instance app.
string mutexName = "e50cf829-f6b9-471e-8d9f-67eac3699f09";
bool grantedOwnership;
//we prefix the mutexName with "Local\\" to allow this to run under terminal services.
//The "Local\\" prefix forces this into local user space.
//If we want to forbid this in TS, use the "Global\\" prefix.
Mutex singleInstanceMutex = new Mutex(true, "Global\\" + mutexName, out grantedOwnership);
try
{
if (!grantedOwnership)
{
MessageBox.Show("Error: X is already running.\n\nYou can only run one copy of X at a time.", "X", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
Application.Exit();
}
else
{
Application.Run(new X(args));
}
}
finally
{
singleInstanceMutex.Close();
}
}
VB .NETには、あなたに代わって仕事をするIsSingleInstanceブールプロパティがあります。
VBの場合(ここから取得):
Public Class Program
Inherits Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
Public Sub New()
Me.IsSingleInstance = True
End Sub
End Class
C#での使用方法は次のとおりです(ここから取得)。
// SingleInstanceApplication.cs
class SingleInstanceApplication : WindowsFormsApplicationBase {
// Must call base constructor to ensure correct initial
// WindowsFormsApplicationBase configuration
public SingleInstanceApplication() {
// This ensures the underlying single-SDI framework is employed,
// and OnStartupNextInstance is fired
this.IsSingleInstance = true;
}
}
// Program.cs
static class Program {
[STAThread]
static void Main(string[] args) {
Application.EnableVisualStyles();
SingleInstanceApplication application =
new SingleInstanceApplication();
application.Run(args);
}
}
プロジェクトでMicrosoft.VisualBasic.dllを参照してください。
プロジェクトのプロパティ ([アプリケーション] タブ) を開き、 [単一インスタンス アプリケーションを作成する] オプションをオンにします。
[アプリケーション] タブから、[アプリケーション イベントの表示] ボタンをクリックして、2 番目のインスタンス イベントを処理できるApplicationEvents.vbクラスを作成することもできます。
Partial Friend Class MyApplication
Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
' Bring First Instance to Foreground
e.BringToForeground = True
' Pass Second Instance Command Line to First Instance
AppShared.DoSomethingWithCommandLine(e.CommandLine)
End Sub
End Class
スコット・ハンゼルマンはこれについて素晴らしい記事を書いています。コードはC#ですが、VBに移植するのは簡単だと思います。
ニーズを満たさない場合に備えて、このトピックに関するもう1つの記事を次に示します。
VB.NET では、単一インスタンス アプリはプロジェクト プロパティ ページの単なるチェックボックスです。My.Application.StartupNextInstanceイベントをトラップして、別のコピーが起動されたときに単一のインスタンスが何かを実行するようにすることもできます。これは、たとえば、要求されたドキュメントを元のインスタンスで開く MDI のような動作に使用できます。
舞台裏では、これはかなりの量のミューテックスと IPC goo をカプセル化します ( WindowsFormApplicationBaseを参照)。C# からも使用できます。
を使用しMutex
ます。事実上、aMutex
は文字列で名前を付けることができ、CLR 全体で一意です。
サンプルコード:
try
{
mutex = Mutex.OpenExisting(mutexName);
//since it hasn’t thrown an exception, then we already have one copy of the app open.
MessageBox.Show(”A copy of Todo 3.0 is already open. Please check your system tray (notification area).”,
“Todo 3.0″, MessageBoxButtons.OK, MessageBoxIcon.Information);
Environment.Exit(0);
}
catch (Exception Ex)
{
//since we didn’t find a mutex with that name, create one
Debug.WriteLine(”Exception thrown:” + Ex.Message + ” Creating a new mutex…”);
mutex = new Mutex(true, mutexName);
}
この投稿から:
アプリケーションが VB.NET 2.0 ~ 3.5 の場合、プログラムの単一インスタンスの実行を維持する最も簡単な方法は、「Windows アプリケーション フレームワーク プロパティ」を使用することです。そこに到達するには、プロジェクト名を右クリックして [プロパティ] に移動します。そこで、[単一インスタンス アプリケーションを作成] チェックボックスを選択します。
ApplicationEvents.vb を使用して、プログラムを 2 回実行したことをユーザーに示すこともできます。[アプリケーション イベントの表示] ボタンを選択すると、同じプロパティ ウィンドウで簡単に作成/表示できます。その中で、 MyApplication_StartupNextInstance サブを選択し、次のようにそこにコードを入力できます。
Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
MessageBox.Show("This program is already running. If you do not see the program running, please check your " _
& "Windows Task Manager for this program name in the 'Processes' Tab." & vbNewLine & vbNewLine & "WARNING: " _
& " If you terminate the process, you will terminate the only instance of this program!", My.Application.Info.ProductName.ToString _
& " is Running!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Sub
これが役立つかどうか教えてください!JFV
アプリにハードコードされたGUIDなどの一意の識別子を割り当て、ミューテックスにその識別子を割り当てるミューテックスインスタンスを作成します。例外がスローされた場合は、アプリケーションがすでに実行されていることを意味します(ミューテックスの作成に成功したため)
リンクの代わりにここにサンプルを置く方が簡単だと思いました。
[STAThread]
static void Main()
{
if (!IsAppAlreadyRunning())
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); /* Change Form1 for your main Form */
}
else
{
MessageBox.Show("Application is already running!");
}
}
public static bool IsAppAlreadyRunning()
{
Process currentProcess = Process.GetCurrentProcess();
return (IsAppAlreadyRunning(currentProcess.Id,
currentProcess.ProcessName));
}
private static bool IsAppAlreadyRunning(int ID, string Name)
{
bool isAlreadyRunning = false;
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
if (ID != process.Id)
{
if (Name == process.ProcessName)
{
isAlreadyRunning = true;
break;
}
}
}
return isAlreadyRunning;
}
これを行うための最も一般的なパターンは、シングルトンパターンを使用することです。言語を指定していないので、ここではC#を参照していると仮定します。そうでない場合でも、原則はほとんどのオブジェクト指向言語で同じです。
この記事はあなたにいくつかの助けを与えるはずです。