使用backgroundworker和事件处理程序的多线程

时间:2016-11-23 02:03:16

标签: c# multithreading winforms backgroundworker

我正在开发一个使用backgroundworker连接多个设备的示例程序。连接的每个设备将作为新对象添加到列表中。完成所有设备的连接后,我想为每个连接的设备添加一个事件处理程序。我现在面临的问题是事件处理程序根本没有触发。以下是示例代码。

连接点击按钮事件:

private void btnConnect_Click(object sender, EventArgs e)
{
    using (BackgroundWorker m_oWorker = new BackgroundWorker())
    {
        m_oWorker.DoWork += delegate (object s, DoWorkEventArgs args)
        {
            int iIpStart = 0;
            int iIpEnd = 0;
            string strIp1 = string.Empty;
            string strIp2 = string.Empty;

            list.Clear();

            string[] sIP1 = txtIpStart.Text.Trim().ToString().Split('.');
            string[] sIP2 = txtIpEnd.Text.Trim().ToString().Split('.');

            iIpStart = Convert.ToInt32(sIP1[3]);
            iIpEnd = Convert.ToInt32(sIP2[3]);

            strIp1 = sIP1[0] + "." + sIP1[1] + "." + sIP1[2] + ".";
            strIp2 = sIP2[0] + "." + sIP2[1] + "." + sIP2[2] + ".";

            Ping ping = new Ping();
            PingReply reply = null;

            int iIncre = 0;
            int iVal = (100 / (iIpEnd - iIpStart));
            for (int i = iIpStart; i <= iIpEnd; i++)
            {
                Thread.Sleep(100);

                string strIpconnect = strIp1 + i.ToString();
                Console.Write("ip address : " + strIpconnect + ", status: ");
                reply = ping.Send(strIpconnect);

                if (reply.Status.ToString() == "Success")
                {
                    if (ConnectDevice(strIpconnect))
                    {
                        strLastDevice = strIpconnect + " Connected";
                        isconnected = true;
                    }
                    else
                    {
                        isconnected = false;
                    }
                }
                else
                {
                    isconnected = false;
                }
                m_oWorker.ReportProgress(iIncre);
                iIncre = iIncre + iVal;
            }
            m_oWorker.ReportProgress(100);
        };
        m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
        m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);
        m_oWorker.WorkerReportsProgress = true;
        m_oWorker.WorkerSupportsCancellation = true;

        m_oWorker.RunWorkerAsync();
    }
}

ConnectDevice函数方法。已连接的设备将添加到列表中:

protected bool ConnectDevice(string sIP)
{
    try
    {
        NewSDK sdk = new NewSDK();

        if (sdk.Connect() == true)
        {
            list.Add(new objSDK { sdk = sdk, ipaddress = sIP });
            return true;
        }
        else
        {

        }
    }
    catch() {}
    return false;
}

背景工作者:

void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    //If it was cancelled midway
    if (e.Cancelled)
    {
        lblStatus.Text = "Task Cancelled.";
    }
    else if (e.Error != null)
    {
        lblStatus.Text = "Error while performing background operation.";
    }
    else
    {
        lblStatus.Text = "Task Completed...";
        btnListen.Enabled = true;
    }
}

void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //Here you play with the main UI thread
    progressBar1.Value = e.ProgressPercentage;
    lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";

    if (isconnected)
    {
        listBox2.Items.Add(strLastDevice);
        string[] ssplit = sDeviceInfo.Split(';');

        foreach (string sword in ssplit)
        {
            listBox1.Items.Add(sword);
        }
    }
}

附加事件的功能:

private void RegisterEvent()
{
    foreach (objSDK obj in list)
    {
        obj.sdk.OnTransaction += () =>
        {
            listBox1.Items.Add("ip : " + obj.IP + " transaction");
        };
    }
}

2 个答案:

答案 0 :(得分:1)

您已将m_oWorker声明为本地变量。我猜这是一个错误(m_前缀只应该用于类成员变量)?

此外,您在using语句中声明了它,这意味着框架将在using块的末尾调用Dispose()。即使你持续引用它(我也不认为你这样做),它仍然意味着它的资源将被解除分配,这可能就是为什么它不能处理任何事件。

答案 1 :(得分:0)

我通过使用线程和任务尝试另一种解决方法并且完美地工作。感谢所有回复