Click here to Skip to main content
15,881,559 members
Articles / Desktop Programming / Windows Forms

Getting Around InvokeRequired Without Copy and Paste

Rate me:
Please Sign up or sign in to vote.
4.93/5 (14 votes)
17 Aug 20072 min read 119.9K   749   53  
Instead of copying and pasting the same if(InvokeRequired) logic in every multithreaded function, use attributes to make code cleaner, its centralize logic and make it self documenting.
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using NUnit.Framework;

using Osherove.SimpleInterception;
using System.Diagnostics;
using System.Windows.Forms;

namespace AOP.UnitTest
{
    [TestFixture]
    public class AOPTest
    {
        bool InvokeRequired = false;

        public void TestSetup()
        {
            //init var so we know other tests aren't affecting it
            InvokeRequired = false;

            Assert.IsFalse(InvokeRequired);
        }

        void form_InvokeRequiredEvent(bool isInvokeRequired)
        {
            //record whether or not the invoke was required
            InvokeRequired = isInvokeRequired;
        }

        [Test]
        public void TestNormalFormCrossThreadedProblem() //this simulates what happens on a winform app when functionality is called in a different thread
        {
            TestSetup();

            //create and launch form which is set to do a cross threaded operation for testing our AOP method of easily getting around the cross threaded UI issue
            AOP_TestForm form = new AOP_TestForm(AOP_TestForm.LoadTypeEnum.Bad);
            form.InvokeRequiredEvent += new AOP_TestForm.InvokeRequiredEventType(form_InvokeRequiredEvent);
            Application.Run(form); //run the app, message pump, all that goodness

            //this reproduces the original problem, invoke is required
            Assert.IsTrue(InvokeRequired);
        }

        [Test]
        public void TestFormCrossThreadedProblemFixManual() //this simulates what happens on a winform app when functionality is called in a different thread
        {
            TestSetup();

            //create and launch form which is set to do a cross threaded operation for testing our AOP method of easily getting around the cross threaded UI issue
            AOP_TestForm form = new AOP_TestForm(AOP_TestForm.LoadTypeEnum.GoodManual);
            form.InvokeRequiredEvent += new AOP_TestForm.InvokeRequiredEventType(form_InvokeRequiredEvent);
            Application.Run(form); //run the app, message pump, all that goodness

            //the above should fix it, invoke should not be required
            Assert.IsFalse(InvokeRequired);
        }

        [Test]
        public void TestFormCrossThreadedProblemFixAOP() //this simulates what happens on a winform app when functionality is called in a different thread
        {
            TestSetup();

            //create and launch form which is set to do a cross threaded operation for testing our AOP method of easily getting around the cross threaded UI issue
            AOP_TestForm form = AOPFactory.Create<AOP_TestForm>();
            form.InvokeRequiredEvent += new AOP_TestForm.InvokeRequiredEventType(form_InvokeRequiredEvent);
            Application.Run(form); //run the app, message pump, all that goodness

            //the above should fix it, invoke should not be required
            Assert.IsFalse(InvokeRequired);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

Comments and Discussions