1

I'm having trouble making a thread wait for two seconds without blocking the GUI. The most simple wait method I know is Thread.Sleep(2000);. If you can use some examples of timers or others that I'm not aware of, please do because I'm not too familiar with the ways of coding.

private void run_program_Click(object sender, RoutedEventArgs e)
{
    if (comboBox1.Text == "Drive forwards and back")
    {
        stop.IsEnabled = true;

        EngineA(90); //Makes EngineA drive at 90% power
        EngineB(90); //Makes EngineB drive at 90% power

        // Basicly it has to wait two seconds here

        EngineA(-90); // -90% power aka. reverse
        EngineB(-90); // -90% power

        // Also two seconds here

        EngineA(0); // Stops the engine
        EngineB(0); // Stops
        EngineC();
     }
}
1
  • 2
    Please look at the edited source of your post for an example of how to post a block of code - there was no need for all those backticks. Commented Feb 4, 2014 at 8:52

3 Answers 3

6

If you're using C# 5, the simplest approach is to make the method async:

private async void RunProgramClick(object sender, RoutedEventArgs e)
{
    // Reverse the logic to reduce nesting and use "early out"
    if (comboBox1.Text != "Drive forwards and back")
    {
        return;
    }

    stop.IsEnabled = true;
    EngineA(90);
    EngineB(90);

    await Task.Delay(2000);

    EngineA(-90);
    EngineB(-90);

    await Task.Delay(2000);

    EngineA(0);
    EngineB(0);
    EngineC();
}
Sign up to request clarification or add additional context in comments.

6 Comments

I'm using C# 4.0, And I get two errors when using async. Error one: The type or namespace name 'async' could not be found (are you missing a using directive or an assmbly refrence?) And the second error: Invalid token 'void' in class, struct, or interface member declaration.
@DeliciousCake: Well yes, you would - because async was introduced in C# 5. Hence the "If you're using C# 5" part of my answer. Any chance you could upgrade to VS2012 or VS2013? It would make your life much simpler...
@Jon_Skeet Unfortunetly not :/ Tech school dosn't allow us to install programs, I need to figure this out within' 1½ week or I'll might fail my exam :(
@DeliciousCake: In that case I suggest you use a DispatcherTimer. That will let you effectively defer some work for later.
@Jon_Skeet Got an example of how to make wait the two seconds within two commands? It'll help alot if I can move on from this problem
|
1
    /// <summary>
    /// WPF Wait
    /// </summary>
    /// <param name="seconds"></param>
    public static void Wait(double seconds)
    {
        var frame = new DispatcherFrame();
        new Thread((ThreadStart)(() =>
        {
            Thread.Sleep(TimeSpan.FromSeconds(seconds));
            frame.Continue = false;
        })).Start();
        Dispatcher.PushFrame(frame);
    }

1 Comment

There are at least a dozen duplicates of this question on SO, and this is THE solution
0

I found this approach simpler,

var task = Task.Factory.StartNew(() => Thread.Sleep(new TimeSpan(0,0,2)));
Task.WaitAll(new[] { task });

Late answer, but i hope it should be useful for someone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.