易失变量的值在多线程中不会改变



我有一个在后台运行的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时,即使_isDownloading设置为 true,new Downloader().Download();也会再次执行。为什么会这样?

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

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

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

最新更新