简体   繁体   中英

How to get variable from backgroundWorker

I have a button that calls a method which calls another method which starts a backgroundworker. The method that calls the backgroundworker

This is the code that calls the first method from the button string result = SocketSendReceive(host, port, txtConn, txtInput, backgroundWorker1, input, txtUsername, txtPassword, txtIP);

That calls this method (just the beginning that calls second method):

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);
        }

That calls this:

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;
    }

Which calls my 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.");
    }

I need to be able to return the s variable back to the SocketSendReceive method.

EDIT

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;
    }

In backgroundWorker1_DoWork() add e.Result = s;

Then in ConnectSocket() add

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM