次の方法で共有


Mutex コンストラクタ (Boolean, String, Boolean)

Boolean 型の値を使用して、Mutex クラスの新しいインスタンスを初期化します。この Boolean 型の値は、呼び出し元のスレッドにミューテックスの初期所有権があるかどうか、ミューテックスの名前を表す文字列、およびメソッドが返されるときにミューテックスの初期所有権が呼び出し元のスレッドに付与されたかどうかを示す Boolean 値を示します。

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)

構文

'宣言
Public Sub New ( _
    initiallyOwned As Boolean, _
    name As String, _
    <OutAttribute> ByRef createdNew As Boolean _
)
'使用
Dim initiallyOwned As Boolean
Dim name As String
Dim createdNew As Boolean

Dim instance As New Mutex(initiallyOwned, name, createdNew)
public Mutex (
    bool initiallyOwned,
    string name,
    out bool createdNew
)
public:
Mutex (
    bool initiallyOwned, 
    String^ name, 
    [OutAttribute] bool% createdNew
)
public Mutex (
    boolean initiallyOwned, 
    String name, 
    /** @attribute OutAttribute() */ /** @ref */ boolean createdNew
)
public function Mutex (
    initiallyOwned : boolean, 
    name : String, 
    createdNew : boolean
)
適用できません。

パラメータ

  • initiallyOwned
    この呼び出しの結果として名前付きシステム ミューテックスが作成される場合に、呼び出し元スレッドに名前付きシステム ミューテックスの初期所有権を付与する場合は true。それ以外の場合は false
  • name
    Mutex の名前。値が null 参照 (Visual Basic では Nothing) の場合、Mutex は無名になります。
  • createdNew
    このメソッドから制御が戻るときに、ローカル ミューテックスが作成された場合 (name が null 参照 (Visual Basic では Nothing) または空の文字列の場合)、または指定した名前付きシステム ミューテックスが作成された場合はブール値 true が格納されます。指定した名前付きシステム ミューテックスが既に存在する場合は false が格納されます。このパラメータは初期化せずに渡されます。

例外

例外の種類 条件

UnauthorizedAccessException

アクセス制御セキュリティを使用した名前付きミューテックスが存在しますが、ユーザーに System.Security.AccessControl.MutexRights.FullControl がありません。

IOException

Win32 エラーが発生しました。

ApplicationException

名前付きミューテックスを作成できません。別の型の待機ハンドルに同じ名前が付けられていることが原因として考えられます。

ArgumentException

name が 260 文字を超えています。

解説

name が null 参照 (Visual Basic では Nothing) ではなく initiallyOwned が true の場合、呼び出し元スレッドは、呼び出し後に createdNew が true である場合にのみ名前付きミューテックスを所有します。それ以外の場合、スレッドは WaitOne メソッドを呼び出すことによってミューテックスを要求できます。

このコンストラクタは、名前付きシステム ミューテックスを表す Mutex オブジェクトを初期化します。同じ名前付きシステム ミューテックスを表す複数の Mutex オブジェクトを作成できます。

名前付きミューテックスがアクセス制御セキュリティで既に作成されていて、呼び出し元に System.Security.AccessControl.MutexRights.FullControl 権限がない場合は、例外がスローされます。スレッドの動作を同期するために必要な権限でのみ既存の名前付きミューテックスを開く方法については、OpenExisting メソッドに関するトピックを参照してください。

name に null 参照 (Visual Basic では Nothing) または空の文字列を指定すると、Mutex(Boolean) コンストラクタを呼び出した場合と同様に、ローカル ミューテックスが作成されます。この場合、createdNew は常に true です。

名前付きミューテックスはシステム全体で有効なため、プロセス境界をまたがってリソース使用を調整するために使用できます。

使用例

名前付きミューテックスを使用してプロセス間またはスレッド間の通知を行うコード例を次に示します。このプログラムは、複数のコマンド ウィンドウから実行します。各プロセスで、名前付きミューテックス "MyMutex" を表す Mutex オブジェクトが作成されます。名前付きミューテックスはシステム オブジェクトです。この例では、名前付きミューテックスの有効期間が、この名前付きミューテックスを表す Mutex オブジェクトの有効期間によって制限されています。名前付きミューテックスは、最初のプロセスでローカル Mutex オブジェクトが作成されるときに作成され、この名前付きミューテックスを表す Mutex オブジェクトがすべて解放されたときに破棄されます。初期状態では、名前付きミューテックスは最初のプロセスによって所有されています。2 番目のプロセスおよび後続のプロセスは、それ以前のプロセスが名前付きミューテックスを解放するまで待機します。

' This example shows how a named mutex is used to signal between
' processes or threads.
' Run this program from two (or more) command windows. Each process
' creates a Mutex object that represents the named mutex "MyMutex".
' The named mutex is a system object whose lifetime is bounded by the
' lifetimes of the Mutex objects that represent it. The named mutex
' is created when the first process creates its local Mutex; in this
' example, the named mutex is owned by the first process. The named 
' mutex is destroyed when all the Mutex objects that represent it
' have been released. 
' The second process (and any subsequent process) waits for earlier
' processes to release the named mutex.

Imports System
Imports System.Threading

Public Class Test
   
   <MTAThread> _
   Public Shared Sub Main()
      ' Set this variable to false if you do not want to request 
      ' initial ownership of the named mutex.
      Dim requestInitialOwnership As Boolean = True
      Dim mutexWasCreated As Boolean
      
      ' Request initial ownership of the named mutex by passing
      ' true for the first parameter. Only one system object named 
      ' "MyMutex" can exist; the local Mutex object represents 
      ' this system object. If "MyMutex" is created by this call,
      ' then mutexWasCreated contains true; otherwise, it contains
      ' false.
      Dim m As New Mutex(requestInitialOwnership, "MyMutex", _
          mutexWasCreated)
      
      ' This thread owns the mutex only if it both requested 
      ' initial ownership and created the named mutex. Otherwise,
      ' it can request the named mutex by calling WaitOne.
      If Not (requestInitialOwnership And mutexWasCreated) Then
         Console.WriteLine("Waiting for the named mutex.")
         m.WaitOne()
      End If
      
      ' Once the process has gained control of the named mutex,
      ' hold onto it until the user presses ENTER.
      Console.WriteLine("This process owns the named mutex. " _
          & "Press ENTER to release the mutex and exit.")
      Console.ReadLine()
      
      ' Call ReleaseMutex to allow other threads to gain control
      ' of the named mutex. If you keep a reference to the local
      ' Mutex, you can call WaitOne to request control of the 
      ' named mutex.
      m.ReleaseMutex()
   End Sub 'Main
End Class 'Test
// This example shows how a named mutex is used to signal between
// processes or threads.
// Run this program from two (or more) command windows. Each process
// creates a Mutex object that represents the named mutex "MyMutex".
// The named mutex is a system object whose lifetime is bounded by the
// lifetimes of the Mutex objects that represent it. The named mutex
// is created when the first process creates its local Mutex; in this
// example, the named mutex is owned by the first process. The named 
// mutex is destroyed when all the Mutex objects that represent it
// have been released. 
// The second process (and any subsequent process) waits for earlier
// processes to release the named mutex.

using System;
using System.Threading;

public class Test
{
    public static void Main()
    {
        // Set this variable to false if you do not want to request 
        // initial ownership of the named mutex.
        bool requestInitialOwnership = true;
        bool mutexWasCreated;

        // Request initial ownership of the named mutex by passing
        // true for the first parameter. Only one system object named 
        // "MyMutex" can exist; the local Mutex object represents 
        // this system object. If "MyMutex" is created by this call,
        // then mutexWasCreated contains true; otherwise, it contains
        // false.
        Mutex m = new Mutex(requestInitialOwnership, 
                            "MyMutex", 
                            out mutexWasCreated);
        
        // This thread owns the mutex only if it both requested 
        // initial ownership and created the named mutex. Otherwise,
        // it can request the named mutex by calling WaitOne.
        if (!(requestInitialOwnership && mutexWasCreated))
        {
            Console.WriteLine("Waiting for the named mutex.");
            m.WaitOne();
        }

        // Once the process has gained control of the named mutex,
        // hold onto it until the user presses ENTER.
        Console.WriteLine("This process owns the named mutex. " +
            "Press ENTER to release the mutex and exit.");
        Console.ReadLine();

        // Call ReleaseMutex to allow other threads to gain control
        // of the named mutex. If you keep a reference to the local
        // Mutex, you can call WaitOne to request control of the 
        // named mutex.
        m.ReleaseMutex();
    }
}
// This example shows how a named mutex is used to signal between
// processes or threads.
// Run this program from two (or more) command windows. Each process
// creates a Mutex object that represents the named mutex "MyMutex".
// The named mutex is a system object whose lifetime is bounded by the
// lifetimes of the Mutex objects that represent it. The named mutex
// is created when the first process creates its local Mutex; in this
// example, the named mutex is owned by the first process. The named 
// mutex is destroyed when all the Mutex objects that represent it
// have been released. 
// The second process (and any subsequent process) waits for earlier
// processes to release the named mutex.
using namespace System;
using namespace System::Threading;
int main()
{
   
   // Set this variable to false if you do not want to request 
   // initial ownership of the named mutex.
   bool requestInitialOwnership = true;
   bool mutexWasCreated;
   
   // Request initial ownership of the named mutex by passing
   // true for the first parameter. Only one system object named 
   // "MyMutex" can exist; the local Mutex object represents 
   // this system object. If "MyMutex" is created by this call,
   // then mutexWasCreated contains true; otherwise, it contains
   // false.
   Mutex^ m = gcnew Mutex( requestInitialOwnership, "MyMutex", mutexWasCreated );
   
   // This thread owns the mutex only if it both requested 
   // initial ownership and created the named mutex. Otherwise,
   // it can request the named mutex by calling WaitOne.
   if (  !(requestInitialOwnership && mutexWasCreated) )
   {
      Console::WriteLine(  "Waiting for the named mutex." );
      m->WaitOne();
   }

   
   // Once the process has gained control of the named mutex,
   // hold onto it until the user presses ENTER.
   Console::WriteLine(  "This process owns the named mutex. "
    "Press ENTER to release the mutex and exit." );
   Console::ReadLine();
   
   // Call ReleaseMutex to allow other threads to gain control
   // of the named mutex. If you keep a reference to the local
   // Mutex, you can call WaitOne to request control of the 
   // named mutex.
   m->ReleaseMutex();
}
// This example shows how a named mutex is used to signal between
// processes or threads.
// Run this program from two (or more) command windows. Each process
// creates a Mutex object that represents the named mutex "MyMutex".
// The named mutex is a system object whose lifetime is bounded by the
// lifetimes of the Mutex objects that represent it. The named mutex
// is created when the first process creates its local Mutex; in this
// example, the named mutex is owned by the first process. The named 
// mutex is destroyed when all the Mutex objects that represent it
// have been released. 
// The second process (and any subsequent process) waits for earlier
// processes to release the named mutex.
import System.*;
import System.Threading.*;
import System.Threading.Thread;

public class Test
{
    public static void main(String[] args)
    {
        // Set this variable to false if you do not want to request 
        // initial ownership of the named mutex.
        boolean requestInitialOwnership = true;
        boolean mutexWasCreated = false;

        // Request initial ownership of the named mutex by passing
        // true for the first parameter. Only one system object named 
        // "MyMutex" can exist; the local Mutex object represents 
        // this system object. If "MyMutex" is created by this call,
        // then mutexWasCreated contains true; otherwise, it contains
        // false.
        Mutex m = new Mutex(requestInitialOwnership, "MyMutex", 
            mutexWasCreated);

        // This thread owns the mutex only if it both requested 
        // initial ownership and created the named mutex. Otherwise,
        // it can request the named mutex by calling WaitOne.
        if (!((requestInitialOwnership && mutexWasCreated))) {
            Console.WriteLine("Waiting for the named mutex.");
            m.WaitOne();
        }

        // Once the process has gained control of the named mutex,
        // hold onto it until the user presses ENTER.
        Console.WriteLine(("This process owns the named mutex. " 
            + "Press ENTER to release the mutex and exit."));
        Console.ReadLine();

        // Call ReleaseMutex to allow other threads to gain control
        // of the named mutex. If you keep a reference to the local
        // Mutex, you can call WaitOne to request control of the 
        // named mutex.
        m.ReleaseMutex();
    } //main
} //Test

.NET Framework のセキュリティ

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

Mutex クラス
Mutex メンバ
System.Threading 名前空間

その他の技術情報

マネージ スレッド処理
ミューテックス