如何从backgroundWorker获取变量

时间:2015-12-31 01:48:30

标签: c# methods backgroundworker

我有一个调用方法的按钮,该方法调用另一个启动后台工作程序的方法。调用backgroundworker的方法

这是从按钮string result = SocketSendReceive(host, port, txtConn, txtInput, backgroundWorker1, input, txtUsername, txtPassword, txtIP);

调用第一个方法的代码

调用此方法(只是调用第二个方法的开头):

private static string SocketSendReceive(string server, int port, RichTextBox txtConn, TextBox txtInput, BackgroundWorker backgroundWorker1, string input, TextBox txtUsername, TextBox txtPassword, TextBox txtIP)
    {
        byte[] bytes = new byte[10024];
        Socket s = null;
        if (txtIP.Text == "")
        {
            s = ConnectSocket(server, port, txtConn, backgroundWorker1);
        } else if (txtIP.Text != "")
        {
            s = ConnectSocketCustom(server, port, txtConn, backgroundWorker1, txtIP);
        }

这称之为:

private static Socket ConnectSocket(string server, int port, RichTextBox txtConn, BackgroundWorker backgroundWorker1)
    {
        Socket s = null;
        IPHostEntry hostEntry = null;

        // Get host related information.
        hostEntry = Dns.GetHostEntry(server);

        // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
        // an exception that occurs when the host IP Address is not compatible with the address family
        // (typical in the IPv6 case).
        //ThreadPool.QueueUserWorkItem(i =>
        //{
            // Connection loop goes here.
        //});
        backgroundWorker1.RunWorkerAsync();

        Console.WriteLine(s);
        return s;
    }

调用我的backgroundWorker:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        backgroundWorker1.ReportProgress(0, "Attempting connection.");
        Socket s = null;
        IPHostEntry hostEntry = null;

        int port;
        if (txtPort.Text == "")
        {
            port = 11000;
        }
        else
        {
            port = Convert.ToInt32(txtPort.Text);
            port = int.Parse(txtPort.Text);
        }
        // Get host related information.
        hostEntry = Dns.GetHostEntry(host);

        // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
        // an exception that occurs when the host IP Address is not compatible with the address family
        // (typical in the IPv6 case).
        foreach (IPAddress address in hostEntry.AddressList)
        {
            IPEndPoint ipe = new IPEndPoint(address, port);
            Socket tempSocket =
                new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            Console.WriteLine(ipe);
            try
            {
                backgroundWorker1.ReportProgress(1, "Attempting connection.");
                tempSocket.Connect(ipe);
            }
            catch (ArgumentNullException ane)
            {
                Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                backgroundWorker1.ReportProgress(50, "Connection could not be established.");
            }
            catch (SocketException se)
            {
                Console.WriteLine("SocketException : {0}", se.ToString());
                backgroundWorker1.ReportProgress(50, "Connection could not be established.");
            }

            if (tempSocket.Connected)
            {
                backgroundWorker1.ReportProgress(100, "Connection established.");
                Console.WriteLine("Connected");
                s = tempSocket;
                break;
            }
            else
            {
                //backgroundWorker1.ReportProgress(50, "Connection could not be established.");
                continue;
            }
        }
        backgroundWorker1.ReportProgress(101, "Connection could not be established.");
    }

我需要能够将s变量返回给SocketSendReceive方法。

修改

private static Socket ConnectSocket(string server, int port, RichTextBox txtConn, BackgroundWorker backgroundWorker1)
    {
        Socket s = null;
        IPHostEntry hostEntry = null;

        // Get host related information.
        hostEntry = Dns.GetHostEntry(server);

        // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
        // an exception that occurs when the host IP Address is not compatible with the address family
        // (typical in the IPv6 case).
        backgroundWorker1.RunWorkerAsync();
        backgroundWorker1.RunWorkerCompleted += (s, e) =>
        {
            s = e.Result;
        };
        Console.WriteLine(s);
        return s;
    }

1 个答案:

答案 0 :(得分:0)

backgroundWorker1_DoWork()添加e.Result = s;

然后在ConnectSocket()添加

backgroundWorker1.RunWorkerCompleted += (sender, e) =>
   {
       s = e.Result;
   };