C#Winforms:可以控制UI并可以“睡眠"的线程.无需暂停整个程序.如何? [英] C# Winforms: A thread that can control the UI and can "sleep" without pausing the whole program. How?

查看:47
本文介绍了C#Winforms:可以控制UI并可以“睡眠"的线程.无需暂停整个程序.如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好...

在同一程序中看起来像WinForms和Threading是我不容易掌握的东西.

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

我试图用两个图片框(使用一个按钮)和一个文本框制作一个自定义的Numeric Up Down.是的,我可以使用默认的NumericUpDown表单,但是我想制作一个外观不同的表单.

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.

我尝试使用计时器(System.Windows.Forms.Timer)来做到这一点.问题在于,当一个计时器遇到"Thread.Sleep(int)"时,整个程序将进入睡眠状态".

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".

我尝试使用线程.某些类型的线程无法控制UI.然后我尝试了

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.

因此,我想制作自定义的数字上下.但是我做错了.我知道我做错了.我想创建一个可以控制UI的线程,并且在调用"Thread.Sleep(int)"时不会使整个程序暂停.

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.

推荐答案

您正在寻找该问题的错误答案.没有线程可以访问UI控件而不会阻塞UI的线程.只有UI线程才能安全地访问UI控件,如果使UI线程处于睡眠状态,则UI将无响应.

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.

恐怕您需要硬着头皮,学习如何正确执行穿线.请注意,睡眠几乎总是错误的方法-尤其是在UI线程中.您并没有真正说出您要做什么,但是使用计时器是一种很好的方式,我想在以后的某个时间执行一些操作"..NET中有各种不同的计时器-您应该使用哪一个取决于您要做什么.

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.

这篇关于C#Winforms:可以控制UI并可以“睡眠"的线程.无需暂停整个程序.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆