2017-01-24 4 views
0

私は文字列を収集するクラスを持っています。文字列をターゲットファイルに書き込むメソッドコミットがあります。C#:クラスが扶養家族に頼ることなく仕事を終わらせる方法を確認するには?

文字列が収集されたにもかかわらず、コミットが呼び出されない場合は、エラーをログファイルに報告します。

IDisposableは、扶養家族がそれを忘れるため、問題を解決しません。
は私がファイナライザを実装しますが、コンパイラエラーました:私は私の会社でのコンパイラの設定を変更することはできません

The class has a finalizer implemented in it.
Consider deriving from IDisposable, CriticalFinalizerObject or SafeHandle, instead.

を。そして、提案されたオプションは、過度に複雑に見える。コミットの検証をどのように実装するのですか? W/O(文字列s)をコミット呼び出し限り、私は問題を理解して、あなたは、あまりにも長い文字列の読みとstuckedている

+0

コミットが実行されない原因は何ですか? –

+0

Commitメソッドをブール値に戻すことはできません。コミットが実行された場合はtrue?あなたの質問にあるコードによっては、助けになるかもしれません。 –

+0

これは、変数または戻り値の型を使用して行うことができます。 –

答えて

1

あなたの質問から、それは私にはこのようなものに聞こえる:

免責事項を、これはテストされていない、メモ帳でちょうど悪い書かれたコードです。

class CollectAndCommit : IDisposable 
{ 
    private Dictionary<string, bool> collectedStrings { get; set; } 

    public CollectAndCommit() 
    { 
     collectedStrings = new Dictionary<string, bool>(); 
    } 

    public void Collect(string collectedString) 
    { 
     collectedStrings.Add(collectedString, false); 
    } 

    public void CommitAllUncommitedStrings() 
    { 
     foreach (var uncommitedString in collectedStrings.Where(c => c.Value == false)) 
     { 
      collectedStrings[uncommitedString.Key] = Commit(uncommitedString.Key); 
     } 
    } 

    public bool HasUncommitedStrings() 
    { 
     return collectedStrings.Count(c => c.Value == false) > 0; 
    } 

    private bool Commit(string str) 
    { 
     try 
     { 
      string path = @"c:\temp\MyTest.txt"; 
      if (!File.Exists(path)) 
      { 
       File.Create(path); 
      } 

      using (StreamWriter sw = File.AppendText(path)) 
      { 
       sw.WriteLine(str); 
      } 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    } 

    public void Dispose() 
    { 
     if (HasUncommitedStrings()) 
     { 
      throw new Exception("Uncommited Strings!"); 
     } 
    } 
} 
-1

I have a class that collects a string. There is a method Commit, that writes the string to a target file.

- そこに小さなメモリのように、多くの理由が(それほど負荷全体のない文字列中にありますメモリに収まる)、またはあなたが連続しているいくつかのストリームから読んでいる、...

は今プログラムが

を失敗したところ、それが依存し、それは例外をスローしていますか? >try-catch/re-throwエラーを使用してください。

停止せずに読み取りますか? >読んだり、一度オーバーランしたり、エラーをスローしたり、あなたが持っているものを返すための時間制限を与えてください。&がコミットします。

public class MyClass 
{ 

    StringBuilder SB; 

    public string ObtainString() 
    { 
     // This is executed, but takes lot of time/times out 
     // Possible solution below 

     SB = new StringBuilder(); 
     DateTime dt = DateTime.Now; 
     bool isRead = false; 

     while(DateTime.Now.Add(20) > dt) && !isRead) 
     { 
      string helpVar = ReadPartOfFileOrBuffer(); 
      SB.Append(helpVar); 

      isRead = checkIfFileOrBufferIsRead(); 
     }; 

     if(!isRead) throw new Exception("Read timed out"); 

     return SB.ToString(); 
    } 

    public voic Commit(string s) 
    { 
     //This is not being executed! 
    } 

    public void Do() 
    { 
     string result = ObtainString(); 
     Commit(result); 
    } 
+0

なぜdownvote? – Tatranskymedved

関連する問題