Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Help Needed @ C#: Cross-thread Operation Not Valid
turbopowerdmaxst...
post Feb 22 2007, 11:42 AM
Post #1


Premium Member
Group Icon

Group: [HOSTED]
Posts: 371
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322



I have a class named as test which triggers an event using the Timer class, the code for which is given below.

CODE
using System;
using System.Timers;

namespace Test
{
    class test
    {
        public delegate void TestEventHandler();
        public event TestEventHandler TestEvent;

        protected Timer TestTimer = new Timer();

        public test()
        {
            TestTimer.Elapsed += new ElapsedEventHandler(Tick);
            TestTimer.Interval = 1000;
        }

        private void Tick(object source, ElapsedEventArgs e)
        {

            TestEvent();
        }

        public void Go()
        {
            TestTimer.Enabled = true;
        }
    }
}



The problem that I am facing with, is that the Event runs in a seperate thread and doesn't allow any kind of operation on the Form's controls. The code for the form is given below.

CODE
using System;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        test A = new test();
        public Form1()
        {
            InitializeComponent();
            A.TestEvent += new test.TestEventHandler(A_TestEvent);
        }

        void A_TestEvent()
        {
            this.Text = "Test Text";        // This is where the Exception "Cross Threaded Operation not valid" is generated
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            A.Go();
        }
    }
}

How do I invoke the event in the same thread as that of the form's?
Go to the top of the page
 
+Quote Post
ethergeek
post Mar 9 2007, 06:13 PM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 393
Joined: 9-March 07
From: Tucson, AZ
Member No.: 20,794



It's a new design change either with VS2005 or with the .NET Framework 2.0 to eliminate alot of concurrency bugs. When the exception handler pops up in VS, click the "more info" link...it will take you to an MSDN article explaining how to properly use delegates to do what you want to do in a thread-safe manner.
Go to the top of the page
 
+Quote Post
iGuest
post Apr 6 2008, 09:21 PM
Post #3


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



Cross Thread Operations
Help Needed @ C#: Cross-thread Operation Not Valid

Yes , you cannot access a windows forms control or a windows forms component from a different thread. What you need to do in order to access the form controls is to invoke the control using a delegate with the help of control.BeginInvoke(). I have posted an article on the issue with sample code on my blog at http://asadsiddiqi.Wordpress.Com/2007/12/24/responsive-user-interfaces-with-ui-threads/
Hope this helps .

Thanks

-reply by Muhammad Asad Siddiqi
Go to the top of the page
 
+Quote Post
magiccode9
post Jun 15 2008, 10:11 PM
Post #4


Member [ Level 2 ]
Group Icon

Group: [HOSTED]
Posts: 55
Joined: 7-November 05
Member No.: 9,489



I have forgotten how the whole sample did.
Just remember that you have to use
Control.InvokeRequired to check that
if you are not on the right thread.

If so, skill to the next method call and try
to update the control UI.

Wish this help

Eric
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Jun 22 2008, 02:28 AM
Post #5


Premium Member
Group Icon

Group: [HOSTED]
Posts: 371
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322



Here I am posting the full solution to my own problem. Maybe this will help out some beginners. And no, it didn't take me a year and a half to figure this one out, thanks to MSDN. Its just that I didn't bother to look into this once I had the answer. Anyways, here goes it.

As said by Muhammad, the trick is in using the Control.BeginInvoke (or Control.Invoke) method to force the method to be executed in the same thread as that of the control. The difference between Invoke and BeginInvoke is that the former is synchronous while the latter is asynchronous. Using BeginInvoke makes more sense as the whole idea is to create efficient multi-threaded application. BeginInvoke starts a new thread passing along a delegate object of the method to be invoked and the parameters to be passed to it.

In this case, the BeginInvoke method of the owner Form has to be called from the test class. So, we include an ISynchronizeInvoke parameter in the constructor of the class which is then stored in a member variable. The ISynchronizeInvoke Interface must be implemented in-order to execute a delegate asynchronously. Thankfully, this has been done in the System.Windows.Forms.Control class, so it doesn't require any work on our part.

CODE
protected ISynchronizeInvoke SyncObject;

public test(ISynchronizeInvoke SyncObject)
{
    this.SyncObject = SyncObject;
    TestTimer.Elapsed += new ElapsedEventHandler(Tick);
    TestTimer.Interval = 1000;
}


We add a new delegate with the same signature as that of the method Tick (the event handler for the System.Timers.Timer's elapsed event).

CODE
protected delegate void TickDelegate(object source, ElapsedEventArgs e);


The Tick method makes sure that it is running in the same thread as that of the Synchronizing object passed to the class's constructor using the Control.InvokeRequired property. If it is not, it calls the BeginInvoke method of the SyncObject passing along a delegate to itself and an object array of it's parameters. No further work is done in this call to the Tick method. After a while, the Tick method is invoked again, only this time on the owner form's thread. The InvokeRequired property returns false and the actual work of the method is done.

CODE
private void Tick(object source, ElapsedEventArgs e)
{
    if (SyncObject.InvokeRequired)
        SyncObject.BeginInvoke(new TickDelegate(Tick), new object[] { source, e });
    else
        TestEvent();
}


Only one change needs to be made in the Form class - a reference to itself must be passed to the contstructor of the test class. For this, the instantiation of the object is now done inside the constructor (this keyword is only valid inside a non-static property, method, or constructor).

CODE
test A;
public Form1()
{
    InitializeComponent();
    A = new test(this);
    A.TestEvent += new test.TestEventHandler(A_TestEvent);
}


Full code for the two classes:-

Test class

CODE
using System;
using System.Timers;
using System.ComponentModel;

namespace Test
{
    class test
    {
        public delegate void TestEventHandler();
        public event TestEventHandler TestEvent;

        protected Timer TestTimer = new Timer();
        protected ISynchronizeInvoke SyncObject;

        public test(ISynchronizeInvoke SyncObject)
        {
            this.SyncObject = SyncObject;
            TestTimer.Elapsed += new ElapsedEventHandler(Tick);
            TestTimer.Interval = 1000;
        }

        protected delegate void TickDelegate(object source, ElapsedEventArgs e);
        private void Tick(object source, ElapsedEventArgs e)
        {
            if (SyncObject.InvokeRequired)
                SyncObject.BeginInvoke(new TickDelegate(Tick), new object[] { source, e });
            else
                TestEvent();
        }

        public void Go()
        {
            TestTimer.Enabled = true;
        }
    }
}


Form Class

CODE
using System;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        test A;
        public Form1()
        {
            InitializeComponent();
            A = new test(this);
            A.TestEvent += new test.TestEventHandler(A_TestEvent);
        }

        void A_TestEvent()
        {
            this.Text = "Test Text";        // This is where the Exception "Cross Threaded Operation not valid" used to be generated
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            A.Go();
        }
    }
}


This post has been edited by turbopowerdmaxsteel: Jun 22 2008, 02:34 AM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Help With Some Php Or Maybe Asp(5)
  2. Mobile Phone Ringtone Converter(7)
  3. Help Needed To Create Login Script With Perl/cgi(21)
  4. Help Needed: Monitor Out Of Timing(6)
  5. Is A Sound Card Absolutely Needed ?(18)
  6. Advice On MP3 Player Needed(22)
  7. Help Needed In Setting Up AdSense In IPB(7)
  8. Sprites Needed.(2)
  9. Help Needed To Mke Phpbb Sub-forum(4)
  10. Links: Free Stuffs Needed For Webmasters!(15)
  11. Help Needed To Create Windows Startup Script!(4)
  12. Juggling An Iframe Box With Xhtml Sites(8)
  13. Photographers And Artists Needed(2)
  14. Part-passworded, Cross-os External Hd(5)
  15. Basics Of Php For Beginners - Suggestion(5)
  1. Coders Needed To Help On A Bulletin Board System (BBS)(4)
  2. Skills Needed(1)
  3. Setting Up Cvs On A Sun Unix System(4)
  4. Warning: Mysql_result(): Supplied Argument Is Not A Valid Mysql Result Resource In ...(4)
  5. Javascript Help Needed : Alert(z) Works Fine But Document.write Not(2)
  6. Cboard!(0)
  7. Help Needed(1)
  8. Suggestions Needed For Latest Ycc Bot Maker Breakdown(5)
  9. Free Signature Thread(3)
  10. Infomation Needed To Add Ram, Gpu, Etc?(19)
  11. Space Needed For Database(10)
  12. Help Needed(1)
  13. Xml Needed?(4)


 



- Lo-Fi Version Time is now: 5th September 2008 - 05:11 AM