Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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);
    }
}


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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.