Search code examples
c#async-awaithttpwebrequest

C# By Pressing button do in background


Can someone explain me how can i do async tasks from this code? Currently code does work and do the job but i have tried to make it async? So app will not get frozen while its executing but no success.

public WebpageLocalScanner()
    {

        InitializeComponent();
        InitializeListView();

    }

internal string GetType(string ip, int port)
    {
        try
        {
            if (port == 1234 || port == 4321)
            {
                string urlAddress = string.Format("http://{0}", ip);
                string text = this.GetHttpData(ip, 80, urlAddress, 5120).ToUpper();
                if (text.Contains("<HTML>"))
                {
                    return "Webpage is here";
                }
            }
        }
        catch (Exception)
        {
        }
        return string.Empty;
    }


public void AddItem() {

            listView1.Items.Clear();

            int froms192 = Convert.ToInt32(tB1.Text);
            int froms168 = Convert.ToInt32(tB2.Text);
            int froms1a = Convert.ToInt32(tB3.Text);
            int froms1b = Convert.ToInt32(tB4.Text);
            int fromports = Convert.ToInt32(tBp1.Text);
            int toports = Convert.ToInt32(tBp2.Text);

            string FromIP = froms192 + "." + froms168 + "." + froms1a + "." + froms1b;
            int FromPort = fromports;
            int ToPort = toports;

            int tos192 = Convert.ToInt32(tB5.Text);
            int tos168 = Convert.ToInt32(tB6.Text);
            int tos1a = Convert.ToInt32(tB7.Text);
            int tos1b = Convert.ToInt32(tB8.Text);

            string ToIP = froms192 + "." + froms168 + "." + froms1a + "." + froms1b;

            if (froms1a < tos1a || froms1b < tos1b)
            {

                for (int i = froms1b; i <= tos1b; i++)
                {

                    for (int u = froms1a; u <= tos1a; u++)
                    {   
                        for (int p = fromports; p <= toports; p++)
                        {
                        string GenIP = froms192 + "." + froms168 + "." + u + "." + i;


                        string result = GetType(GenIP, p);
                        if (result != null) {
                            string[] row = { Convert.ToString(GenIP), Convert.ToString(fromports), Convert.ToString(result), Convert.ToString(tos1a), Convert.ToString(tos1a) };
                            var listViewItem = new ListViewItem(row);
                            listView1.Items.Add(listViewItem);
                        };


                        }
                    }
                }
            }

    }

private void InitializeListView()
    {
        // Set the view to show details.
        listView1.View = View.Details;
        // Allow the user to edit item text.
        listView1.LabelEdit = true;
        // Allow the user to rearrange columns.
        listView1.AllowColumnReorder = true;
        // Display check boxes.
        // listView1.CheckBoxes = true;
        // Select the item and subitems when selection is made.
        listView1.FullRowSelect = true;
        // Display grid lines.
        listView1.GridLines = true;
        // Sort the items in the list in ascending order.
        listView1.Sorting = SortOrder.Ascending;
        // Attach Subitems to the ListView
        listView1.Columns.Add("IP", 100, HorizontalAlignment.Left);
        listView1.Columns.Add("PORT", 50, HorizontalAlignment.Left);
        listView1.Columns.Add("SERVER", 100, HorizontalAlignment.Left);
        listView1.Columns.Add("TYPE", 100, HorizontalAlignment.Left);
        listView1.Columns.Add("COMMENT", 100, HorizontalAlignment.Left);            
    }

private void button1_Click(object sender, EventArgs e)
    {
        AddItem();
    }

What im really struggle is understand how can i convert each function to return and can be used in background if its called.

Whatever i try its getting missing something. Can you able to make it more clear for me? What this does is simply scan local network to find current ip of webserver on other pc.

But 255 ips to do it takes a while and app just hungs and have to wait till its finished?

ANSWER CODE APPLIED:

public async void AddItem()
    {

        //listView1.Items.Clear();




        int froms192 = Convert.ToInt32(tB1.Text);
        int froms168 = Convert.ToInt32(tB2.Text);
        int froms1a = Convert.ToInt32(tB3.Text);
        int froms1b = Convert.ToInt32(tB4.Text);
        int fromports = Convert.ToInt32(tBp1.Text);
        int toports = Convert.ToInt32(tBp2.Text);

        string FromIP = froms192 + "." + froms168 + "." + froms1a + "." + froms1b;
        int FromPort = fromports;
        int ToPort = toports;

        int tos192 = Convert.ToInt32(tB5.Text);
        int tos168 = Convert.ToInt32(tB6.Text);
        int tos1a = Convert.ToInt32(tB7.Text);
        int tos1b = Convert.ToInt32(tB8.Text);

        string ToIP = froms192 + "." + froms168 + "." + froms1a + "." + froms1b;

        if (froms1a < tos1a || froms1b < tos1b)
        {
            //var listViewItems = new List<ListViewItem>();
            await Task.Run(() =>
            {
                for (int i = froms1b; i <= tos1b; i++)
                {
                    List<string[]> rows = new List<string[]>();
                    for (int u = froms1a; u <= tos1a; u++)
                    {
                        for (int p = fromports; p <= toports; p++)
                        {
                            string GenIP = froms192 + "." + froms168 + "." + u + "." + i;

                            string result = GetProxyType(GenIP, p);
                            if (result != "")
                            {
                                string[] row = { Convert.ToString(GenIP), Convert.ToString(fromports), Convert.ToString(result), Convert.ToString(tos1a), Convert.ToString(tos1a) };
                                var listViewItem = new ListViewItem(row);

                                if (listView1.InvokeRequired)
                                {
                                    listView1.Invoke(new MethodInvoker(delegate
                                    {


                                        listView1.Items.Add(listViewItem);

                                        //row.Checked = true;

                                    }));
                                }
                                else
                                {
                                    listView1.Items.Add(listViewItem);
                                    listViewItem.Checked = true;
                                } 
                                //string[] row = { Convert.ToString(GenIP), Convert.ToString(fromports), Convert.ToString(result), Convert.ToString(tos1a), Convert.ToString(tos1a) };
                                //listViewItems.Add(row);
                                //listView1.Items.AddRange(rows.Select(a => new ListViewItem(a)).ToArray());

                            };
                        }
                    }
                }

            });

        }

    }

    private async void button1_Click(object sender, EventArgs e)
    {

        AddItem();

    }

Solution

  • You use the async keyword on any method that will need to run asynchronously. You use await on the code that you want to run in the background. Await yields control back to the UI until that code finishes. You only use async on methods that have an await, and you only use await on code that returns a Task. If you have code that you need to run in the background and it doesn't return a Task, you can wrap it in a Task.Run.

    I think this will work, but I haven't tested it.

    public WebpageLocalScanner()
    {
    
        InitializeComponent();
        InitializeListView();
    
    }
    
    internal string GetType(string ip, int port)
    {
        try
        {
            if (port == 1234 || port == 4321)
            {
                string urlAddress = string.Format("http://{0}", ip);
                string text = this.GetHttpData(ip, 80, urlAddress, 5120).ToUpper();
                if (text.Contains("<HTML>"))
                {
                    return "Webpage is here";
                }
            }
        }
        catch (Exception)
        {
        }
        return string.Empty;
    }
    
    
    public async void AddItem() {
    
            listView1.Items.Clear();
    
            int froms192 = Convert.ToInt32(tB1.Text);
            int froms168 = Convert.ToInt32(tB2.Text);
            int froms1a = Convert.ToInt32(tB3.Text);
            int froms1b = Convert.ToInt32(tB4.Text);
            int fromports = Convert.ToInt32(tBp1.Text);
            int toports = Convert.ToInt32(tBp2.Text);
    
            string FromIP = froms192 + "." + froms168 + "." + froms1a + "." + froms1b;
            int FromPort = fromports;
            int ToPort = toports;
    
            int tos192 = Convert.ToInt32(tB5.Text);
            int tos168 = Convert.ToInt32(tB6.Text);
            int tos1a = Convert.ToInt32(tB7.Text);
            int tos1b = Convert.ToInt32(tB8.Text);
    
            string ToIP = froms192 + "." + froms168 + "." + froms1a + "." + froms1b;
    
            if (froms1a < tos1a || froms1b < tos1b)
            {
                var rows = new List<string[]>();
                await Task.Run(() => {
                    for (int i = froms1b; i <= tos1b; i++)
                    {
                        for (int u = froms1a; u <= tos1a; u++)
                        {   
                            for (int p = fromports; p <= toports; p++)
                            {
                                string GenIP = froms192 + "." + froms168 + "." + u + "." + i;
    
                                string result = GetType(GenIP, p);
                                if (result != null) {
                                    string[] row = { Convert.ToString(GenIP), Convert.ToString(fromports), Convert.ToString(result), Convert.ToString(tos1a), Convert.ToString(tos1a) };
                                    rows.add(row);
    
                                };
                            }
                        }
                    }
                });
                listView1.Items.AddRange(rows.Select(a => new ListViewItem(a)).ToArray());
            }
    
    }
    
    private void InitializeListView()
    {
        // Set the view to show details.
        listView1.View = View.Details;
        // Allow the user to edit item text.
        listView1.LabelEdit = true;
        // Allow the user to rearrange columns.
        listView1.AllowColumnReorder = true;
        // Display check boxes.
        // listView1.CheckBoxes = true;
        // Select the item and subitems when selection is made.
        listView1.FullRowSelect = true;
        // Display grid lines.
        listView1.GridLines = true;
        // Sort the items in the list in ascending order.
        listView1.Sorting = SortOrder.Ascending;
        // Attach Subitems to the ListView
        listView1.Columns.Add("IP", 100, HorizontalAlignment.Left);
        listView1.Columns.Add("PORT", 50, HorizontalAlignment.Left);
        listView1.Columns.Add("SERVER", 100, HorizontalAlignment.Left);
        listView1.Columns.Add("TYPE", 100, HorizontalAlignment.Left);
        listView1.Columns.Add("COMMENT", 100, HorizontalAlignment.Left);            
    }
    
    private async void button1_Click(object sender, EventArgs e)
    {
        AddItem();
    }