2009-11-14 5 views
1

참고 : 실제 코드 조각을 추가했습니다. 끝까지 스크롤하십시오.이상한 연기 루프 C#

// files is created by a OpenFileDialog. 
public void Function(String[] files, ...) 
{ 
    for(int i; i<files.Length; i++) 
    { 
     WriteLine("File " + i + "/" + files.Length + " being processed."); 
     //... processing for a long time and printing information to console ... 
    } 

    //... print results, e.g.: "Results: bla bla"... 
} 

함수가 다른 루프에서 호출됩니다. 나는 코드를 몇 번 실행했고 그 때가 이상하게 작동 할 때까지 이 잘 작동한다고 생각했습니다.

------------------------- 
File 0/6 being processed. 
...lots of output... 
File 1/6 being processed. 
...lots of output... 
File 2/6 being processed. 
...lots of output... 
File 3/6 being processed. 
...lots of output... 
File 4/6 being processed. 
...lots of output... 
File 5/6 being processed. 
...lots of output... 

Results: bla bla... 
------------------------- 

그러나 I 얻은 출력은 그와 같았다 : I는 6이었다의 배열 길이가 상기 기능 를 제공하고, 예상 결과는 다음과 같았다

------------------------- 
File 0/1 being processed. 
...lots of output... 

Results: bla bla... 

File 0/3 being processed. 
...lots of output... 
File 1/3 being processed. 
...lots of output... 
File 2/3 being processed. 
...lots of output... 

Results: bla bla... 

File 0/3 being processed. 
...lots of output... 
File 1/3 being processed. 
...lots of output... 
File 2/3 being processed. 
...lots of output... 

Results: bla bla... 

File 0/6 being processed. 
...lots of output... 
File 1/6 being processed. 
...lots of output... 
File 2/6 being processed. 
...lots of output... 
File 3/6 being processed. 
...lots of output... 
------------------------- 

I 톱 그 출력은 현재 루프가 끝나기 전에 실행을 종료합니다 (매우 오랫동안 을 실행합니다).

함수가 올바르게 작동하고있는 것 같습니다 (이후에는 files.Length 시간이 실행되고 결과는 출력됩니다). 그러나 , arg 함수에 전달 된 인수는 어떻게 든 잘못되었다 (이 함수는 흥미롭게도 한 번 이상 호출된다. 일반적으로이 경우에는 한 번만 실행해야합니다. 즉, 스크립트 파일의 줄 수는 함수가 호출되는 횟수를 결정하며 스크립트 파일에는 한 줄만 포함됩니다.)이 인수 (파일 배열)는 OpenFileDialog에서 가져옵니다. 즉, 그것. 방금 배열을 함수에 전달합니다.

나는 아직도 그런 이상한 결과의 이유를 이해하려고 노력하고 있습니다. 이것은 한 번만 발생했지만 문제를 진단해야합니다. 왜냐하면, 나는 프로그램을 며칠 동안 운영하는 상태로 둘 것입니다. 제대로 작동해야합니다.

이 넌센스에 대한 의견이 있으십니까?


위의 함수의 실제 코드 :

public String Start(String[] files, StreamWriter reportWriter) 
{ 
    List<SortedDictionary<int, SortedDictionary<long, int>>>[] allResults 
     = new List<SortedDictionary<int,SortedDictionary<long,int>>>[files.Length]; 
    List<SortedDictionary<int, SortedDictionary<long, int>>> results; 
    Simulation_DenemePositionEstimator p; 
    Simulation_WimaxStreamReader reader; 
    String ret; 

    for (int i = 0; i < files.Length; i++) 
    { 
     System.Console.WriteLine("File " + (i+1) + "/" + files.Length + " being processed."); 
     reader = new Simulation_WimaxStreamReader(grids, new StreamReader(files[i])); 
     p = new Simulation_DenemePositionEstimator(grids, reader); 
     // Using parameters in script file which were saved into 
     // different variables when Simulation instance was created. 
     results = 
      p.StartInvestigation(maxRssiDiff, maxCinrDiff, maxAvgTxPwrDiff, 
       maxUncontinuity, radiusForNeighbors, expansionFactor, increment, 
       n, numberOfIterations, resetCountForPositioning); 
     allResults[i] = results; 
     reader.Close(); 
    } 

    ret = Statistics(allResults); 
    System.Console.WriteLine(ret); 
    reportWriter.WriteLine(ret); 
    reportWriter.Flush(); 

    return ret; 
} 

발신자 기능 코드 :

// read a line from script file. 
    while((line = reader.ReadLine()) != null) 
    { 
     // line starting with # is comment. 
     if (line.StartsWith("#") == false) 
     { 
      // save parameters retrieved from script file into an array. 
      values = line.Split(delimiters); 
      // new Simulation instance with new parameters 
      sim = new Simulation(map, values); 
      // Start simulation. scenarioFiles comes from OpenFileDialog. 
      report = sim.Start(scenarioFiles, reportWriter); 
      //reportWriter.WriteLine(report); 
      reportWriter.WriteLine("---------------NEW-PARAMETERS---------------"); 
      reportWriter.Flush(); 
     } 
    } 

스크립트 파일 :

# Horizontal grid count 
# Vertical grid count 
# maxRssiDiff is the maximum RSSI difference allowed. 
# maxCinrDiff is the maximum CINR difference allowed. 
# maxAvgTxPwrDiff is the maximum AvgTxPwr difference allowed. 
# maxUncontinuity 
# radiusForNeighbors 
# expansionFactor 
# increment 
# n -> MT'den gelen kaç değerin ortalaması alınıp yer bulma algoritmasına girdi olarak verilsin? 
# Algoritma kaç adımda bir sonuçları dosyaya yazsın? 
# Kaç adımdan sonra yer bulma işlemine sıfırdan başlamış gibi devam etsin? 
# 
# Örnek: 
# 118 90 4 3 4 2 1 1 1 3 10 100 
118 90 6 4 6 2 1 1 1 3 250 500 
# 200 140 4 3 4 2 1 1 1 3 10 100 
+0

어딘가에 루프가 있습니다. 게시 한 코드로는 코드를 재현하기에는 충분하지 않습니다. 두려워요. 스레드로 재미있게하고 있습니까? 사기성 "포획"과 관련이있을 수 있습니까? –

+0

이 함수를 호출하는 코드와 같은 세부 정보를 더 추가해야합니다. 또한 잠재적으로 프로세싱의 - 잠재적으로 비동기로 처리되는 프로세싱입니까? –

+0

스레드를 사용하고 있지 않습니다. 나는 단순히 파일을 읽고 계산을하고있다. 곧 코드를 게시하겠습니다. – blahbaa

답변

4

그것은 뭔가 것 같아 메서드를 예상보다 자주 호출하고 있습니다.

메서드의 첫 번째 줄에 중단 점을 넣고 호출되는시기와 이유를 확인합니다. 버그는 메서드 자체가 아니라 호출 코드에있을 가능성이 거의 큽니다. 즉, 중단 점, 스택 추적 등을 제안하는 것보다 훨씬 많은 도움을 줄 수는 없습니다.

+0

나는 당신이 한 말을했지만 문제는 그 이후로 두 번째로 발생하지 않았다. 또한 위 함수가 호출되는 곳은 단 하나입니다. 나를 미치게 해. – blahbaa

+0

그래서 호출 장소에 로그인하십시오. (적절한 정보가있어 호출하는 이유를 알려줄 것입니다.) 다음 번에 잘못되면 더 많은 정보를 얻을 수 있습니다. –

+0

로깅은 좋은 생각이지만 어떻게해야할지 모르겠습니다. 나는 정말로 혼란 스럽다. 나는 지금 그것을 시도 할 것이다. 그러나 다시 나는 빨기 결과를보기 위해 많은 시간을 기다려야한다. – blahbaa