Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# winform c中的多线程_C#_Datetime - Fatal编程技术网

C# winform c中的多线程

C# winform c中的多线程,c#,datetime,C#,Datetime,我想知道我已经在表单中定义了一个函数 例如:动态显示正在运行的当前日期时间 它在该窗体上显示当前系统时间 是否可以调用相同的函数而不在每个窗体上创建新函数 请帮助解决方案1:您不需要为此编写函数: Label1.Text = DateTime.Now.ToString(); 解决方案2:但如果您想使用函数访问它,请创建一个静态函数 public static class Utility { public static string DisplayDateTime() {

我想知道我已经在表单中定义了一个函数

例如:动态显示正在运行的当前日期时间

它在该窗体上显示当前系统时间

是否可以调用相同的函数而不在每个窗体上创建新函数

请帮助解决方案1:您不需要为此编写函数:

Label1.Text = DateTime.Now.ToString();
解决方案2:但如果您想使用函数访问它,请创建一个静态函数

public static class Utility
{
    public static string DisplayDateTime()
    {
         return DateTime.Now.ToString();
    }
}
在任何需要的地方调用上述函数,如下所示:

 Label1.Text = Utility.DisplayDateTime();
解决方案3:如果要每秒钟更改一次日期时间,请尝试以下操作:

    public static class Utility
    {
        public static string DisplayDateTime()
        {
             return DateTime.Now.ToString();
        }
    }

    System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
    timer1.Interval=1000;//one second
    timer1.Tick += new System.EventHandler(timer1_Tick);
    timer1.Start();

    private void timer1_Tick(object sender, EventArgs e)
    {
         //do whatever you want 
         Label1.Text = Utility.DisplayDateTime();
    }

可以对表单使用继承。但这和线程有什么关系?对不起,我不明白你的问题。你想打电话给DateTime吗?现在在每一张表格上打电话给一个标签什么的?是的,我想在每一张表格上打电话给它form@user3327117:您可以使用静态功能,检查下面的答案,但这与线程有什么关系?问题完全不清楚。我可以用我想打电话的任何形式打电话给职能部门?@user3327117:对不起,我没听清楚。你能解释一下吗?我想调用datetime函数,它在我的project@user3327117:如果要更改每秒的datetime值,请检查我的解决方案3