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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:27:01+00:00 2026-05-28T16:27:01+00:00

I have created 2 programs. Both use timers with interval set to 250 milliseconds.

  • 0

I have created 2 programs. Both use timers with interval set to 250 milliseconds.

The problem is that my first program doesn’t lock then I run it, and the second locks with out giving me ability to click stop button. By saying locks I mean that program runs do it’s job until I stop it from VS.

I don’t understand why I can’t stop my first program while the basic same way work for other program. Any ideas?

Program that locks:

    private void btnScan_Click(object sender, EventArgs e)
    {
        tmrInterval.Interval = (int)nudInterval.Value;
        tmrInterval.Start();
    }

    private void ScanPort(IPAddress address, int port)
    {
        using (TcpClient client = new TcpClient())
        {
            IAsyncResult result = client.BeginConnect(address, port, null, null);

            if (result.AsyncWaitHandle.WaitOne((int)nudTimeout.Value, false)) txtDisplay.AppendText("Port: " + port + " is open." + Environment.NewLine);
            else txtDisplay.AppendText("Port: " + port + " is closed." + Environment.NewLine);
        }
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
        tmrInterval.Stop();
    }

    private void tmrInterval_Tick(object sender, EventArgs e)
    {
        ScanPort(IPAddress.Parse(txtIP.Text), currentPort);
        currentPort++;
        if (currentPort == nudTo.Value) tmrInterval.Stop();
    }

Program that doesn’t lock:

    void tmrPingInterval_Tick(object sender, EventArgs e)
    {
        if (txtTo.Text == string.Empty) Ping(IPAddress.Parse(ip2str(startIP)));
        else
        {
            if (currentIP >= endIP) tmrPingInterval.Stop();
            Ping(IPAddress.Parse(ip2str(currentIP)));
            currentIP++;
        }
    }

    private void btnPing_Click(object sender, EventArgs e)
    {
        if (txtFrom.Text != string.Empty)
        {
            txtFrom.Enabled = false;
            txtTo.Enabled = false;
            txtDisplay.Text = string.Empty;
            tsslPingCount.Text = string.Empty;
            count = 0;
            open = 0;
            closed = 0;
            tmrPingInterval.Interval = int.Parse(nudInterval.Value.ToString());

            try
            {
                startIP = str2ip(txtFrom.Text);
                if (txtTo.Text != string.Empty) endIP = str2ip(txtTo.Text);
                currentIP = startIP;
                tmrPingInterval.Start();
            }
            catch
            {
                MessageBox.Show("Input must be in IP format: 100.100.100.100", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtFrom.Enabled = true;
                txtTo.Enabled = true;
            }
        }
        else MessageBox.Show("IP field cannot be empty!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
        txtFrom.Enabled = true;
        txtTo.Enabled = true;
        tmrPingInterval.Stop();
    }

    private void Ping(IPAddress address)
    {
        Ping pingSender = new Ping();
        PingOptions options = new PingOptions();
        if (cbDontFragment.Checked) options.DontFragment = true;
        else options.DontFragment = false;
        string data = string.Empty;
        int dataCounter = 0;
        options.Ttl = (int)nudTTL.Value;

        for (int i = 0; i < nudData.Value; i++)
        {
            dataCounter++;
            if (dataCounter == 10) dataCounter = 0;
            data += dataCounter.ToString();
        }

        byte[] buffer = Encoding.ASCII.GetBytes(data);
        int timeout = 120;
        try
        {
            PingReply reply = pingSender.Send(address, timeout, buffer, options);

            if (reply.Status == IPStatus.Success)
            {
            }
            else
            {
            }
        }
        catch (Exception ex)
        {
            txtDisplay.SelectedText += Environment.NewLine + ex.Message;
        }
    }
  • 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-28T16:27:03+00:00Added an answer on May 28, 2026 at 4:27 pm

    I’m going to make a guess here since we don’t have enough information. I am guessing that tmrInterval in a System.Windows.Forms.Timer. If that is the case, the what’s happening is that when the timer ticks, it is handed as a windows message (the same way that mouse clicks, keystrokes, and everything else that make your application appear ‘not frozen’ are handled).

    In the first application, you are doing something that takes substantial time- opening a TCP port, then waiting for a (presumably) large number of milliseconds until the TCP connects or doesnt, then almost immediately doing it again the next time that the timer ticks. You are literally never giving the application a chance to respond to mouse clicks and keystrokes because the UI thread is busy trying to connect to some random ports somewhere.

    In the second application, you are doing something relatively quick- just sending a ping somewhere. what you will probably see in the second application is that your application will ‘stutter’- become unresponsive for just a second or less while it pings whatever other machine you pointed it at. It doesn’t get hung because the UI thread isn’t being given that much work to do.

    To fix both of these, you should look into implementing a BackgroundWorker. Timers (of any sort) are not generally considered a good way to spawn background tasks in .NET.

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

Sidebar

Related Questions

I have written a runtime-created dialog class that doesn't use any resource files based
I have created Excel Sheet using Java program. It works fine. My problem is,
I have created a non-form c# program that uses the NotifyIcon class. The text
I have created a small FTP program, it's just for my own use, so
I have a uploader program that has a JTable displaying a set of rows
Many programs have created a huge amount of swap files. They annoy me, because
I have created a program which allows me to upload images to my server.
I have created a concurrent, recursive directory traversal and file processing program, which sometimes
I have a web service I'm trying to load test. I created a program
I have a program that creates a Windows user account using the NetUserAdd() API

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.