Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7677339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:24:01+00:00 2026-05-31T17:24:01+00:00

I’m getting a ‘cross-thread operation not valid’ in one member but not in another

  • 0

I’m getting a ‘cross-thread operation not valid’ in one member but not in another and both belong to the same Form, the same UI thread I believe. Those members are uxServerGroup and uxServerListen. When I change the text on uxServerGroup.ValuesSecondary.Heading it works fine, I don’t get a cross-thread exception. But I do get one when I try to enable back the uxServerListen button. Why?

I should probably note that both of those components belong to Krypton Toolkit, they are not standard Windows Forms.

Here’s a sample of the code I have:

MainForm.cs:

public partial class MainForm : Form {
    public MainForm() {
        InitializeComponent();
        SomeClass.ObjectStateChanged += new ObjectStateEventHandler(SomeClass_ObjectStateChanged);
    }

    private void uxServerListen_Click(object sender, EventArgs e) {
        uxServerListen.Enabled = false;

        ClassHandler ch = ClassHandler();
        ch.Initialize();
    }

    private void SomeClass_ObjectStateChanged(Enum e1, Enum e2) {
        switch(e1) {
            case E1.TypeA:
                HandleTypeAObjectChanges(e1);
                break;
            case E1.TypeB:
                HandleTypeBObjectChanges(e2);
                break;
        }
    }

    private void HandleTypeAObjectChanges(Enum e2) {
        switch(e2) {
            case E2.ModeA:
                uxServerGroup.ValuesSecondary.Image = Resources.StatusSuccess16;
                break;
            case E2.ModeB:
                uxServerGroup.ValuesSecondary.Image = Resources.StatusFailure16;
                uxServerListen.Enabled = true;
                break;
        }
    }
}

SomeClass.cs:

public delegate void ObjectStateEventHandler(Enum e1, Enum e2);

public static class SomeClass {
    public static event ObjectStateEventHandler ObjectStateChanged;

    internal static E1 e1;

    internal static void ObjectStateChanged(Enum e2) {
        if(ConnectionStateChanged != null) {
            ConnectionStateChanged(e1, e2);
        }
    }
}

ClassHandler.cs:

public class ClassHandler {
    public ClassHandler() {
        // (...)
    }

    public void Initialize() {
        Thread thread = new Thread(new ThreadStart(SomeMethod));
        thread.Start();
    }

    private void SomeMethod() {
        SomeClass.ObjectStateChanged(E2.ModeB);
    }
}

After a lot of searching, I ended up fixing it like this:

private void SomeClass_ObjectStateChanged(Enum e1, Enum e2) {
        if(InvokeRequired) {
            Invoke(new MethodInvoker(() => {
                switch(e1) {
                    case E1.TypeA:
                        HandleTypeAObjectChanges(e2);
                        break;
                    case E1.TypeB:
                        HandleTypeBObjectChanges(e2);
                        break;
                }
            }));
        }
    }

As an additional question, can someone explain me this (the thing inside MethodInvoker):

new MethodInvoker(() => { /* (...) */ })
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:24:03+00:00Added an answer on May 31, 2026 at 5:24 pm

    I’m getting a ‘cross-thread operation not valid’ in one member but not in another

    Invalid cross-thread operations need to be specifically detected to trigger a usable exception. If there is no detection, you won’t get that exception. But be careful: don’t take that as permission to access the property from another thread, it’s still not usually allowed, and the few exceptions will be documented as thread-safe.

    As an additional question, can someone explain me this (the thing inside MethodInvoker):

    new MethodInvoker(() => { /* (...) */ })
    

    This creates an anonymous inline method, and when called, the /* (...) */ statements will be executed. A delegate is then created to make the method callable, and that’s what gets passed to Invoke. Usually, it can be easier to write it slightly more elaborately like so:

    MethodInvoker foo = () =>
    {
        ...
    };
    // no part of the ... above has run yet
    if (!control.InvokeRequired)
        foo(); // call foo directly
    else
        control.Invoke(foo); // let the UI thread call foo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.