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

  • Home
  • SEARCH
  • 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 1092675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:43:10+00:00 2026-05-16T23:43:10+00:00

I am work with SharpPcap library. I have following code: private void button1_Click(object sender,

  • 0

I am work with SharpPcap library. I have following code:

private void button1_Click(object sender, EventArgs e)
    {
        LivePcapDevice device = deviceList[cbDeviceList.SelectedIndex];
        device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);
        device.Open(DeviceMode.Promiscuous, 1000);
        device.StartCapture();
        btStop.Enabled = true;
        btStartCapture.Enabled = false;
    }

void device_OnPacketArrival(object sender, CaptureEventArgs e)
    {         
        dgvPacketInfo.Rows.Add(e.Packet.Data, e.Packet.Timeval,
                                e.Packet.LinkLayerType);          
    }

I’ve got an exception is thrown:InvalidOperationException with the message, “Control dgvPacketInfo accessed from a thread other than the thread it was created on.” I read this.
And rewrite my code like this:

private delegate void GetPacketInfoCallback(CaptureEventArgs packet);

void device_OnPacketArrival(object sender, CaptureEventArgs e)
    {
        Thread newThread = new Thread(delegate()
                                          {
                                              GetPacketInfoCallback getPacketInfoCallback = new GetPacketInfoCallback(PrintIntoGridView);
                                              this.Invoke(getPacketInfoCallback, new object[] {e});
                                          });            
    }

private void PrintIntoGridView(CaptureEventArgs captureEventArgs)
    {
        dgvPacketInfo.Rows.Add("1", "2", "3"); // simple for test
        dgvPacketInfo.Rows.Add(captureEventArgs.Packet.Data, captureEventArgs.Packet.Timeval,
                               captureEventArgs.Packet.LinkLayerType);
    }

But method PrintIntoGridView not called. What’s problem?
PS. Sorry for my English.

  • 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-16T23:43:10+00:00Added an answer on May 16, 2026 at 11:43 pm

    I guess the device_OnPacketArrival is already executed on a different thread from the GUI thread, so spawning yet another thread wouldn’t be necessary. Try this:

    void device_OnPacketArrival(object sender, CaptureEventArgs e)
    {
        if (InvokeRequired)
        {
            // If this is executing on a different thread than the GUI thread
            var getPacketInfoCallback = new GetPacketInfoCallback(PrintIntoGridView);
            Invoke(getPacketInfoCallback, new object[] { e });
        } 
        else 
        {
            // This is the GUI thread => 
            // you can safely modify GUI controls
            PrintIntoGridView(e);
        }
    }
    
    private void PrintIntoGridView(CaptureEventArgs captureEventArgs)
    {
        dgvPacketInfo.Rows.Add("1", "2", "3"); // simple for test
        dgvPacketInfo.Rows.Add(
            captureEventArgs.Packet.Data, 
            captureEventArgs.Packet.Timeval,
            captureEventArgs.Packet.LinkLayerType
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Work on this small test application to learn threading/locking. I have the following code,
I work in Ruby, and have to create a single regexp for the following
Work on C# Desktop project,I have an account on paypal ,From my desktop application
work on asp.net vs 05 C#.Master page header contain the bellow code <script type=text/javascript
Work on asp.net vs05. I have three type of value Like:IsDesign,IsPrinting,IsInstall they are bit
Work on the following website: http://cetcolor.com The masthead graphic with the Read About It
At work I have two XP PCs. On one, I can click with the
I work with a android device which use a WCF web service. I managed
Work on ASP.NET 2.0 C# on the web. In my site, I have three
Work on C# linq.I have a list to list .From this list I want

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.