2014-03-07 58 views
0

你好,我有下面的代碼:防止上漲第二個事件

時是否存在縮略圖被竊聽我要下載的文件,如果不localy存在,或打開。

問題是,如果我快速點兩下 - 它下載兩次相同的文件 - 如何防止這種情況發生?

正如你所看到的我嘗試使用布爾 - 沒有幫助。

也試過使用私人靜態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(); 
     } 
    } 
} 

回答

0

你爲什麼不使用標誌(布爾,同步對象,東西)來標記下載或顯示操作過程中,在這種情況下沒有表現出來。

相反概念,這樣的事情:

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; 
} 
+0

不這樣做,它還是進入OpenOrDownload – Cheese

+0

但我必須設法解決這個問題,可以說你指着我一個正確的方向。謝謝。問題是我正在下載2個文件:縮略圖和文件 - 縮略圖下載速度更快,並刪除了工作標誌 – Cheese