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
| 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
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 13th, 2006, 09:17 AM
Registered User
Join Date: Dec 2006
Location: Paris, , France.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Richw71
Default Cross-thread operation not valid


I have a form that, on clicking a button, kicks off a thread. That thread raises occasional events that I want the original form to trap and show information about.

Everything seems perfectly logical to me, but at run-time I get an error: Cross-thread operation not valid.

The solutions that I have seen work for the case where the code to update the form is contained within the thread itself. In my case, though, I want the thread class to be reusable without modification and so do not want to code references to a particular form inside the thread.

Here is the code, in the form, that uses the thread:

// This is my UI class: the standard VS2005 created "Form1"
// The form contains a button: button1, and a label: label1

/*  Info:
 *  When the button1 is clicked a thread is created that 
 *  performs a long calculation.   From time-to-time during 
 *  the calculation an event - ChangedResult - is raised and 
 *  this event passes back the current result to the calling 
 *  form for display in label1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestCrossThreadApp1
    public partial class Form1 : Form

        // Default constructor... no surprises
        public Form1()

        // The button click event that creates an instance of a "calc" thread
        // an event handling method, onChangedResult, is setup to received
        // the ChangedResult event from the Calc1 thread.
        private void button1_Click(object sender, EventArgs e)
            MyCalcThread Calc1 = new MyCalcThread();
            Calc1.ChangedResult += onChangedResult;
        }// button1_click

        // The onChangedResult method is called by delegation
        private void onChangedResult(object sender, CalcEventArgs CEA)
            string s = Convert.ToString(CEA.Result);
            label1.Text = s;  //<-------- This is where the ERROR occurs!
        }// onChangedResult

    }// class Form1

}// Namespace

The following is the code for the thread object and it's eventargs object:

using System;
using System.Threading;

namespace TestCrossThreadApp1
    // Define the delegate for the ChangedResult event
    public delegate void ChangedResultHandler(object sender, CalcEventArgs CEA);

    // Event Arguments
    public class CalcEventArgs : EventArgs
        private int result;

        // Constructor
        public CalcEventArgs(int Result)
            this.result = Result;
        } // Constructor

        // Get Parameter
        public int Result
                return result;
            } // Get
        } // Get Parameter
    }//class CalcEventArgs

    // Declare a class that encapsulates the threaded calculations
    public class MyCalcThread
        // Define an event that will be fired when the calculation result is changed significantly
        public event ChangedResultHandler ChangedResult;

        // This method creates an instance of the event args and raises the ChangedResult event
        public void RaiseResultChangedEvent(int Result)
            CalcEventArgs CEA = new CalcEventArgs(Result);
            ChangedResult(this, CEA);
        }// RaiseResultChangedEvent

        // Public class that starts the calculation in it's own thread
        public void StartCalculation()
            Thread CalculationThread = new Thread(new ThreadStart(CalcThread));
        }// StartCalculation

        // This is a mundane "calculation"
        public void CalcThread()
            int i = 0;

            while (++i < 10000000)
                // Raise the ChangedEvent method via the RaiseResultChangedEvent wrapper
                switch (i)
                    case 1:
                    case 100000:
                    case 500000:
                    case 1000000:
                } // switch
            }// While
        }// CalcThread

    }// Class MyCalcThread

}// Namespace

This is very frustrating, so any help that you could offer would be appreciated!

- Rich

Reply With Quote

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
cross thread exception YoungLuke C# 1 March 23rd, 2008 06:46 PM
cross thread exception with windows live API YoungLuke C# 2008 aka C# 3.0 1 March 23rd, 2008 01:43 PM
Thread Pool bhavana General .NET 0 October 23rd, 2007 06:15 AM
Which Thread to use and How peace2007 ASP.NET 1.0 and 1.1 Professional 0 September 23rd, 2007 03:24 AM
cross thread Error angelboy C# 2005 1 July 28th, 2007 04:35 AM

All times are GMT -4. The time now is 06:27 AM.

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