如何在 Windows 窗体上(有效地)使用多线程?

发布于 2024-10-14 12:25:50 字数 888 浏览 5 评论 0原文

这让我很难过。 问题是我有一个在 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
        }
    }
}

当我单击它时,它开始播放,但无论我做什么,它停不下来。但问题是,如果我在控制台中做同样的事情,它就会完美地工作。

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

This one is giving me a hard time.
The thing is that I have a code that plays some notes in MIDI, and I wanted to be able to pause it, so I made a simple Form like this:

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

And when I click it starts playing, but no matter what I do, it can't stop. But the thing is that if I do the same thing in the console it works perfect.

Did I miss something?
How can I control a separate thread from the form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别想她 2024-10-21 12:25:50

这似乎有效...

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

This seems to work...

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);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文