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 7040725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:57:09+00:00 2026-05-28T01:57:09+00:00

I am trying to listen on a device and when got a message from

  • 0

I am trying to listen on a device and when got a message from that showing it on a datagridview the first but getting the message from the device works perfectly, but the problem is that when I want to set the DGV content I got this exception Cross-thread operation not valid, and I have read this related topic.
but none of them was helpful since there weren’t on a binding DGV.
here is my code:
1. first I have a message class which is binded to the DGV,

public class Msg_log
{
    public Msg_log()
    {
        this.Event = null;
        this.Operator = null;
    }
    public string Event { get; set; }
    public string Operator { get; set; }
}

and here is how I create another thread in the loadform event:

newThread = new System.Threading.Thread(this.Event_Listener);
        newThread.Start();   

and in the Event_Listener function

                x.Add(message);
                MsgDGV.DataSource = null;
                MsgDGV.DataSource = x;
                MsgDGV.Refresh();

the message object is like this:

Msg_log message = new Msg_log();

and its Event and Operator variables of message have been set correctly, and I put the MSG.DataSource = null, since I want to update my DGV after the new message is comming (Actually this was my idea and if there is any better way for that I would appreciate that) and that’s the line I got the exeption:Cross-thread operation not valid. In the other posts I found out that I should use Invoke method but I don’t know how to call Msg_DGV.Invoke(??,???); I don’t know what should I pass to that to get the right result…
Cheers,

  • 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-28T01:57:09+00:00Added an answer on May 28, 2026 at 1:57 am

    You do want to do the same as that post you linked to, just change your control to use a DataGridView. C# Thread issues

    Msg_DGV.Invoke(new Action( () => Msg_DGV.DataSource = null ) );
    

    A more full blown example from here that doesn’t use Actions.

    // The declaration of the textbox.
    private TextBox m_TextBox;
    
    // Updates the textbox text.
    private void UpdateText(string text)
    {
      // Set the textbox text.
      m_TextBox.Text = text;
    }
    
    //Now, create a delegate that has the same signature as the method that was previously defined:
    public delegate void UpdateTextCallback(string text);
    
    //In your thread, you can call the Invoke method on m_TextBox, 
    //passing the delegate to call, as well as the parameters.
    m_TextBox.Invoke(new UpdateTextCallback(this.UpdateText), 
                new object[]{”Text generated on non-UI thread.”});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to listen tab-in tab-out action for my swing gui that is
I'm trying to find the correct event to listen for that will ensure that
I'm trying to setup application tests to run on my development device but following
I am trying to listen on two devices with libpcap but I still cant
I'm trying to listen for changes in a XML structure using Javascript. I've got
I am trying to build a web interface for an embedded device, that will
I am trying to create an application that reads keyboard input from a HID.
I've been trying to listen for changes in the status bar height – such
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
I'm trying to work with sockets and I have such problem In code example:

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.