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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:08:51+00:00 2026-05-14T01:08:51+00:00

Greetings, I have a method to capture packets. After capturing I want to add

  • 0

Greetings,

I have a method to capture packets. After capturing I want to add each packet as a row in listview in real time so as soon as I capture packet I want to add it to the list view.

The problem that when I use items.Add() I will get overload argument error.
Please advise!!

Here is my code:

private void packetCapturingThreadMethod()
{

        Packet packet = null;
       int countOfPacketCaptures = 0;
        while ((packet = device.GetNextPacket()) != null)
            {

            packet = device.GetNextPacket();
            if (packet is TCPPacket)
                {
                TCPPacket tcp = (TCPPacket)packet;
                myPacket tempPacket = new myPacket();

                tempPacket.packetType = "TCP";
                tempPacket.sourceAddress = Convert.ToString(tcp.SourceAddress);
                tempPacket.destinationAddress = Convert.ToString(tcp.DestinationAddress);
                tempPacket.sourcePort = Convert.ToString(tcp.SourcePort);
                tempPacket.destinationPort = Convert.ToString(tcp.DestinationPort);
                tempPacket.packetMessage = Convert.ToString(tcp.Data);
                packetsList.Add(tempPacket);


                string[] row = { packetsList[countOfPacketCaptures].packetType, packetsList[countOfPacketCaptures].sourceAddress, packetsList[countOfPacketCaptures].destinationAddress, packetsList[countOfPacketCaptures].sourcePort, packetsList[countOfPacketCaptures].destinationPort, packetsList[countOfPacketCaptures].packetMessage };
                try {


                    listView1.Items.Add(packetsList[countOfPacketCaptures].packetType, packetsList[countOfPacketCaptures].sourceAddress, packetsList[countOfPacketCaptures].destinationAddress, packetsList[countOfPacketCaptures].sourcePort, packetsList[countOfPacketCaptures].destinationPort, packetsList[countOfPacketCaptures].packetMessage)

                    ; countOfPacketCaptures++;
                lblCapturesLabels.Text = Convert.ToString(countOfPacketCaptures);}
                catch (Exception e) { }

                }
            else if (packet is UDPPacket)
                {

                UDPPacket udp = (UDPPacket)packet;


                myPacket tempPacket = new myPacket();

                tempPacket.packetType = "UDP";
                tempPacket.sourceAddress = Convert.ToString(udp.SourceAddress);
                tempPacket.destinationAddress = Convert.ToString(udp.DestinationAddress);
                tempPacket.sourcePort = Convert.ToString(udp.SourcePort);
                tempPacket.destinationPort = Convert.ToString(udp.DestinationPort);
                tempPacket.packetMessage = Convert.ToString(udp.Data);
                packetsList.Add(tempPacket);
                string[] row = { packetsList[countOfPacketCaptures].packetType, packetsList[countOfPacketCaptures].sourceAddress, packetsList[countOfPacketCaptures].destinationAddress, packetsList[countOfPacketCaptures].sourcePort, packetsList[countOfPacketCaptures].destinationPort, packetsList[countOfPacketCaptures].packetMessage };
                try { dgwPacketInfo.Rows.Add(row);
                countOfPacketCaptures++;
                lblCapturesLabels.Text = Convert.ToString(countOfPacketCaptures);
                }
                catch (Exception e) { }


                }


            }
        }
  • 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-14T01:08:51+00:00Added an answer on May 14, 2026 at 1:08 am

    This is really just a synthesis of the advice you’ve been given thus far.

    The points that have been given so far are:

    1. Convert your data into a format that ListView can understand.
    2. Separate concerns to make testing and maintenance easier.
    3. Understand and work with the WinForms threading model.

    Code follows:

    public partial class Form1 : Form
    {
      private readonly Device _device;
      private readonly PacketBinder _packetBinder;
    
      public Form1()
      {
        InitializeComponent();
        _device = Device.GetInstance();
        _packetBinder = PacketBinder.GetInstance(listView1);
    
        var thread = new Thread(WritePackets);
        thread.IsBackground = true;
    
        thread.Start();
      }
    
      private void WritePackets()
      {
        Packet packet = null;
        int countOfPacketCaptures = 0;
        while ((packet = _device.GetNextPacket()) != null)
        {
          packet = _device.GetNextPacket();
          MyPacket tempPacket = null;
    
          if (packet is TCPPacket)
          {
            TCPPacket tcp = (TCPPacket)packet;
            tempPacket = MyPacket.GetInstance();
    
            tempPacket.PacketType = "TCP";
            tempPacket.SourceAddress = Convert.ToString(tcp.SourceAddress);
            tempPacket.DestinationAddress =
              Convert.ToString(tcp.DestinationAddress);
            tempPacket.SourcePort = Convert.ToString(tcp.SourcePort);
            tempPacket.DestinationPort = Convert.ToString(tcp.DestinationPort);
            tempPacket.PacketMessage = Convert.ToString(tcp.Data);
          }
          else if (packet is UDPPacket)
          {
    
            UDPPacket udp = (UDPPacket)packet;
    
    
            tempPacket = MyPacket.GetInstance();
    
            tempPacket.PacketType = "UDP";
            tempPacket.SourceAddress = Convert.ToString(udp.SourceAddress);
            tempPacket.DestinationAddress =
              Convert.ToString(udp.DestinationAddress);
            tempPacket.SourcePort = Convert.ToString(udp.SourcePort);
            tempPacket.DestinationPort = Convert.ToString(udp.DestinationPort);
            tempPacket.PacketMessage = Convert.ToString(udp.Data);
          }
    
          if (tempPacket != null)
          {
            _packetBinder.AddPacketToView(tempPacket);
          }
        }
      }
    }
    
    internal class Device
    {
      private Device() { }
    
      internal static Device GetInstance()
      {
        return new Device();
      }
    
      internal Packet GetNextPacket()
      {
        var random = new Random();
        var coin = random.Next(2);
    
        Thread.Sleep(500);
    
        if (coin == 0)
        {
          return new TCPPacket()
          {
            Data = GetRandomString(random),
            SourceAddress = GetRandomString(random),
            SourcePort = random.Next(),
            DestinationAddress = GetRandomString(random),
            DestinationPort = random.Next()
          };
        }
        else
        {
          return new UDPPacket()
          {
            Data = GetRandomString(random),
            SourceAddress = GetRandomString(random),
            SourcePort = random.Next(),
            DestinationAddress = GetRandomString(random),
            DestinationPort = random.Next()
          };
        }
      }
    
      private string GetRandomString(Random random)
      {
        var bytes = new byte[16];
        random.NextBytes(bytes);
    
        return Convert.ToBase64String(bytes);
      }
    }
    
    internal class MyPacket
    {
      private MyPacket() { }
    
      internal static MyPacket GetInstance()
      {
        return new MyPacket();
      }
    
      internal string PacketType { get; set; }
      internal string SourceAddress { get; set; }
      internal string DestinationAddress { get; set; }
      internal string SourcePort { get; set; }
      internal string DestinationPort { get; set; }
      internal string PacketMessage { get; set; }
    }
    
    internal class ThreadSafeListViewMutator
    {
      private readonly ListView _target;
    
      private ThreadSafeListViewMutator(ListView target)
      {
        _target = target;
      }
    
      internal static ThreadSafeListViewMutator GetInstance(ListView target)
      {
        return new ThreadSafeListViewMutator(target);
      }
    
      internal void AddListViewItem(ListViewItem listItem)
      {
        Action action = () => _target.Items.Add(listItem);
        Delegate asDelegate = action;
        var handle = _target.BeginInvoke(asDelegate);
        _target.EndInvoke(handle);
      }
    }
    
    internal class PacketBinder
    {
      private readonly ThreadSafeListViewMutator _target;
    
      private PacketBinder(ListView target)
      {
        _target = ThreadSafeListViewMutator.GetInstance(target);
      }
    
      internal static PacketBinder GetInstance(ListView target)
      {
        return new PacketBinder(target);
      }
    
      internal void AddPacketToView(MyPacket tempPacket)
      {
        var listItem = new ListViewItem() { Text = tempPacket.PacketType };
    
        AddSubItem(listItem, "From", tempPacket.SourceAddress + ":"
          + tempPacket.SourcePort);
        AddSubItem(listItem, "To", tempPacket.DestinationAddress + ":"
          + tempPacket.DestinationPort);
        AddSubItem(listItem, "Message", tempPacket.PacketMessage);
    
        _target.AddListViewItem(listItem);
      }
    
      private void AddSubItem(ListViewItem listItem, string attribute, string value)
      {
        listItem.Text = listItem.Text + @"
    " + attribute + " = " + value;
      }
    }
    
    internal class Packet { }
    
    // Are these really duplicated this way?  Seems crazy...
    internal class TCPPacket : Packet
    {
      internal string SourceAddress { get; set; }
      internal string DestinationAddress { get; set; }
      internal int SourcePort { get; set; }
      internal int DestinationPort { get; set; }
      internal string Data { get; set; }
    }
    
    internal class UDPPacket : Packet
    {
      internal string SourceAddress { get; set; }
      internal string DestinationAddress { get; set; }
      internal int SourcePort { get; set; }
      internal int DestinationPort { get; set; }
      internal string Data { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I want to capitalize each word in a script, for this I have
Greetings. I have a java method that I consider expensive, and I'm trying to
Greetings Stackoverflowers, I have an application in which a communication abstraction layer exists. Each
Greetings! I have heard of various methods to capture and return text. For example
greetings all I have a post method in a controller, which redirects to a
Greetings, I have a tab control and I want to have 1 of the
Greetings - I have a table of Articles and a table of Categories. An
Greetings I have a program that creates multiples instances of a class, runs the
Greetings! I have a Repeater control that's using an XmlDataSource control. <asp:FormView id=myFormView runat=server
Greetings! I'm trying to have a UIWebView controlled from generated events. Is there a

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.