-5

For my Win Form program, I need to wait 5 second but the normal c# Thread.Sleep(5000); doesn’t work in the Win Form so I tried Task.Delay(5000); but it still doesn’t work. Help

public void waitThen();
{
    Task.Delay(5000);
    checkForDone();
}
XYink
  • 3
  • 5
  • 1
    `public async Task waitThen() { await Task.Delay(5000); }` -- The it depends on what `checkForDone();` is doing: You can add `.ConfigureAwait(false)` to the delay, eventually. – Jimi Feb 08 '22 at 17:39
  • Please provide a [mcve]. Show how you're calling this method etc. – mason Feb 08 '22 at 17:39
  • I just need to wait 5 seconds @mason – XYink Feb 08 '22 at 17:41
  • `Task.Delay` is an asynchronous operation. You need to `await` it. – David Feb 08 '22 at 17:42
  • 3
    *the normal c# Thread.Sleep(5000); doesn’t work in the Win Form* - it does, it just jams the UI – Caius Jard Feb 08 '22 at 17:44
  • When I write Thread.Sleep(5000); its says that Thread is not a thing in the curent context – XYink Feb 08 '22 at 17:46
  • Then go to the word `Thread` in your code and press Ctrl+ and the IDE will help you – Sir Rufo Feb 08 '22 at 17:47
  • @Jimi The checkForDone(); is just a messagebox and reset some variables – XYink Feb 08 '22 at 17:47
  • @XYink: And did you add `using System.Threading;` to address that? Much in the same way that `Task` doesn't exist in the current context without `using System.Threading.Tasks;` – David Feb 08 '22 at 17:48
  • @Jimi er.. can you be sure it's safe to continue on a non UI thread, outside of what you can see here? – Caius Jard Feb 08 '22 at 17:48
  • I thought it was there by default – XYink Feb 08 '22 at 17:48
  • @CaiusJard *it depends on what `checkForDone();` is doing [...], eventually* – Jimi Feb 08 '22 at 17:49
  • 1
    *I thought it was there by default* - looking at the top of the file would make sure - given that youre getting a complaint, it's probably not! – Caius Jard Feb 08 '22 at 17:49
  • @Jimi indeed, which we can't see - I don't think I'd advocate CA(f) here without seeing everything else too – Caius Jard Feb 08 '22 at 17:50
  • I can’t check it right now but I know that I didn’t change or add any imports/using so its the default imports/using of a WinForm – XYink Feb 08 '22 at 17:51
  • 3
    @XYink Don't assume. I've seen many times where a developer is convinced the compiler is wrong but I don't think I've yet seen a situation where they were correct – Caius Jard Feb 08 '22 at 17:52
  • @XYink That is why you were asked to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Sir Rufo Feb 08 '22 at 17:53
  • @SirRufo i said it, i cannot check my code right now. I just asked the question. I didn’t think that answers would come that fast. – XYink Feb 08 '22 at 17:54
  • @CaiusJard It's something the OP may want to experiment with (and check the Docs about its functionality). I cannot say whether there's something UI related there, but it's not part of the question. – Jimi Feb 08 '22 at 17:55
  • *I didn’t think that answers would come that fast* - think you might need to upgrade the question then! :D – Caius Jard Feb 08 '22 at 17:57
  • 2
    Hmm, that's a fairly terrible close duplicate @NicholasCarey – Caius Jard Feb 08 '22 at 17:59

1 Answers1

3

There's a long comment thread above, but hopefully an answer can simplify for future visitors...

Task.Delay is an asynchronous operation. (Whether or not it's internally marked as async I can't say for sure, but it's "awaitable".) You just need to await it:

public async Task waitThen()
{
    await Task.Delay(5000);
    checkForDone();
}

Note also that the method now returns a Task, not void. This makes your waitThen method also awaitable, so consuming code will need to await it if that code also wants to wait until the operation is complete before continuing.


but the normal c# Thread.Sleep(5000); doesn’t work in the Win Form

Sure it does, it always has. However, unless it's done on a separate thread explicitly then it will also freeze the UI for 5 seconds. Which "works" but is certainly not ideal. Relying on Task is generally the preferred approach.


Asides related to the comment thread above:

  • In order to use Task, you need using System.Threading.Tasks;
  • In order to use Thread, you need using System.Threading;
  • Method names in C# are traditionally capitalized: WaitThen
David
  • 208,112
  • 36
  • 198
  • 279