简体   繁体   中英

Delay 2 seconds to delete an item

I have a listview with checkboxes enabled. When a user checks an item I need to remove this item after two seconds, without locking the screen.

How I can do it? Do I need to use a thread?

Use a Timer to perform some operation after a given amount of time. Because this is performing the operation asynchronously, rather than synchronously, the UI thread won't be blocked.

The other option is to leverage await :

private async void checkbox_CheckedChanged(object sender, EventArgs e)
{
    if (checkbox.Checked)
    {
        await Task.Delay(2000);
        RemoveItem();
    }
}

Use a Timer control from the System.Windows.Forms namespace.

That will trigger an event in the main thread when the time has elapsed, so you won't have any issues to try to access the form from a different thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM