Hi,

I have three functions, a(), b() and c().

In function a() I need to call function b() and then after a 2 second delay call function c().

However, I can't use Thread.sleep(2000) as I need to be able to keep clicking on other buttons.

I think I need to use Threads a bit more, but I can't see how to delay the thread from starting
Thanks for any and all help.

Andrew.

Dani AI

Generated

Short recap: wanted to call b(), then call c() after a 2‑second pause without freezing the UI. and suggested worker threads and timers — both valid — but a simple, modern, non‑blocking pattern is to use async/await so the UI thread stays free and the delay is cheap and cancellable.

A minimal example (suitable for .NET 4.5+):

private async void A_Click(object sender, EventArgs e)  // event handler only
{
    B();                     // quick UI work or start a background operation
    await Task.Delay(2000);  // non-blocking 2s pause
    C();                     // runs after the delay on the UI thread
}

Notes and tips:

  • Use async void only for event handlers. For reusable methods prefer async Task.
  • If B() or C() do CPU-heavy work, run that part with Task.Run(...) so the UI stays responsive, and marshal any UI updates back to the UI thread.
  • Add a CancellationTokenSource when you need to cancel a pending delay (useful if the user can interrupt the action).
  • Timers are still appropriate when you need repeated ticks or higher timing control; remember there are UI-thread timers and thread-pool timers and each has different threading/Invoke implications.

This keeps things simple and readable compared with manually creating threads or keeping a sleeping thread around. It also makes cancellation and error handling straightforward — follow these patterns when adapting the suggestions from and . Thanks to for closing the loop on the original thread.

Recommended Answers

All 7 Replies

When you create a thread you create it with a callback delegate and when you want to start it you call thread.Start() with an optional object as a parameter for information you want to pass to the thread.

If this operation is really as simple as you have indicated it is then you could use a BackgroundWorker to execute in the background. It supports reporting progress and a finished event. You can drop one on your form from the toolbox and wire up the events to call a() b() and c() and call Thread.Sleep(2000); inside of it since it is not running on the thread that paints your GUI, so you can keep clicking buttons.

Background worker! Brilliant!

I have just (before I read your post) managed to do it with a thread, starting a new thread and having Thread.sleep(1000) as the first line of the function called for the thread.

Which is "better"? I don't understand the differences between backgroundworker and a thread.

Thanks again.

Andrew.

A backgroundworker uses a thread... its really just a "drag and drop" version of a simple thread. Unless you're writing very processor intensive code then you don't need to worry about the overhead difference. If i'm working on a form and doing something simple I usually pick the background worker

Thanks. This worked brilliantly.

I'm glad you got it working

Please mark this thread as solved if you have found an answer to your original question and good luck!

I know this has been handled... but after reading through, why go so complicated? why not just use a timer control?

seems odd to create a new thread for the sole purpose of sleeping it.

mark as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.