This project is read-only.

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

Mar 14, 2011 at 6:06 AM
Edited Mar 14, 2011 at 6:06 AM

"An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll"

My code :

 public Statistics(PacketCommunicator packetcom, int deviceIndex)
        {
            InitializeComponent();
           communicator= packetcom ;
            i = deviceIndex;
            Thread thread = new Thread(capture);
            thread.IsBackground = true;
            thread.Start();
        }
        public void capture()
        {
            IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
            //deviceIndex = 0;
            PacketDevice selectedOutputDevice = allDevices[i - 1];

            // Open the output adapter
            using (
                communicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous,
                                                                            1000))
            {
                // Compile and set the filter
              //  communicator.SetFilter("");

                // Put the interface in statstics mode
                communicator.Mode = PacketCommunicatorMode.Statistics;

                // Console.WriteLine("TCP traffic summary:");

                // Start the main loop
                communicator.ReceiveStatistics(0, StatisticsHandler);
            }
        }
        private  void StatisticsHandler(PacketSampleStatistics statistics)
        {
            // Current sample time
            DateTime currentTimestamp = statistics.Timestamp;

            // Previous sample time
            DateTime previousTimestamp = _lastTimestamp;

            // Set _lastTimestamp for the next iteration
            _lastTimestamp = currentTimestamp;

            // If there wasn't a previous sample than skip this iteration (it's the first iteration)
            if (previousTimestamp == DateTime.MinValue)
                return;

            // Calculate the delay from the last sample
            double delayInSeconds = (currentTimestamp - previousTimestamp).TotalSeconds;

            // Calculate bits per second
            double bitsPerSecond = statistics.AcceptedBytes * 8 / delayInSeconds;

            // Calculate packets per second
            double packetsPerSecond = statistics.AcceptedPackets / delayInSeconds;

            update_textbox(packetsPerSecond);
            update_bitps(bitsPerSecond);
        }
        private static DateTime _lastTimestamp;


        private void update_textbox(double count)
        {
            if (InvokeRequired)
            {
                label_numberpacket.Invoke(new Updatenumberpacket(OnUpdatePacket), count);
            }
            else
                OnUpdatePacket(count);
        }
        private void OnUpdatePacket(double count)
        {
           label_numberpacket.Text = count.ToString();
        }
        private void update_bitps(double count)
        {
            if (InvokeRequired)
            {
                label_bit.Invoke(new Updatenumberpacket(OnUpdate_bit), count);
            }
            else update_bitps(count);
        }
        private void OnUpdate_bit(double count)
        {
            label_bit.Text = count.ToString();
       }

Mar 18, 2011 at 6:48 PM

Hi haonhien,

 

Can you please add the full exception details (including the callstack)? This will help a lot in understanding the problem.

From what I can guess looking at the code you've posted, something in your UI code is calling itself in infinite recursion and causes a stack overflow.

In general I suggest trying to debug this and see where exactly is the problematic code.

 

Boaz.

Mar 20, 2011 at 7:01 AM

  private void update_textbox(double count)
        {
            if (label_numberpacket.InvokeRequired)
            {
                label_numberpacket.Invoke(new Updatenumberpacket(OnUpdatePacket), count);
            }
            else update_textbox(0);
        }
        private void OnUpdatePacket(double count)
        {
           label_numberpacket.Text = count.ToString();
        }
        private void update_bitps(double count)
        {
            if (label_bit.InvokeRequired)
            {
                label_bit.Invoke(new Updatenumberpacket(OnUpdate_bit), count);
      
            }
            //else update_bitps(count);
        }
        private void OnUpdate_bit(double count)
        {
            try
            {
                label_bit.Text = count.ToString();
            }

            catch (System.InvalidOperationException)
            {
                MessageBox.Show("Erroe Invoke lable");
            }
        }

Mar 20, 2011 at 9:46 AM

It does seem that you call recursively in the UI code (possibly infinitely).

To verify that this is the problem, please post the full exception details including the callstack.