您好我有下一个代码:
当点击缩略图时,我必须下载文件,如果它不存在于localy,或打开,如果存在。
问题是,如果我快速两次点击 - 它会两次下载相同的文件 - 如何防止这种情况?
正如你所看到我尝试使用bool - 没有帮助。
尝试使用私有静态SemaphoreSlim TapSemaphore =新的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();
}
}
}
答案 0 :(得分:0)
为什么不使用标志(bool,sync对象等)标记下载或显示操作正在进行中,在这种情况下不显示。
相反,在概念上,这样的事情:
public bool IsCurrentlyDownloading = false;
bool isWorking = false;
private async void assetThumbnail_Tapped(object sender, TappedRoutedEventArgs e)
{
if(!isWorking)
await OpenOrDownload();
}
private async Task OpenOrDownload()
{
isWorking = true;
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();
}
}
isWorking = false;
}