volatile变量的值不会在多线程改变 [英] Value of volatile variable doesn't change in multi-thread

查看:103
本文介绍了volatile变量的值不会在多线程改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行在后台有一个WinForm应用程序中的的BackgroundWorker ,有一个无限循环中执行的一些每隔一小时。我的UI 表格类是这样的:

I have a winform application that runs in background with a BackgroundWorker that has an infinite loop that execute something every hour. My UI Form class is something like this:

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 ,单击时,它会下载和变革的挥发性值 varialbe _isDownloading 。按钮的事件是这样的:

And in that frmAutoScript, I also have a button named btnDownload that when clicked, it will download and change the value of the volatile varialbe _isDownloading. The event of the button is something like this:

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 设置为,而当系统时间击中了 16:00 新下载()下载(); 再次执行eventhough该 _isDownloading 设置为true。为什么会这样呢?

But when I click the button btnDownload and the _isDownloading is set to true, and when the system time hit the 4:00 PM, the new Downloader().Download(); is executed again eventhough the _isDownloading is set to true. Why was it like this?

我的code是在C#中,框架4,项目是的WinForms,建立在Visual Studio 2010专业版。

My code is in C#, framework 4, project is in winforms, build in Visual Studio 2010 Pro.

推荐答案

您code未测试对挥发性字段 - 它是对<$ C测试$ C> isDownloading ,其中的看起来的像一个地方,但(因为它被捕获)实际上是一个常规(非 - 挥发性)领域。所以说:无论是使用某种记忆障碍,或力是挥发性读取。或者更简单地说:摆脱 isDownloading 中的完全,然后检查对财产

Your code is not testing against the volatile field - it is testing against isDownloading, which looks like a "local", but (because it is captured) is in fact a regular (non-volatile) field. So: either use some kind of memory barrier, or force that to be a volatile read. Or more simply: get rid of isDownloading completely, and check against the property.

顺便说一句,的缓存击败性挥发不可以的关键字的意图,而在于:结果。它会工作,但的个人的我建议你写code工作按意向而不是结果,也许是使用一个简单的锁定或类似互锁

Incidentally, the cache-defeating properties of volatile are not the intent of the keyword, but rather: a consequence. It'll work, but personally I'd suggest writing the code to work by intent rather than by consequence, perhaps using either a simple lock or something like Interlocked.

这篇关于volatile变量的值不会在多线程改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆