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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:27:17+00:00 2026-06-09T04:27:17+00:00

So I am trying to create a basic WinForm Application that uses SNMP, from

  • 0

So I am trying to create a basic WinForm Application that uses SNMP, from snmpsharpnet.

I have two buttons ‘Eye’ and ‘Jitter’ that when one is pressed a timer starts which executes SNMP code inside the timer handler every minute.

I am trying to write the SNMP output to a textbox from the timer handler but everything I try either leads to thread exceptions or a continuous running process when I exit the program.

I have tried so many different things to fix those two errors that I may be screwing everything up but here is the code I have:

using System;
using System.Net;
using SnmpSharpNet;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static bool stop = false; 
        static bool min = false, sec = false, eye = false, jitter = false;
        static string ipAdd = "";
        static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        static int alarmCounter = 1;
        static bool exitFlag = false;
        static TextBox textbox;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textbox = outputBox;
        }

        private void IPtext_TextChanged(object sender, EventArgs e)
        {
            ipAdd = IPtext.Text;
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            stop = true;
            timer.Stop();
        }

        // This is the method to run when the timer is raised.
        private static void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs)
        {
            timer.Stop();

            // If stop button has not been pressed then continue timer.
            if (stop == false)
            {
                textbox.Clear();
                // Restarts the timer and increments the counter.
                alarmCounter += 1;
                timer.Enabled = true;
                /*
                textbox.Invoke(
                    new MethodInvoker(
                        delegate { textbox.AppendText("fsjdaò"); }));
                */
                System.IO.StreamWriter file;
                if (eye == true)
                {
                    file = new System.IO.StreamWriter("c:/Users/bshellnut/Desktop/Eye.txt", true);
                }
                else
                {
                    file = new System.IO.StreamWriter("c:/Users/bshellnut/Desktop/Jitter.txt", true);
                }

                // SNMP community name
                OctetString community = new OctetString("public");

                // Define agent parameters class
                AgentParameters param = new AgentParameters(community);
                // Set SNMP version to 2 (GET-BULK only works with SNMP ver 2 and 3)
                param.Version = SnmpVersion.Ver2;
                // Construct the agent address object
                // IpAddress class is easy to use here because
                //  it will try to resolve constructor parameter if it doesn't
                //  parse to an IP address
                IpAddress agent = new IpAddress(ipAdd);

                // Construct target
                UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);

                // Define Oid that is the root of the MIB
                //  tree you wish to retrieve
                Oid rootOid;
                if (eye == true)
                {
                    rootOid = new Oid("1.3.6.1.4.1.128.5.2.10.14"); // ifDescr
                }
                else
                {
                    rootOid = new Oid("1.3.6.1.4.1.128.5.2.10.15");
                }

                // This Oid represents last Oid returned by
                //  the SNMP agent
                Oid lastOid = (Oid)rootOid.Clone();

                // Pdu class used for all requests
                Pdu pdu = new Pdu(PduType.GetBulk);

                // In this example, set NonRepeaters value to 0
                pdu.NonRepeaters = 0;
                // MaxRepetitions tells the agent how many Oid/Value pairs to return
                // in the response.
                pdu.MaxRepetitions = 5;

                // Loop through results
                while (lastOid != null)
                {
                    // When Pdu class is first constructed, RequestId is set to 0
                    // and during encoding id will be set to the random value
                    // for subsequent requests, id will be set to a value that
                    // needs to be incremented to have unique request ids for each
                    // packet
                    if (pdu.RequestId != 0)
                    {
                        pdu.RequestId += 1;
                    }
                    // Clear Oids from the Pdu class.
                    pdu.VbList.Clear();
                    // Initialize request PDU with the last retrieved Oid
                    pdu.VbList.Add(lastOid);
                    // Make SNMP request
                    SnmpV2Packet result;
                    try
                    {
                        result = (SnmpV2Packet)target.Request(pdu, param);
                        //textbox.Text = "";
                    }
                    catch (SnmpSharpNet.SnmpException)
                    {
                        textbox.Invoke(
                            new MethodInvoker(
                                delegate { textbox.AppendText("Could not connect to the IP Provided."); }));
                        timer.Stop();
                        //outputBox.Text += "Could not connect to the IP Provided.";
                        break;
                    }
                    // You should catch exceptions in the Request if using in real application.

                    // If result is null then agent didn't reply or we couldn't parse the reply.
                    if (result != null)
                    {
                        // ErrorStatus other then 0 is an error returned by 
                        // the Agent - see SnmpConstants for error definitions
                        if (result.Pdu.ErrorStatus != 0)
                        {
                            // agent reported an error with the request
                            /*Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                                result.Pdu.ErrorStatus,
                                result.Pdu.ErrorIndex);*/
                                textbox.Invoke(
                                    new MethodInvoker(
                                        delegate { textbox.AppendText("Error in SNMP reply. " + "Error " + result.Pdu.ErrorStatus + " index " + result.Pdu.ErrorIndex); }));
                            //outputBox.Text = "Error in SNMP reply. " + "Error " + result.Pdu.ErrorStatus + " index " + result.Pdu.ErrorIndex;
                            lastOid = null;
                            break;
                        }
                        else
                        {
                            // Walk through returned variable bindings
                            foreach (Vb v in result.Pdu.VbList)
                            {
                                // Check that retrieved Oid is "child" of the root OID
                                if (rootOid.IsRootOf(v.Oid))
                                {
                                    /*Console.WriteLine("{0} ({1}): {2}",
                                        v.Oid.ToString(),
                                        SnmpConstants.GetTypeName(v.Value.Type),
                                        v.Value.ToString());*/

                        textbox.Invoke(
                                            new MethodInvoker(
                                                 delegate { textbox.AppendText(v.Oid.ToString() + " " + SnmpConstants.GetTypeName(v.Value.Type) +
                                            " " + v.Value.ToString() + Environment.NewLine); }));

                                    //outputBox.Text += v.Oid.ToString() + " " + SnmpConstants.GetTypeName(v.Value.Type) +
                                       //" " + v.Value.ToString() + Environment.NewLine;

                    file.WriteLine(v.Oid.ToString() + " " + SnmpConstants.GetTypeName(v.Value.Type) +
                                   " " + v.Value.ToString(), true);
                                    if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
                                        lastOid = null;
                                    else
                                        lastOid = v.Oid;
                                }
                                else
                                {
                                    // we have reached the end of the requested
                                    // MIB tree. Set lastOid to null and exit loop
                                    lastOid = null;
                                }
                            }
                        }
                    }
                    else
                    {
                        //Console.WriteLine("No response received from SNMP agent.");
                        textbox.Invoke(
                            new MethodInvoker(
                                delegate { textbox.AppendText("No response received from SNMP agent."); }));
                        //outputBox.Text = "No response received from SNMP agent.";
                    }
                }
                target.Close();
                file.Close();
            }
            else
            {
                // Stops the timer.
                exitFlag = true;
            }
        }

        private void eyeButton_Click(object sender, EventArgs e)
        {
            outputBox.Text = "Connecting...";
            eye = true;
            jitter = false;
            stop = false;

            timer.Tick += new EventHandler(TimerEventProcessor);

            // Sets the timer interval to 5 seconds.
            timer.Interval = 5000;
            timer.Start();

            // Runs the timer, and raises the event.
            while (exitFlag == false)
            {
                // Processes all the events in the queue.
                Application.DoEvents();
            }
        }

        private void jitterButton_Click(object sender, EventArgs e)
        {
            outputBox.Text = "Connecting...";
            eye = false;
            jitter = true;
            stop = false;

            timer.Tick += new EventHandler(TimerEventProcessor);

            // Sets the timer interval to 5 seconds.
            timer.Interval = 5000;
            timer.Start();

            // Runs the timer, and raises the event.
            while (exitFlag == false)
            {
                // Processes all the events in the queue.
                Application.DoEvents();
            }
        }

        private void Seconds_CheckedChanged(object sender, EventArgs e)
        {
            min = false;
            sec = true;
        }

        private void Minutes_CheckedChanged(object sender, EventArgs e)
        {
            min = true;
            sec = false;
        }
    }
}
  • 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-06-09T04:27:19+00:00Added an answer on June 9, 2026 at 4:27 am

    I believe you are running into a deadlock on the UI thread.

    TimerEventProcessor() is called by your instance of System.Windows.Forms.Timer, which runs on the UI thread. When the timer goes off, I believe it puts a message into the UI thread’s message queue to call your TimerEventProcessor() method. That method in turn calls textbox.Invoke(), which puts another message into the queue and then waits for it to be processed.

    Your UI thread is now stuck, as it is in the middle of processing a message, but must wait until another message is processed before it can continue. The calls to Application.DoEvents() do you no good, as they are not being called once your program enters TimerEventProcessor(). (They’re also unnecessary, since your button click handlers wouldn’t be blocking the UI thread anyway.)

    Since the timer runs on the UI thread, you can get rid of the textbox.Invoke() calls and just access textbox directly.

    Summary:

    1. Replace your calls to textbox.Invoke() with direct access to textbox
    2. Remove your calls to Application.DoEvents()

    Note: if you got the Application.DoEvents() logic from the MSDN example for using a timer, they put it there so that the application doesn’t quit once the Main function completes.

    Update: You can see if this is truly the issue by replacing your calls to textbox.Invoke with the following code. If this code works, then you definitely have a UI message queue deadlocking issue. Also, if this does resolve the issue, I would not recommend keeping this as the solution, but rather, addressing the deadlocking as suggested above.

    // Make the request asynchronously
    System.IAsyncResult asyncResult = textbox.BeginInvoke(
        new MethodInvoker(
            delegate { /* insert delegate code here */ }));
    
    // Process the message queue until this request has been completed
    while(!asyncResult.IsCompleted) Application.DoEvents();
    
    // Clean up our async request
    textbox.EndInvoke(asyncResult);
    

    Since you seem to have your code working, you can ignore the above test code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a basic database trigger that conditionally deletes rows from database1.table1
I am trying to create a basic WPF application that can store an encrypted
I am trying to create a very basic sprite image. First off i have
I am trying to create a basic datasheet in my application using a TableLayout.
I have a website that uses Basic Authentication (username/password). Why is the following code
i'm trying to create basic application on Play Frramework(1.2.2) + siena (2.0.2) + crudsiena(2.0.1)
I'm trying to create a basic application, i created facebook application before but everything
Simple question. I am trying to create a basic lambda expression that returns the
I am trying to create a basic functionality in AS3. I have a class
I'm trying to create a basic video file using OpenCV (in Python). I have

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.