old Software on a new computer

Christian little wood 1 Reputation point

I have an old (build in 2008 with VS2003) application software for an industrial application based on windows forms. Now our customer wants to extend some functions, but I can't run the software properly.
I have a lot of cross-thread operations errors:
System.InvalidOperationException: "Invalid cross-thread operation: The control btn002 was accessed from a different thread than the thread it was created on."
This is not only with btn002.
I could solve those errors with using Invoke delegates,

mbtnArr[i].btn.Invoke(new MethodInvoker(delegate ()


but some functions don't work now correctly.
Does anybody had this problem too and could it solve with an easy add of a library or something like that?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,869 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,604 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,381 Reputation points

    See if this works for you

    public static class ControlExtensions  
        /// <summary>  
        /// Determines if a control needs to be invoked to prevent a cross thread violation. This is a generic  
        /// extension method  
        /// </summary>  
        /// <typeparam name="T">Control</typeparam>  
        /// <param name="control">Control</param>  
        /// <param name="action">Predicate to run</param>  
        /// <example>  
        /// private void OnTimedEvent(object source, ElapsedEventArgs e)  
        /// {  
        ///     ElapsedTimerLabel.InvokeIfRequired(label =>  
        ///     {  
        ///         label.Text = $"{e.SignalTime}";  
        ///     });  
        /// }  
        /// </code>  
        /// </example>          
        public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : ISynchronizeInvoke  
            if (control.InvokeRequired)  
                control.Invoke(new Action(() => action(control)), null);  

    Usage where TODO is where you perform whatever action you want for mbtnArr[i].btn

    mbtnArr[i].btn.InvokeIfRequired(label =>  
    	// TODO  
    0 comments No comments

  2. Christian little wood 1 Reputation point

    Hello karenpayneoregon

    Thank you for your fast response. I tried this solution but it's still the same effect. When I debug step by step, it enters the control.InvokeRequired case and looses the pointer afterwards in the line:
    control.Invoke(new Action(() => action(control)), null);

    during this procedure, the mouse hourglass is tourning with no end on the application. So it looks for me, that he just do it the first time and afterwards don't get a time window to do it again.


  3. Lex Li (Microsoft) 5,237 Reputation points Microsoft Employee

    If you just want the program to quickly run as usual, follow https://stackoverflow.com/a/43890612/11182 to disable cross thread check. However, after that you need a lot of efforts to study the latest async coding best practices, such as using async/await and Tasks and getting rid of old things like BackgroundWorker.