2013-01-22 4 views
1

파일을보고 더 이상 grwoning하지 않는 경우를 찾고 싶습니다. 나는 외부 프로그램지연된 작업으로 파일이 계속 커지고 있는지 확인하십시오.

Process.Start(
    "procdump.exe", 
    string.Format(@"-ma {0} Output\{1}_RAW_DUMP.dump", 
    processName, 
    dt.ToString("yyyyMMdd-HHmmss"))); 

을 실행하고 나는이 과정이 그 일을 완료 할 때 알아야합니다.

private void CheckDumpFile(DateTime startDateTime, IConfigHolder configHolder, List<string> results) 
{ 
    var path = CheckExistensDumpFile(startDateTime); 
    const int delayMs = 250; 
    if (path == null) 
    { 
     Console.WriteLine("Dumpfile not ready yet, next try in 0.25s, now: {0}", DateTime.Now.ToString("HH:mm:ss.fff")); 
     RetryAction(() => CheckDumpFile(startDateTime, configHolder, results), delayMs); 
    } 
    else 
    { 
     var fileInfo = new FileInfo(path); 
     if (fileInfo.Length == 0) 
     { 
      Console.WriteLine("Dumpfile has no Length yet, now: {0}", DateTime.Now.ToString("HH:mm:ss.fff")); 
      RetryAction(() => CheckDumpFile(startDateTime, configHolder, results), delayMs); 
     } 
     else 
     { 
      if (_lastLength == fileInfo.Length) 
      { 
       Console.WriteLine("Dumpfile is " + _lastLength + "bytes, starting analysis, now: {0}", DateTime.Now.ToString("HH:mm:ss.fff")); 
       ReadDumpFile(configHolder, path, results); 
      } 
      else 
      { 
       Console.WriteLine("Dumpfile is still growing, next try in 0.25s, now: {0}", DateTime.Now.ToString("HH:mm:ss.fff")); 
       _lastLength = fileInfo.Length; 
       RetryAction(() => CheckDumpFile(startDateTime, configHolder, results), delayMs); 
      } 
     } 
    } 
} 

그리고이

public static void RetryAction(Action action, int delayMs) 
{ 
    new Timer(obj => action(), null, delayMs, Timeout.Infinite); 
} 

이 파일이 둔화 grwoing 될 때까지 작동하고 나는 'RetryAction'의 몇 전화를 가지고 :

그래서 나는 이것을 썼다. 하나의 전화가 돌아 오지 않습니다.

문제가 무엇인지 압니까? 제 경우에는 더 나은 해결책이 있습니까? 누군가 FileWatcher가 networkwork share에서 끔찍하다고 이야기했기 때문에 FilWatcher를 무시했습니다.

+0

확실한 점은 각 호출마다 새 타이머를 인스턴스화해서는 안됩니다. ['Timer.Change'] (http://msdn.microsoft.com/en-us/library/system.threading.timer.change (v = vs.100) .aspx)를 사용하여 대신 재설정하십시오. 상위 컨테이너/컨트롤이 처리 할 때 폐기하십시오. – Groo

+0

파일이 사용 중인지 테스트하고 싶습니까? – NoviceProgrammer

+0

실제로 일어날 일을 설명해 주시겠습니까? 쉽게 재현 할 수없는 경우 코드 주위에 추적 메시지를 추가하고 질문에 출력을 게시하십시오. 또한 백그라운드 스레드에서 예외가 발생하여 손실 될 수 있으므로 'Application.ThreadException + = (s, e) => Console.WriteLine (e.Exception.ToString());'과 같은 것을 추가 할 수 있습니다. 앱의 시작 부분. – Groo

답변

0

이 문제도 발생했습니다. 마지막 쓰기 시간을 확인하는 메소드를 작성 했으므로 파일이 1 바이트 씩 증가하더라도 여전히 사용중인 것으로 분류됩니다.

/// <summary> 
    /// Waits for completion of writing to a file. 
    /// </summary> 
    /// <param name="fullPath">The full path.</param> 
    /// <param name="timeToWait">The time to wait on each attempt.</param> 
    /// <param name="retryAttempts">The maximum number of retry attempts.</param> 
    /// <param name="safetyBuffer">The safety buffer.</param> 
    public static void WaitForFile(string fullPath, TimeSpan timeToWait, int retryAttempts, TimeSpan safetyBuffer) 
    { 
     var timesSkipped = 0; 
     var info = new FileInfo(fullPath); 
     var maxWriteTime = DateTime.Now.Add(-safetyBuffer);// no activity for a minute 
     while (info.Exists && (info.LastWriteTime > maxWriteTime) && timesSkipped < retryAttempts) { 
      Thread.Sleep(timeToWait); 
      maxWriteTime = DateTime.Now.Add(-safetyBuffer); 
      info.Refresh(); 
      timesSkipped++; 
     } 
    } 
+1

"안전 버퍼"는 timespan IMHO에 대한 약간의 가난한 이름 선택입니다. :-) – Groo

관련 문제