English 中文(简体)
Weird acting loop in C#
原标题:

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
问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签