views:

161

answers:

1

Note: I added actual code snippets. Just scroll to end.

// 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"...
}

Function is called in another loop. I ran the code a few times and thought it was working well until I saw that one time it acted weird. I provided the above function with an array length of which was 6, and the expected output was like this:

-------------------------
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...
-------------------------

However, the output I got was like that:

-------------------------
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...
-------------------------

When I saw that output I quit the execution before the current loop was over (it runs for a very long time.)

It looks like the function is working correctly (It runs files.Length times and outputs results after that.) However, the argument passed to the function is somehow faulty (The function is interestingly called more than once. Normally, it should run for only one time in this case. I mean, the number of lines in a script file determine the number of times above function is called, and the script file contains only one line.) That argument (files array) comes from a OpenFileDialog, which means I have nothing to do with it. I just pass the array to the function.

I'm still trying to understand the reason for such a strange outcome. This only happened one time, but I still have to diagnose the problem; because, I will leave the program running maybe for a couple of days. It should work correctly.

Do you have any ideas about this nonsense?


Actual code of above function:

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

Caller function code:

    // 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();
        }
    }

Script file:

# 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
+4  A: 

It seems like something is calling the method more often than you expect.

Put a breakpoint on the first line of the method, and see when and why it's being called. The bug is almost bound to be in the calling code rather than the method itself, which means we can't really help much more than suggesting things like breakpoints, logging stack traces etc.

Jon Skeet
I did what you say but the problem did not occur for a second time since then. Also there is only one place where the above function is called. It drives me crazy.
blahbaa
So put logging in at that calling place (with appropriate information which will tell you why it's calling it) - then next time it goes wrong, you'll get more information.
Jon Skeet
Logging is a good idea but I am not sure how to do it. I am really confused. I'll try that now, but again I have to wait for lots of hours to see the outcome which sucks.
blahbaa