C#在后台按do按钮

C#在后台按do按钮,c#,async-await,httpwebrequest,C#,Async Await,Httpwebrequest,有人能告诉我如何从这段代码中执行异步任务吗? 目前代码确实有效,但我已经尝试使其异步?所以应用程序在执行时不会被冻结,但不会成功 public WebpageLocalScanner() { InitializeComponent(); InitializeListView(); } internal string GetType(string ip, int port) { try {

有人能告诉我如何从这段代码中执行异步任务吗? 目前代码确实有效,但我已经尝试使其异步?所以应用程序在执行时不会被冻结,但不会成功

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();
    }
publicWebPageLocalScanner()
{
初始化组件();
初始化列表视图();
}
内部字符串GetType(字符串ip、int端口)
{
尝试
{
如果(端口==1234 | |端口==4321)
{
字符串urlAddress=string.Format(“http://{0}”,ip);
string text=this.GetHttpData(ip,80,urlAddress,5120).ToUpper();
if(text.Contains(“”)
{
返回“网页在此”;
}
}
}
捕获(例外)
{
}
返回字符串。空;
}
公共无效附加项(){
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);
inttoports=Convert.ToInt32(tBp2.Text);
字符串FromIP=froms192+“+”froms168+“+”froms1a+“+”froms1b;
int FromPort=fromports;
int-ToPort=ToPort;
int-tos192=Convert.ToInt32(tB5.Text);
int-tos168=Convert.ToInt32(tB6.Text);
int-tos1a=Convert.ToInt32(tB7.Text);
int-tos1b=Convert.ToInt32(tB8.Text);
字符串ToIP=froms192+“+”froms168+“+”froms1a+“+”froms1b;
如果(从s1a对于(int i=froms1b;i在需要异步运行的任何方法上使用async关键字。在要在后台运行的代码上使用await。await会将控制权返回到UI,直到代码完成。您只在具有await的方法上使用async,而在返回任务的代码上使用await。如果您有代码t如果您需要在后台运行,并且它不会返回任务,则可以将其包装在Task.run中

我认为这会起作用,但我还没有测试过

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();
}
publicWebPageLocalScanner()
{
初始化组件();
初始化列表视图();
}
内部字符串GetType(字符串ip、int端口)
{
尝试
{
如果(端口==1234 | |端口==4321)
{
字符串urlAddress=string.Format(“http://{0}”,ip);
string text=this.GetHttpData(ip,80,urlAddress,5120).ToUpper();
if(text.Contains(“”)
{
返回“网页在此”;
}
}
}
捕获(例外)
{
}
返回字符串。空;
}
公共异步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);
inttoports=Convert.ToInt32(tBp2.Text);
字符串FromIP=froms192+“+”froms168+“+”froms1a+“+”froms1b;
int FromPort=fromports;
int-ToPort=ToPort;
int-tos192=Convert.ToInt32(tB5.Text);
int-tos168=Convert.ToInt32(tB6.Text);
int-tos1a=Convert.ToInt32(tB7.Text);
int-tos1b=Convert.ToInt32(tB8.Text);
字符串ToIP=froms192+“+”froms168+“+”froms1a+“+”froms1b;
如果(从s1a{

对于(int i=froms1b;i)步骤1:从您编写的所有代码中删除
catch(Exception){}
。步骤2:阅读。是的!谢谢,我应用了您代码的一些想法,终于能够修复其余错误。现在它在后台运行!-将更新我的问题以获得答案!再次感谢。
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();
}