Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 1st, 2007, 10:25 AM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with Timer and BackgroundWorker

Hi, After starting below code, the output I got is:
-------------
day
minute
day
minute,minute
day
minute,minute,minute
--------------
It should be:
--------------
day
minute,
day
minute
day
minute
--------------
I don`t knowo why the backgroud worker starts more then 1 time on each steps, is Background worker cancelletion working right?
Please help.


Code:
        private void Form1_Load(object sender, EventArgs e)
        {


            timer1.Interval = 3000;
            timer1.Tick += new EventHandler(DayTicker);
            timer1.Start();
        }

        public void DayTicker(object sender, EventArgs e)
        {
            timer1.Stop();
            if (backgroundWorker1.IsBusy) backgroundWorker1.CancelAsync();
            if (backgroundWorker1.CancellationPending) return;

            richTextBox1.Text += "\n day   \n";

            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(bw_DoWork_day);
            backgroundWorker1.RunWorkerAsync("test");
            backgroundWorker1.RunWorkerCompleted += bw_workerday_coplete;

        }

        public void bw_DoWork_day(object sender, DoWorkEventArgs e)
        {
            string w = "minute";
            e.Result = 1;

            e.Cancel = true;

            if (InvokeRequired)
                Invoke(new Change(OnChange), w);
        }

        public void bw_workerday_coplete(object sender, RunWorkerCompletedEventArgs e)
        {
           if (e.Error != null)
                MessageBox.Show("Worker exception: " + e.Error.ToString());
            else if (e.Cancelled)
            {
                timer1.Tick += new EventHandler(DayTicker);
                timer1.Start();
            }
         }

        private void OnChange(string w)
        {
            richTextBox1.Text += w + ",";
            Application.DoEvents();
        }

        private delegate void Change(string w);
    }
}
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Countdown timer DennyLoi Javascript How-To 2 November 27th, 2007 08:01 AM
Timer Dhanapal ADO.NET 2 April 16th, 2007 04:58 AM
Timer prasanta2expert Access VBA 1 November 8th, 2006 09:28 AM
BackgroundWorker phenom BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 5 May 1st, 2006 05:28 AM
timer amerei ASP.NET 1.0 and 1.1 Professional 0 October 15th, 2005 10:49 PM



All times are GMT -4. The time now is 11:30 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.