呼び出し元のスレッドにミューテックスの初期所有権があるかどうかを示す Boolean 型の値と、ミューテックスの名前を表す文字列を使用して、Mutex クラスの新しいインスタンスを初期化します。
名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文
'宣言
Public Sub New ( _
initiallyOwned As Boolean, _
name As String _
)
'使用
Dim initiallyOwned As Boolean
Dim name As String
Dim instance As New Mutex(initiallyOwned, name)
public Mutex (
bool initiallyOwned,
string name
)
public:
Mutex (
bool initiallyOwned,
String^ name
)
public Mutex (
boolean initiallyOwned,
String name
)
public function Mutex (
initiallyOwned : boolean,
name : String
)
適用できません。
パラメータ
- initiallyOwned
この呼び出しの結果として名前付きシステム ミューテックスが作成される場合に、呼び出し元スレッドに名前付きシステム ミューテックスの初期所有権を付与する場合は true。それ以外の場合は false。
- name
Mutex の名前。値が null 参照 (Visual Basic では Nothing) の場合、Mutex は無名になります。
例外
例外の種類 | 条件 |
---|---|
アクセス制御セキュリティを使用した名前付きミューテックスが存在しますが、ユーザーに System.Security.AccessControl.MutexRights.FullControl がありません。 |
|
Win32 エラーが発生しました。 |
|
名前付きミューテックスを作成できません。別の型の待機ハンドルに同じ名前が付けられていることが原因として考えられます。 |
|
name が 260 文字を超えています。 |
解説
name が null 参照 (Visual Basic では Nothing) ではなく initiallyOwned が true の場合、呼び出し元スレッドは、この呼び出しの結果として名前付きシステム ミューテックスが作成された場合にのみミューテックスを所有します。名前付きシステム ミューテックスが作成されたかどうかを確認するしくみはないため、このコンストラクタ オーバーロードを呼び出すときは initiallyOwned を false に設定することをお勧めします。初期所有権があるかどうかを調べる必要がある場合は、Mutex(Boolean,String,Boolean) コンストラクタを使用できます。
このコンストラクタは、名前付きシステム ミューテックスを表す Mutex オブジェクトを初期化します。同じ名前付きシステム ミューテックスを表す複数の Mutex オブジェクトを作成できます。
名前付きミューテックスがアクセス制御セキュリティで既に作成されていて、呼び出し元に System.Security.AccessControl.MutexRights.FullControl がない場合は、例外がスローされます。スレッドの動作を同期するために必要な権限でのみ既存の名前付きミューテックスを開く方法については、OpenExisting メソッドに関するトピックを参照してください。
name に null 参照 (Visual Basic では Nothing) または空の文字列を指定すると、Mutex(Boolean) コンストラクタを呼び出した場合と同様に、ローカル ミューテックスが作成されます。この場合、createdNew は常に true です。
名前付きミューテックスはシステム全体で有効なため、プロセス境界をまたがってリソース使用を調整するために使用できます。
使用例
' 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 constructor overload shown here cannot tell the calling thread
' whether initial ownership of the named mutex was granted. Therefore,
' do not request initial ownership unless you are certain that the
' thread will create the named mutex.
Imports System
Imports System.Threading
Public Class Test
<MTAThread> _
Public Shared Sub Main()
' Create the named mutex. Only one system object named
' "MyMutex" can exist; the local Mutex object represents
' this system object, regardless of which process or thread
' caused "MyMutex" to be created.
Dim m As New Mutex(False, "MyMutex")
' Try to gain control of the named mutex. If the mutex is
' controlled by another thread, wait for it to be released.
Console.WriteLine("Waiting for the Mutex.")
m.WaitOne()
' Keep control of the mutex until the user presses
' ENTER.
Console.WriteLine("This application owns the mutex. " _
& "Press ENTER to release the mutex and exit.")
Console.ReadLine()
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 constructor overload shown here cannot tell the calling thread
// whether initial ownership of the named mutex was granted. Therefore,
// do not request initial ownership unless you are certain that the
// thread will create the named mutex.
using System;
using System.Threading;
public class Test
{
public static void Main()
{
// Create the named mutex. Only one system object named
// "MyMutex" can exist; the local Mutex object represents
// this system object, regardless of which process or thread
// caused "MyMutex" to be created.
Mutex m = new Mutex(false, "MyMutex");
// Try to gain control of the named mutex. If the mutex is
// controlled by another thread, wait for it to be released.
Console.WriteLine("Waiting for the Mutex.");
m.WaitOne();
// Keep control of the mutex until the user presses
// ENTER.
Console.WriteLine("This application owns the mutex. " +
"Press ENTER to release the mutex and exit.");
Console.ReadLine();
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 constructor overload shown here cannot tell the calling thread
// whether initial ownership of the named mutex was granted. Therefore,
// do not request initial ownership unless you are certain that the
// thread will create the named mutex.
using namespace System;
using namespace System::Threading;
int main()
{
// Create the named mutex. Only one system object named
// "MyMutex" can exist; the local Mutex object represents
// this system object, regardless of which process or thread
// caused "MyMutex" to be created.
Mutex^ m = gcnew Mutex( false,"MyMutex" );
// Try to gain control of the named mutex. If the mutex is
// controlled by another thread, wait for it to be released.
Console::WriteLine( "Waiting for the Mutex." );
m->WaitOne();
// Keep control of the mutex until the user presses
// ENTER.
Console::WriteLine( "This application owns the mutex. "
"Press ENTER to release the mutex and exit." );
Console::ReadLine();
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 constructor overload shown here cannot tell the calling thread
// whether initial ownership of the named mutex was granted. Therefore,
// do not request initial ownership unless you are certain that the
// thread will create the named mutex.
import System.*;
import System.Threading.*;
import System.Threading.Thread;
public class Test
{
public static void main(String[] args)
{
// Create the named mutex. Only one system object named
// "MyMutex" can exist; the local Mutex object represents
// this system object, regardless of which process or thread
// caused "MyMutex" to be created.
Mutex m = new Mutex(false, "MyMutex");
// Try to gain control of the named mutex. If the mutex is
// controlled by another thread, wait for it to be released.
Console.WriteLine("Waiting for the Mutex.");
m.WaitOne();
// Keep control of the mutex until the user presses
// ENTER.
Console.WriteLine(("This application owns the mutex. "
+ "Press ENTER to release the mutex and exit."));
Console.ReadLine();
m.ReleaseMutex();
} //main
} //Test
.NET Framework のセキュリティ
- SecurityPermissionAttribute (アンマネージ コードを呼び出して、名前付きミューテックスを作成するために必要なアクセス許可)SecurityPermissionFlag.UnmanagedCode (関連する列挙体)。 LinkDemand (セキュリティ アクション)。
プラットフォーム
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 名前空間