3

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

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时,它将下载并更改volatilevarialbe的值_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,即使设置为 true ,new Downloader().Download();也会再次执行。_isDownloading为什么会这样?

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

4

1 回答 1

4

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

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

于 2012-05-28T08:52:46.967 回答