3

OK...

Looks like WinForms and Threading in the same program is something that I can't master easily.

I'm trying to make a custom Numeric Up Down out of two picture boxes (used a buttons) and a text box. Yes, I can use the default NumericUpDown form, but I want to make a different looking one.

I have tried to do that with timers (System.Windows.Forms.Timer). The problem is that when one timer meets "Thread.Sleep(int)", the whole program "falls to sleep".

I tried with threads. Some types of threads can't control the UI. Then I tried this

private void declare_thread()
{
    //some work
    Thread delay = new Thread(new ThreadStart(delay0));
    delay.Start();
    //some more work
}
//other functions
private void delay0()
{
    //delay_for_500ms.WaitOne();
    this.Invoke((ThreadStart)delegate()
    {
    Thread.Sleep(500);
    if (is_mouse_down)
    timer1.Enabled = true;
    });
}

The result was the same as when I used timers only.

So, I want to make custom Numeric Up Down. But I can't get it right. I know I'm doin' it wrong. I want to make a thread that can control the UI and doesn't make the whole program pause when calling "Thread.Sleep(int)".

Please, give me answers for beginners. So far I haven't found a good answer that can show me the right way and it's easy to understand.

1
  • Is WPF an option for you? If so you would have a much easier time customizing the numeric up-down control and in executing "wait" tasks without blocking the UI.
    – bic
    Aug 26, 2011 at 17:59

4 Answers 4

8

You're looking for the wrong answer to the problem. There's no such thing as a thread which can access UI controls and sleep without blocking the UI. Only the UI thread can access UI controls safely, and if you make the UI thread sleep, your UI will be unresponsive.

I'm afraid you need to bite the bullet and learn how to perform the threading properly. Note that sleeping is almost always the wrong approach - especially in the UI thread. You ahven't really said what you're trying to do, but a timer is a good way of saying, "I want to perform some action at a later point in time." There are various different timers in .NET - which one you should use will depend on what you're trying to do.

Threading is one of those topics that you can't really shortcut - you should consider reading a thorough tutorial or a book which covers it in detail.

2

To answer your actual question, use this:

private void declare_thread()
{
    //some work
    Thread delay = new Thread(new ThreadStart(delay0));
    delay.Start();
    //some more work
}

//other functions
private void delay0()
{
    //delay_for_500ms.WaitOne();
    Thread.Sleep(500);
    this.Invoke((ThreadStart)delegate()
    {

    if (is_mouse_down)
    timer1.Enabled = true;
    });
}

Of course your whole mental model of proper, event driver programming is wrong. You'd fit in better with the old single-task DOS people 20 years ago.

If you want to wait half a second before doing something, set a timer for 500ms and exit your function. Let the timer tell you when it's done so you can do the second half:

// on the main thread

// la la la doing work
// wait 500ms
var tmr=new Timer{Interval=500};
tmr.Tick+=(s,e)=>{/*action .5sec later*/ btn.Enabled=false; tmr.Enabled=false;};
tmr.Enabled=true;

// and quit
return;
1

If you are willing to download the Async CTP (or wait for C# 5.0) you can use the new async and await keywords to make for a really elegant solution.1

public class YourForm : Form
{
  private async void UpPictureBox_Click(object sender, EventArgs args)
  {
    await Task.Delay(500);
    timer1.Enabled = is_mouse_down;
  }

  private async void DownPictureBox_Click(object sender, EventArgs args)
  {
    await Task.Delay(500);
    timer1.Enabled = is_mouse_down;
  }
}

The await keyword in the code above will yield control back to the message loop while the delay task is executing. Execution will pick back up where it left off upon completion of that task. This is but one among many reasons why the new keywords are so wicked cool.


1Note that the Async CTP uses TaskEx instead of Task.

3
  • Looks easy. I'll give it a try... As soon as I install VS 2010 SP1. This will take some time for me. Aug 29, 2011 at 14:08
  • This is not working. When I add the "async" word this error comes up "Cannot find Task-related types. Are you missing a reference to 'AsyncCtpLibrary.dll' ?". And when I add the line "await Task.Delay(500);" another error comes up :"The name 'Task' does not exist in the current context". Why? Is there something more? Aug 30, 2011 at 12:58
  • would be good to update this answer to take into account that TPL is now an official part of .NET proper. This really is the best, most-applicable answer to the question now, IMHO. Jul 9, 2020 at 7:01
-1
button.Click += delegate { BeginInvoke(new UiUpdate(delegate { Thread.Sleep(500); // Do Stuff After Sleep };)) };

private delegate void UiUpdate();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.