2016-04-19 4 views
0

私はDropbox内のファイルを使用してサーバー間で通信しています。しばらくすると、Dropbox内のファイルに対して実行されるときに、基本的には遅れがあることに気が付きました(File)。私は必要なものDropbox内のファイルが実際に削除されたことを確認するにはどうすればよいですか?

は、このようなものです:私は、迅速なソリューションを必要としているので

public class MyFile 
{ 
    const int maxWaitCount = 60; 
    //.. 
    /// <summary> 
    /// remove file - also has to work inside Dropbox directory 
    /// </summary> 
    /// <returns>0 for ok or error code</returns> 
    public int rm() 
    { 
     string name = Os.winInternal(FullName); 
     if (File.Exists(name)) 
     { 
      File.Delete(name); 
      #region double check if file is gone 
      if (Ensure) 
       return awaitFileVanishing(name); 
      #endregion 
     } 
     return 0; 
    } 
    //.. 
    private int awaitFileMaterializing(string fileName) //.. 
    private int awaitFileVanishing(string fileName) //.. 
    /// <summary> 
    /// Constructor: auto-ensure mode for file systems that do not synchronously wait for the end of an IO operation i.e. Dropbox 
    /// </summary> 
    /// <remarks>only use the ensure mode if it has to be guaranteed that the IO operation was completely done 
    /// when the method call returns; necessary e.g. for Dropbox directories since (currently) Dropbox first updates the 
    /// file in the invisible . folder and then asynchronously updates the visible file and all the remote copies of it</remarks> 
    /// <param name="filename"></param> 
    public MyFile(string filename) 
    { 
     Ensure = filename.ToLower().Contains("dropbox"); 
     // .. 
    } 
} 

、私は以下の自分の質問への答えとして提示しています1を思い付きました。しかし、あなたの中にはこの問題もあって、おそらくもっと魅力的な解決法が見つかったと簡単に想像することができます。私にお知らせください。

答えて

0

これはの一部として使用されるawaitFileMaterializingおよびawaitFileVanishingの現在のソリューションです。

private int awaitFileMaterializing(string fileName) 
    { 
     int count = 0; 
     bool exists = false; 
     while (count < maxWaitCount) 
     { 
      try 
      { 
       exists = File.Exists(fileName); 
      } 
      catch (Exception) { } // device not ready exception if Win 2003 
      if (exists) 
       break; 
      Thread.Sleep(5); 
      count++; 
     } 
     if (count >= maxWaitCount) 
      throw new FileNotFoundException("ensure failed - timeout.", fileName); 
     return -count; 
    } 
    private int awaitFileVanishing(string fileName) 
    { 
     int count = 0; 
     bool exists = true; 
     while (count < maxWaitCount) 
     { 
      try 
      { 
       exists = File.Exists(fileName); 
      } 
      catch (Exception) { } // device not ready exception if Win 2003 
      if (!exists) 
       break; 
      Thread.Sleep(5); 
      count++; 
     } 
     if (count >= maxWaitCount) 
      throw new IOException("ensure failed - timeout in deleting " + fileName + "."); 
     return -count; 
    } 
+1

代わりのスレッドを寝て、私はスレッドが完全に他の何かをすることによって利用することができるように、非同期遅延を使用することをお勧めだと思います。 – Hendry

0

FileSystemWatcherを作成して、Dropboxフォルダを監視することができます。 FileSystemWatcherには、ファイルが監視されているフォルダから削除されたときに通知する削除イベントがあります。

MSDN Source & Examples

+0

あなたはそれに対処するためのイベントドリブンな方法を勧めているようですね、ドミトリー。私の場合、プログラミングモデル全体を確実に変更するでしょう。私はいくつかのスタイルのアイデアや解決策を待っていました。 –

+0

私はドミトリーについて考えていましたが、今私は追加したいと思っています:私は変化の伝播のためにオブザーバーパターンを使用することの巨大なファンです。しかし、上記の文脈では、ファイルを削除して、同じ名前で別の内容のファイルを再度作成するなど、次の処理を実行するまで最終的に完了するまで待つ必要があります。だから、私は同期位置に座っていて、以前に開始されたアクションが完了するのを待っています。 –

関連する問題