次の方法で共有


ManualResetEvent.Reset メソッド

指定したイベントの状態を非シグナル状態に設定します。

Public Function Reset() As Boolean
[C#]
public bool Reset();
[C++]
public: bool Reset();
[JScript]
public function Reset() : Boolean;

戻り値

関数が成功した場合は true 。それ以外の場合は false

使用例

[Visual Basic, C#, C++] 待機ハンドルを使用して、複雑な数値計算の各段階の完了をシグナル通知する例を次に示します。計算の形式は、 result = first term + second

term + third term です。各項目には、事前計算と、算出された基数を使用した最終計算が行われます。

 
Imports System
Imports System.Threading

Public Class CalculateTest

    <MTAThreadAttribute> _
    Shared Sub Main()
        Dim calc As New Calculate()
        Console.WriteLine("Result = {0}.", _
            calc.Result(234).ToString())
        Console.WriteLine("Result = {0}.", _
            calc.Result(55).ToString())
    End Sub
End Class

Public Class Calculate

    Dim baseNumber, firstTerm, secondTerm, thirdTerm As Double
    Dim autoEvents() As AutoResetEvent
    Dim manualEvent As ManualResetEvent

    ' Generate random numbers to simulate the actual calculations.
    Dim randomGenerator As Random 

    Sub New()
        autoEvents = New AutoResetEvent(2) { _
            New AutoResetEvent(False), _
            New AutoResetEvent(False), _
            New AutoResetEvent(False) }

        manualEvent = New ManualResetEvent(False)
    End Sub

    Private Sub CalculateBase(stateInfo As Object)
        baseNumber = randomGenerator.NextDouble()

        ' Signal that baseNumber is ready.
        manualEvent.Set()
    End Sub

    ' The following CalculateX methods all perform the same
    ' series of steps as commented in CalculateFirstTerm.

    Private Sub CalculateFirstTerm(stateInfo As Object)
    
        ' Perform a precalculation.
        Dim preCalc As Double = randomGenerator.NextDouble()

        ' Wait for baseNumber to be calculated.
        manualEvent.WaitOne()

        ' Calculate the first term from preCalc and baseNumber.
        firstTerm = preCalc * baseNumber * _
            randomGenerator.NextDouble()

        ' Signal that the calculation is finished.
        autoEvents(0).Set()
    End Sub

    Private Sub CalculateSecondTerm(stateInfo As Object)
        Dim preCalc As Double = randomGenerator.NextDouble()
        manualEvent.WaitOne()
        secondTerm = preCalc * baseNumber * _
            randomGenerator.NextDouble()
        autoEvents(1).Set()
    End Sub

    Private Sub CalculateThirdTerm(stateInfo As Object)
        Dim preCalc As Double = randomGenerator.NextDouble()
        manualEvent.WaitOne()
        thirdTerm = preCalc * baseNumber * _
            randomGenerator.NextDouble()
        autoEvents(2).Set()
    End Sub

    Function Result(seed As Integer) As Double

        randomGenerator = New Random(seed)

        ' Simultaneously calculate the terms.
        ThreadPool.QueueUserWorkItem(AddressOf CalculateBase)
        ThreadPool.QueueUserWorkItem(AddressOf CalculateFirstTerm)
        ThreadPool.QueueUserWorkItem(AddressOf CalculateSecondTerm)
        ThreadPool.QueueUserWorkItem(AddressOf CalculateThirdTerm)

        ' Wait for all of the terms to be calculated.
        WaitHandle.WaitAll(autoEvents)

        ' Reset the wait handle for the next calculation.
        manualEvent.Reset()

        Return firstTerm + secondTerm + thirdTerm
    End Function

End Class

[C#] 
using System;
using System.Threading;

class CalculateTest
{
    static void Main()
    {
        Calculate calc = new Calculate();
        Console.WriteLine("Result = {0}.", 
            calc.Result(234).ToString());
        Console.WriteLine("Result = {0}.", 
            calc.Result(55).ToString());
    }
}

class Calculate
{
    double baseNumber, firstTerm, secondTerm, thirdTerm;
    AutoResetEvent[] autoEvents;
    ManualResetEvent manualEvent;

    // Generate random numbers to simulate the actual calculations.
    Random randomGenerator;

    public Calculate()
    {
        autoEvents = new AutoResetEvent[]
        {
            new AutoResetEvent(false),
            new AutoResetEvent(false),
            new AutoResetEvent(false)
        };

        manualEvent = new ManualResetEvent(false);
    }

    void CalculateBase(object stateInfo)
    {
        baseNumber = randomGenerator.NextDouble();

        // Signal that baseNumber is ready.
        manualEvent.Set();
    }

    // The following CalculateX methods all perform the same
    // series of steps as commented in CalculateFirstTerm.

    void CalculateFirstTerm(object stateInfo)
    {
        // Perform a precalculation.
        double preCalc = randomGenerator.NextDouble();

        // Wait for baseNumber to be calculated.
        manualEvent.WaitOne();

        // Calculate the first term from preCalc and baseNumber.
        firstTerm = preCalc * baseNumber * 
            randomGenerator.NextDouble();

        // Signal that the calculation is finished.
        autoEvents[0].Set();
    }

    void CalculateSecondTerm(object stateInfo)
    {
        double preCalc = randomGenerator.NextDouble();
        manualEvent.WaitOne();
        secondTerm = preCalc * baseNumber * 
            randomGenerator.NextDouble();
        autoEvents[1].Set();
    }

    void CalculateThirdTerm(object stateInfo)
    {
        double preCalc = randomGenerator.NextDouble();
        manualEvent.WaitOne();
        thirdTerm = preCalc * baseNumber * 
            randomGenerator.NextDouble();
        autoEvents[2].Set();
    }

    public double Result(int seed)
    {
        randomGenerator = new Random(seed);

        // Simultaneously calculate the terms.
        ThreadPool.QueueUserWorkItem(
            new WaitCallback(CalculateBase));
        ThreadPool.QueueUserWorkItem(
            new WaitCallback(CalculateFirstTerm));
        ThreadPool.QueueUserWorkItem(
            new WaitCallback(CalculateSecondTerm));
        ThreadPool.QueueUserWorkItem(
            new WaitCallback(CalculateThirdTerm));

        // Wait for all of the terms to be calculated.
        WaitHandle.WaitAll(autoEvents);

        // Reset the wait handle for the next calculation.
        manualEvent.Reset();

        return firstTerm + secondTerm + thirdTerm;
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;

__gc class Calculate
{
    double baseNumber, firstTerm, secondTerm, thirdTerm;
    AutoResetEvent* autoEvents __gc [];
    ManualResetEvent* manualEvent;

    // Generate random numbers to simulate the actual calculations.
    Random* randomGenerator;

public:
    Calculate()
    {
        autoEvents = new AutoResetEvent* __gc [3];
        autoEvents[0] = new AutoResetEvent(false);
        autoEvents[1] = new AutoResetEvent(false);
        autoEvents[2] = new AutoResetEvent(false);

        manualEvent = new ManualResetEvent(false);
    }

private:
    void CalculateBase(Object* stateInfo)
    {
        baseNumber = randomGenerator->NextDouble();

        // Signal that baseNumber is ready.
        manualEvent->Set();
    }

    // The following CalculateX methods all perform the same
    // series of steps as commented in CalculateFirstTerm.

    void CalculateFirstTerm(Object* stateInfo)
    {
        // Perform a precalculation.
        double preCalc = randomGenerator->NextDouble();

        // Wait for baseNumber to be calculated.
        manualEvent->WaitOne();

        // Calculate the first term from preCalc and baseNumber.
        firstTerm = preCalc * baseNumber * 
            randomGenerator->NextDouble();

        // Signal that the calculation is finished.
        autoEvents[0]->Set();
    }

    void CalculateSecondTerm(Object* stateInfo)
    {
        double preCalc = randomGenerator->NextDouble();
        manualEvent->WaitOne();
        secondTerm = preCalc * baseNumber * 
            randomGenerator->NextDouble();
        autoEvents[1]->Set();
    }

    void CalculateThirdTerm(Object* stateInfo)
    {
        double preCalc = randomGenerator->NextDouble();
        manualEvent->WaitOne();
        thirdTerm = preCalc * baseNumber * 
            randomGenerator->NextDouble();
        autoEvents[2]->Set();
    }

public:
    double Result(int seed)
    {
        randomGenerator = new Random(seed);

        // Simultaneously calculate the terms.
        ThreadPool::QueueUserWorkItem(
            new WaitCallback(this, &Calculate::CalculateBase));
        ThreadPool::QueueUserWorkItem(
            new WaitCallback(this, &Calculate::CalculateFirstTerm));
        ThreadPool::QueueUserWorkItem(
            new WaitCallback(this, &Calculate::CalculateSecondTerm));
        ThreadPool::QueueUserWorkItem(
            new WaitCallback(this, &Calculate::CalculateThirdTerm));

        // Wait for all of the terms to be calculated.
        WaitHandle::WaitAll(autoEvents);

        // Reset the wait handle for the next calculation.
        manualEvent->Reset();

        return firstTerm + secondTerm + thirdTerm;
    }
};

void main()
{
    Calculate* calc = new Calculate();
    Console::WriteLine(S"Result = {0}.", 
        calc->Result(234).ToString());
    Console::WriteLine(S"Result = {0}.", 
        calc->Result(55).ToString());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

ManualResetEvent クラス | ManualResetEvent メンバ | System.Threading 名前空間 | スレッド処理 | ManualResetEvent