BackgroundWorker线程没有停止winforms c#

时间:2011-12-18 00:45:38

标签: c# winforms multithreading backgroundworker

我正在使用BackgroundWorker线程调用一个小例程,一旦消息到达MSMQ队列(消息每5秒钟到达msmq),它就会从MSMQ接收消息。它来到我正在使用的应用程序BackgroundWorker主题。下面是我的Win Form类。我是线程新手,所以如果我做错了,请说明。

问题:我的应用程序是MDI应用程序,当我第一次执行我的应用程序时它完全正常并且在它到达队列时收到MSMQ消息,这是每5秒但是当我关闭此表单时是一个儿童形式,它关闭很好,但在打开这个相同的表格后,我收到消息来自MSMQ与10秒dalay,所以这意味着我搞乱后台工作线程中的东西,我试图取消这个后台工作线程,但我失败了,无法正确地取消或终止线程。请帮助并分享您的经验。下面是我的表格代码。

public partial class FrmBooking : BookingManager.Core.BaseForm.BaseForm
{
    internal const string queName = @"messageServer\private$\Response";
    private Int32 counter = 0;
    BackgroundWorker backgroundWorker1 = new BackgroundWorker();


    public FrmBooking()
    {
        InitializeComponent();
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.WorkerSupportsCancellation = true;
        backgroundWorker1.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        backgroundWorker1.ProgressChanged+=new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        backgroundWorker1.DoWork+=new DoWorkEventHandler(backgroundWorker1_DoWork);

    }           

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker bgWorker = sender as BackgroundWorker;

        if (bgWorker.CancellationPending)
        {
            e.Cancel = true;
            return;
        }


            try
            {

                MessageQueue messageQueue = null;
                if (MessageQueue.Exists(queName))
                {
                    messageQueue = new MessageQueue(queName);
                }
                else
                {
                    MessageQueue.Create(queName);
                }

                messageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });

                System.Messaging.Message msg = messageQueue.Receive();



                bgWorker.ReportProgress(100, msg);

            }
            catch (Exception ex) { }


    }
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (!e.Cancelled)
        {
            backgroundWorker1.RunWorkerAsync();
        }


    }
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

        System.Messaging.Message msg = e.UserState as System.Messaging.Message;

        listBoxControl1.Items.Add(msg.Body.ToString());
        counter++;
        labelControl1.Text = String.Format("Total messages received {0}", counter.ToString());
    }

    private void FrmBooking_Load(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();

    }
}

2 个答案:

答案 0 :(得分:1)

这里有两个选项:

1)在Form的Closing事件中,调用backgroundWorker1。CancelAsync()。

2)更好的方法是完全删除后台工作程序,并使用MessageQueue的本机异步处理机制。您可以在添加ReceivedCompleted事件处理程序后使用BeginReceive方法启动队列请求,然后在已完成的事件处理程序中处理该消息并重新启动请求。

问题在于,如果发出Receive请求,它将阻塞后台工作线程,直到队列中收到消息为止,CancelAsync将仅请求后台工作程序停止,它不会取消接收请求

例如(更新):

public partial class FrmBooking : BookingManager.Core.BaseForm.BaseForm
{
    public FrmBooking()
    {
        InitializeComponent();

        this.FormClosing += new FormClosingEventHandler(FrmBooking_FormClosing);
    }

    internal const string queName = @"messageServer\private$\Response";
    private Int32 counter = 0;
    private MessageQueue messageQueue = null;

    private bool formIsClosed = false;

    private void FrmBooking_Load(object sender, EventArgs e)
    {
        StartQueue();
    }

    void FrmBooking_FormClosing(object sender, FormClosingEventArgs e)
    {
        // Set the flag to indicate the form is closed
        formIsClosed = true;

        // If the messagequeue exists, close it
        if (messageQueue != null)
        {
            messageQueue.Close();
        }
    }

    private void StartQueue()
    {
        if (MessageQueue.Exists(queName))
        {
            messageQueue = new MessageQueue(queName);
        }
        else
        {
            MessageQueue.Create(queName);
        }

        messageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });

        // Add an event handler for the ReceiveCompleted event.
        messageQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MessageReceived);

        messageQueue.BeginReceive(TimeSpan.FromSeconds(15));
    }

    // Provides an event handler for the ReceiveCompleted event.
    private void MessageReceived(Object source, ReceiveCompletedEventArgs asyncResult)
    {
        if (!this.formIsClosed)
        {
            // End the asynchronous receive operation.
            System.Messaging.Message msg = messageQueue.EndReceive(asyncResult.AsyncResult);

            // Display the message information on the screen.
            listBoxControl1.Items.Add(msg.Body.ToString());
            counter++;
            labelControl1.Text = String.Format("Total messages received {0}", counter.ToString());

            // Start receiving the next message
            messageQueue.BeginReceive(TimeSpan.FromSeconds(15));
        }
    }

}

答案 1 :(得分:0)

当您关闭表单时,是否终止了该工作人员?如果没有,如果有一个对表单或某个地方的工作者的引用(事件处理程序?)那么它将不会得到GCd并保持不变。打开第二个实例后,您可能有两个后台工作人员正在运行....