-1

I have an operation via While that i want to Freeze:

    do
    {
        ManualResetEvent.Reset(); // Here i want to wait

        // Here i am doing my stuff...
    }

    while (some boolean value);
}

My ManualResetEvent :

private static ManualResetEvent _manualResetEvent;

public static ManualResetEvent ManualResetEvent
{
    get { return _manualResetEvent; }
    set { _manualResetEvent = value; }
}

ManualResetEvent = new ManualResetEvent(false);

In some point in my code via Button i just want to freeze my operation:

private void btnPause_Click(object sender, RoutedEventArgs e)
{
    ManualResetEvent.WaitOne();
}

Is this the right way to do that ?

user979033
  • 5,430
  • 7
  • 32
  • 50

1 Answers1

2

You have your two functions backwards. The loop you want to wait needs to use the .WaitOne(). Also, if you want it to run at the start you need to initialize the reset event to true

init

private static ManualResetEvent _manualResetEvent;

public static ManualResetEvent ManualResetEvent
{
    get { return _manualResetEvent; }
    set { _manualResetEvent = value; }
}

ManualResetEvent = new ManualResetEvent(true); //Set it to true to let it run at the start.

loop

    do
    {
        ManualResetEvent.WaitOne(); // Here i want to wait

        // Here i am doing my stuff...
    }

    while (some boolean value);
}

elsewhere

private void btnPause_Click(object sender, RoutedEventArgs e)
{
    ManualResetEvent.Reset();
}
private void btnUnPause_Click(object sender, RoutedEventArgs e)
{
    ManualResetEvent.Set();
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431