Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have Boolean variable. I have timer which is based on this Boolean value. Both are in different form. Boolean is True when form is initialize. It set false on a specific condition. I want to put 2-3 second hold before it set to false.

//Form 1

Private void updateGrid()
{
    if(Form2.isBooleanTrue)
    {
        //Code to execurte
    }
}


//Form 2
public static isBooleanTrue = false;
Private void checkCondition()
{
    // I want to hold here. Note it should not hold the Form1 process
    isBooleanTrue = true;
}

Can any body suggest me how to hold the process before Boolean set false? So, timer can run for few more seconds.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
233 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use Thread.Sleep, which suspends current thread for given period of time.

Thread.Sleep(3000); // 3 sec wait.

Update

To leave the UI responding and get updates after few seconds you can do below.

this.Invoke((MethodInvoker)delegate {

    Thread.Sleep(3000); 
    // run your code on UI thread
});

Another option, start asynchoronous Task that performs your action.

Task.Factory.StartNew(()=>
{
    Thread.Sleep(3000); // wait 3 secs
    form1.Invoke(new Action(()=> 
               {
                    // your logic goes here.
               }));
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...