Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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
 
Old February 18th, 2008, 01:44 AM
Authorized User
 
Join Date: Jul 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Threading with Background worker

Hello.

I'mt rying to tun a process in the back gorund using the background worker, but when the thread is comlpete it does not run the "RunWorkerCompleted" function do i need to call/invoke it somehow or should it run when the thread is complete?

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
        
            BackgroundWorker bw = new BackgroundWorker();

            int arg = (int)e.Argument;

            e.Result = Engine(bw, arg, e);

            if (bw.CancellationPending)
            {
                e.Cancel = true;
            }
        }

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                MessageBox.Show("Operation was canceled");
            }
            else if (e.Error != null)
            {
                string msg = string.Format("An error occurred: {0}", e.Error.Message);
                MessageBox.Show(msg);
            }
            else
            {
                string msg = string.Format("Result = {0}", e.Result);
            }
        }


in addition if i try to call the progressChanged event the progress bar will not update, what em i doing wrong?

THANKS!!
 
Old February 18th, 2008, 03:23 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Why are you creating a new BackgroundWorker in the DoWork event?

Try:

BackgroundWorker bw = (BackgroundWorker)sender;

/- Sam Judson : Wrox Technical Editor -/
 
Old February 18th, 2008, 12:22 PM
Authorized User
 
Join Date: Jul 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have changed that line of code and i get no update in the behavior of the app any more ideas?

 
Old February 18th, 2008, 12:46 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

a) What is in your ProcessChanged event?

b) the line "string msg = string.Format("Result = {0}", e.Result);" doesn't display anything - have you tried setting a break point on this line to make sure it is getting to this line.

/- Sam Judson : Wrox Technical Editor -/
 
Old February 19th, 2008, 01:34 AM
Authorized User
 
Join Date: Jul 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I set a break point in the function running in the background, whent he function is complete the application does not run the backgroundWorker1_RunWorkerCompleted function. it never gets there.

 
Old February 19th, 2008, 05:19 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Then I suspect that an exception is occurring in your code. Try putting a try/catch around the call to the Engine method.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Worker Threads dill BOOK: Professional Microsoft Robotics Studio ISBN: 978-0-470-14107-6 4 June 23rd, 2008 08:42 PM
threading swifty_programmer ASP.NET 2.0 Basics 12 May 19th, 2007 06:10 AM
how Asp.Net worker process works hhkris4u ASP.NET 1.0 and 1.1 Professional 1 January 7th, 2007 06:20 PM
How to Pause or sleep the background worker thread mks033 C# 2005 0 May 3rd, 2006 07:03 AM





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