Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows WPF 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);
 
            
        }
Posted 17-Apr-13 6:34am
klottim197
Edited 17-Apr-13 6:35am
v2
Comments
Irina Pykhova at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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

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



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100