Click here to Skip to main content
13,089,207 members (100,351 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
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)

This simple code works:

        private void bw_DoWork(object sender, DoWorkEventArgs e)
Posted 17-Apr-13 5:34am
Updated 17-Apr-13 5:35am
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

Rate this: bad
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};
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      string[] S = (string[]) e.Argument;


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2017
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