Backgroundworker错误DateTime [英] Backgroundworker error DateTime

查看:81
本文介绍了Backgroundworker错误DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,在Backgroundworker中有一些DateTime的问题。

这个的任何解决方案吗?

我真的需要DateTime。



此代码不起作用:



Hello, have some problem with DateTime in Backgroundworker.
Any solution for this one?
I really need DateTime.

This code don''t work:

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;

        DateTime? start = DateTimePicker1.Value;
        DateTime? end = DateTimePicker2.Value;
        DateTime now = DateTime.Now;
        if (now >= end)
        {
            System.Threading.Thread.Sleep(500);
        }

}





这个简单的代码有效:





This simple code works:

private void bw_DoWork(object sender, DoWorkEventArgs e)
{

            System.Threading.Thread.Sleep(500);


}

推荐答案

看来你正在交叉线程化应用程序。如果你希望其他线程能够与用户控件交互(甚至只是阅读它们),你需要使用委托。

你可以利用这个代码片段



代码片段

It would appear you are cross threading the application. You need to use delegates if you want other threads to be able to interact with user controls (even just reading them).
You could take advantage of this code snippet

Code Snippet
private void button1_Click(object sender, EventArgs e)
    {
      string[] A = {textBox1.Text,textBox2.Text};
      backgroundWorker1.RunWorkerAsync(A);
    }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
      string[] S = (string[]) e.Argument;
    }







(来源:http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/47b693a3 -1add-4fc7-8459-4232554c7131 [ ^ ])


这篇关于Backgroundworker错误DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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