简体   繁体   English

在标签上显示特定时间的文本

[英]Show text on a label for a specific time

I want to show a text in a label for a particular time so I did a search on google and I found these two solutions : 我想在标签中显示特定时间的文本,所以我在Google上进行了搜索,发现了这两种解决方案:

The first solution is : 第一个解决方案是:

public void InfoLabel(string value)
{
    if (InvokeRequired)
    {
        this.Invoke(new Action<string>(InfoLabel), new object[] { value });
        return;
    }
    barStaticItem3.Caption = value;

    if (!String.IsNullOrEmpty(value))
    {
        System.Timers.Timer timer = 
                                new System.Timers.Timer(3000) { Enabled = true };
        timer.Elapsed += (sender, args) =>
        {
            this.InfoLabel(string.Empty);
            timer.Dispose();
        };
    }
}

The second solution : 第二种解决方案:

private void ShowTextForParticularTime(String caption)
{
    Timer t = new Timer { Interval = 5000, Enabled = true};
    t.Tick += (sender, args) => OnTimerEvent(sender, args, caption);  
}

private void OnTimerEvent(object sender, EventArgs e, String caption)
{
    barStaticItem3.Caption = caption;
}

Could you please tell me the deffrence between the two solutions, and why we use this symbole "=>" , also I understood nothing from this line : 您能否告诉我这两种解决方案之间的矛盾,以及为什么我们使用此符号“ =>”,所以我也从这行中一无所知:

if (InvokeRequired)
{
    this.Invoke(new Action<string>(InfoLabel), new object[] { value });
    return;
}

In WinForms and WPF, the UI can only be updated from the thread that created the control in question. 在WinForms和WPF中,只能从创建有问题的控件的线程中更新UI。 These two approach show two ways to update your UI from a different thread. 这两种方法显示了从不同线程更新UI的两种方法。

The first approach manually checks if the code is running on a different thread and, if it is, marshals the call to the UI thread. 第一种方法是手动检查代码是否在其他线程上运行,如果是,则将对UI线程的调用编组。

The second approach uses an event, leaving the details of marshaling to .NET 第二种方法使用事件,将封送处理的详细信息留给.NET

The symbol => represents a lamda expression . 符号=>表示lamda表达式 You can think of it much like a function pointer (though sometimes it is really something called an expression tree behind the scenes). 您可以将其视为函数指针(尽管有时确实是幕后的表达式树 )。 Essentially, it creates a variable that points to code that can be called by referencing that variable. 本质上,它创建一个变量,该变量指向可以通过引用该变量来调用的代码。

Either approach should work fine. 两种方法都可以正常工作。 Personally I prefer the second approach because it allows the framework to handle more of the plumbing work. 我个人更喜欢第二种方法,因为它允许框架处理更多的管道工作。

Okay, there is a good amount to explain here. 好的,这里有很多要解释的东西。

There is no major differences between the two options you have shown. 您显示的两个选项之间没有主要区别。 The reason they look different is because the first id declaring a delegate method (lambda expression) inside the public method, while the second is just creating an event handler. 它们看起来不同的原因是,第一个id在public方法内部声明了委托方法(lambda表达式),而第二个只是创建事件处理程序。 They do almost the exact same thing. 他们几乎做同样的事情。 Infact you can see that in the delegate method you have the tradition event handler prameters (object sender, EventArgs e). 实际上,您可以看到在委托方法中您具有传统的事件处理程序参数(对象发送器,EventArgs e)。 Personally I prefer the second solution because it looks cleaner to me. 我个人更喜欢第二种解决方案,因为它对我来说看起来更干净。

Invoke Required is used to handle threading. 需要调用用于处理线程。 In C# errors will be thrown if a thread that didn't create a visual object tries to alter the visual object. 在C#中,如果未创建可视对象的线程尝试更改可视对象,则会引发错误。 To get around this we make a call to the thread that created the visual object by calling "Invoke". 为了解决这个问题,我们通过调用“ Invoke”来调用创建可视对象的线程。 The "InvokeRequired" property just tells us if the current thread did not create the visual object. “ InvokeRequired”属性只是告诉我们当前线程是否未创建可视对象。 You should always use this when you are threading or making delegate methods (because you can't control the thread that runs them.) 在线程化或创建委托方法时,应始终使用此方法(因为您无法控制运行它们的线程)。

I hope this brief explanation helps. 我希望这个简短的解释会有所帮助。 Comment if it is unclear 如果不清楚,请发表评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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