【发布时间】:2024-04-30 22:10:04
【问题描述】:
你好,我有下一个代码:
点击缩略图时,如果本地不存在,我必须下载文件,如果存在则打开。
问题是,如果我非常快速地轻按两次 - 它会下载同一个文件两次 - 如何防止这种情况发生?
如您所见,我尝试使用 bool - 没有帮助。
也尝试使用私有静态 SemaphoreSlim TapSemaphore = new SemaphoreSlim(1, 1); - 没有帮助
public bool IsCurrentlyDownloading = false;
private async void assetThumbnail_Tapped(object sender, TappedRoutedEventArgs e)
{
await OpenOrDownload();
}
private async Task OpenOrDownload()
{
if (FileIsDownloaded == true)
{
string filename = Util.GetLocalFileName(CustomerAsset.file.id, "CustomerAssetFile");
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = false;
var sampleFile = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
await Windows.System.Launcher.LaunchFileAsync(sampleFile, options);
}
else
{
if (!IsCurrentlyDownloading)
{
IsCurrentlyDownloading = true;
DownloadFiles();
}
}
}
【问题讨论】:
标签: c# asynchronous windows-8 windows-8.1