Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello, have some problem with DateTime in Backgroundworker.
Any solution for this one?
I really need DateTime.

This code don't work:

C#
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:

C#
private void bw_DoWork(object sender, DoWorkEventArgs e)
{

            System.Threading.Thread.Sleep(500);


}
Posted
Updated 17-Apr-13 5:35am
v2
Comments
Irina Pykhova 17-Apr-13 11:52am    
look at answer about cross-threading, you should correct your code. And perhaps you should check whether end.HasValue is true and then compare with end.Value, not with end

1 solution

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
C#
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;
    }



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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900