【问题标题】:How can I multithread (effectively) on Windows Forms?如何在 Windows 窗体上实现多线程(有效)?
【发布时间】:2026-01-22 07:30:01
【问题描述】:

这个让我很难受。 问题是我有一个在 MIDI 中播放一些音符的代码,我希望能够暂停它,所以我做了一个这样的简单表单:

namespace Music
{
    public partial class Form1 : Form
    {

        static BackgroundWorker _bw = new BackgroundWorker
        {
            WorkerSupportsCancellation = true
        };

        private void button1_Click(object sender, EventArgs e)
        {
            if (!Playing)
            {
                Playing = true;
                _bw.DoWork += Start_Playing;
                _bw.RunWorkerAsync("Hello to worker");
            }
            else
            {
                Playing = false;
                _bw.CancelAsync();
            }
        }

        static void Start_Playing(object sender, DoWorkEventArgs e)
        {
            //Plays some music
        }
    }
}

当我点击它开始播放,但无论我做什么,它都无法停止。但问题是,如果我在控制台中做同样的事情,它会完美运行。

我错过了什么吗? 如何控制表单中的单独线程?

【问题讨论】:

  • CancelAsync() 方法只是将CancellationPending 属性设置为true。由工作线程来处理它(很可能是通过定期检查挂起的取消),因为它认为合适。

标签: c# .net winforms multithreading backgroundworker


【解决方案1】:

这似乎有效...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private BackgroundWorker _bw = new BackgroundWorker { WorkerSupportsCancellation = true, 
            WorkerReportsProgress = true};
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_bw.IsBusy)
            {
                _bw.CancelAsync();
            }
            else
            {
                _bw.ProgressChanged += new ProgressChangedEventHandler(_bw_ProgressChanged);
                _bw.DoWork += new DoWorkEventHandler(_bw_DoWork);
                _bw.RunWorkerAsync();
            }
        }

        void _bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            textBox1.Text += (string)e.UserState;
        }

        void _bw_DoWork(object sender, DoWorkEventArgs e)
        {
            int count = 0;
            while (!_bw.CancellationPending)
            {
                _bw.ReportProgress(0, string.Format("worker working {0}", count));
                ++count;
                Thread.Sleep(2000);
            }
        }
    }
}

【讨论】:

  • 成功了!是的,正如 Ani 所说,我将 CancellationPending 属性设置为 true,但实际上并没有取消。奇怪的是,它在控制台中确实有效。谢谢!!