【问题标题】:Value of volatile variable doesn't change in multi-threadvolatile变量的值在多线程中不会改变
【发布时间】:2012-05-28 08:47:21
【问题描述】:

我有一个在后台运行的 winform 应用程序,其 BackgroundWorker 有一个无限循环,每小时执行一次。我的 UI Form 类是这样的:

public partial class frmAutoScript : Form
{
    private volatile bool _isDownloading = false;
    private bool IsDownloading { get { return this._isDownloading; } set { this._isDownloading = value; } }

    public frmAutoScript()
    {
        InitializeComponent();
        this.RunAutoSynchronization();
    }

    private void RunAutoSynchronization()
    {
        bool isDownloading = this.IsDownloading;

        BackgroundWorker bgwDownloader = new BackgroundWorker();
        bgwDownloader.WorkerReportsProgress = true;
        bgwDownloader.ProgressChanged += (sndr, evnt) =>
        {
            if (evnt.ProgressPercentage == 2)
                isDownloading = this.IsDownloading;
            else
            {
                this.IsDownloading = evnt.ProgressPercentage == 1;
                isDownloading = this.IsDownloading;
            }
        };
        bgwDownloader.DoWork += (sndr, evnt) =>
            {
                while (true)
                {
                    if (DateTime.Now.Hour == 16 &&
                        DateTime.Now.Minute == 0)
                    {
                        try
                        {
                            bgwDownloader.ReportProgress(2);
                            if (!isDownloading)
                            {
                                bgwDownloader.ReportProgress(1);
                                new Downloader().Download();
                            }
                            bgwDownloader.ReportProgress(0);
                        }
                        catch { }
                    }

                    System.Threading.Thread.Sleep(60000);
                }
            };
        bgwDownloader.RunWorkerAsync();
    }
}

frmAutoScript 中,我还有一个名为btnDownload 的按钮,单击该按钮时,它将下载并更改volatile 变量_isDownloading 的值。按钮的事件是这样的:

private void btnDownload_Click(object sender, EventArgs e)
{
    if (IsDownloading)
        MessageBox.Show("A download is currently ongoing. Please wait for the download to finish.",
            "Force Download", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    else
    {
        this.IsDownloading = true;
        BackgroundWorker bgwDownloader = new BackgroundWorker();
        bgwDownloader.DoWork += (sndr, evnt) =>
        {
            try
            {
                new Downloader().Download();
            }
            catch(Exception ex)
            {
                MessageBox.Show("An error occur during download. Please contact your system administrator.\n Exception: " +
                    ex.GetType().ToString() + "\nError Message:\n" + ex.Message + " Stack Trace:\n" + ex.StackTrace, "Download Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        };
        bgwDownloader.RunWorkerCompleted += (sndr, evnt) =>
        {
            this.IsDownloading = false;
        };
        bgwDownloader.RunWorkerAsync();
    }
}

但是当我点击按钮btnDownload并且_isDownloading设置为true,并且当系统时间达到4:00 PM时,new Downloader().Download();被再次执行,即使_isDownloading设置为真的。为什么会这样?

我的代码是 C#,框架 4,项目是 winforms,在 Visual Studio 2010 Pro 中构建。

【问题讨论】:

    标签: c# multithreading .net-4.0 backgroundworker volatile


    【解决方案1】:

    您的代码不是针对 volatile 字段进行测试 - 它是针对 isDownloading 进行测试,看起来 像“本地”,但(因为它被捕获)实际上是常规(非volatile)字段。所以:要么使用某种内存屏障,要么强制它成为易失性读取。或者更简单地说:完全删除isDownloading,然后检查该属性。

    顺便说一句,volatile 的缓存击败属性不是关键字的意图,而是:结果。它会工作,但个人我建议编写代码以按意图而不是结果工作,也许使用简单的lock 或类似@987654330 @。

    【讨论】:

    • 那么关键字的意图是什么?
    • @zmbq 非常复杂,了解甚少,并且与 CPU 指令重新排序有关;这是 ECMA 334 中的话:pastie.org/pastes/3981577/text。我对我的线程非常有信心,而且很简单:volatile 不是我通常用来保证行为的语义
    • 我发现了这个:msdn.microsoft.com/en-us/library/aa645755%28v=vs.71%29.aspx。我可能遗漏了一些东西,因为我不明白为什么挥​​发物没有每个人都认为的目的。
    • @zmbq 然后也许允许 Eric Lippert 添加一个词:见Atomicity, volatility and immutability are different, part three,最后一段以“Frankly”开头
    • 没有参数...也许应该有一个synchronized 关键字自动锁定所有字段访问。不过,它不会让你免于死锁。
    猜你喜欢
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 2021-04-09
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多