我有一个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
varialbe _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
设置为true,但再次执行。为什么会这样?
我的代码在C#中,框架4,项目在winforms中,在Visual Studio 2010 Pro中构建。
答案 0 :(得分:3)
您的代码未针对volatile
字段进行测试 - 它正在针对isDownloading
进行测试,其中看起来像<本地“,但是(因为它被捕获)是实际上是一个常规(非volatile
)字段。所以:要么使用某种内存屏障,要么强迫它成为易失性读取。或者更简单:完全摆脱isDownloading
,并检查该属性。
顺便提一下,volatile
的缓存失败属性不关键字的意图,而是:结果。它会起作用,但个人我建议编写代码以按意图而非后果,或许使用简单的lock
或类似的东西Interlocked
。