Backgroundworkers

时间:2014-12-17 11:14:30

标签: c# backgroundworker

我知道很多这样的问题都会被问到,但我仍然无法解决我的问题。 我正在为网络管理员等制作帮助程序。当我实现端口扫描程序时,我发现了一个问题。只要它扫描端口,我就无法做任何其他事情,甚至不取消它,所以我决定使用背景工作者。现在,我的后台工作者需要给出2个参数,但是每次找到一个开放端口时,都必须将它添加到我的richtextbox中。在页面的底部你可以找到我的扫描仪代码,但我无法让它在扫描它时发送给我端口,只在最后... 任何帮助将不胜感激。

 string host = textBox4.Text;

        int portstart = (int)begin.Value;
        int count = (int)einde.Value;
        progressBar2.Value = 0;
        progressBar2.Maximum = ((int)einde.Value - (int)begin.Value);

        for (int i = portstart; i <= count; i++)
        {

            if (progressBar2.Value == progressBar2.Maximum)
            {

            }
            else
            {
                progressBar2.Value++;
            }


            using (var tcp = new System.Net.Sockets.TcpClient())
            {
                var ar = tcp.BeginConnect(host, i, null, null);
                using (ar.AsyncWaitHandle)
                {

                    if (ar.AsyncWaitHandle.WaitOne(500, false))
                    {
                        try
                        {
                            tcp.EndConnect(ar);
                            richTextBox1.Text += "Port " + i + " Is Opened" + System.Environment.NewLine;
                            //Connect was successful.

                        }
                        catch
                        {
                              richTextBox1.Text += "Connection Refused On Port " + i + System.Environment.NewLine;
                            //EndConnect threw an exception.
                            //Most likely means the server refused the connection.

                        }
                    }
                    else
                    {
                        //Connection timed out.

                    }
                }
            }
        }
        richTextBox1.Text += "End Of Scan";

1 个答案:

答案 0 :(得分:2)

您应该将扫描代码放入后台工作程序的DoWork事件处理程序中。基本上它是一个循环。您还可以实施ProgressChanged事件以显示整体扫描进度。要通知用户界面您找到的开放端口,实际上只有这样:

// Invoke the following code in context of UI thread
this.Invoke((Action)delegate()
{
    // Show the port in UI
});

您需要在UI线程的上下文中更新UI。对于ProgressChanged,后台工作程序本身确保在正确的上下文中调用它。对于所有其他UI更新,您需要自己使用例如上面的代码段。


编辑:我将添加一些示例代码。

// Call DoWork like this:
backgroundWorker1.RunWorkerAsync(new Tuple<string, int, int>(host, startPort, endPort));

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    Tuple<string, int, int> portRange = e.Argument as Tuple<string, int, int>;

    for (int i = portRange.Item2; i <= portRange.Item3; i++)
    {
        using (var tcp = new System.Net.Sockets.TcpClient())
        {
            try
            {
                tcp.SendTimeout = 500;
                tcp.ReceiveTimeout = 500;

                tcp.Connect(portRange.Item1, i);

                this.Invoke((Action)delegate()
                {
                    richTextBox1.Text += "Port " + i + " Is Opened" + System.Environment.NewLine;
                });
            }
            catch
            {
                this.Invoke((Action)delegate()
                {
                    richTextBox1.Text += "Connection Refused On Port " + i + System.Environment.NewLine;
                });
            }
        }
    }