Home > Blockchain >  C# Issue while trying to use System.Threading.Timer
C# Issue while trying to use System.Threading.Timer

Time:10-15

My issue could be a simple one but I can't manage to find the solution after dozens of tries

So I have a function that is iterating through files,directories and subdirectories recursively of a path that I set as a parameters, inside that function I have a counter that is called cpt and every time he find a file he simple increment it by 1.

Inside that function I would like to monitor the number of file added to that counter every 30 seconds because sometimes the drectories are huge and these numbers will be added for statistics analysis.

So every 30 seconds I would like to run another function that will simply send that number to a PHP api using WebClient.


I tried many ways to solve this problem without breaking the counter function or without blocking the function itself but with no success, I will add code so you guys can maybe give new ideas to that problem

Code of the recursive function :

    int recursive(string path= "M:\\files\\data",int cpt = 0)
    {
        string[] files = Directory.GetFiles(path);
        string[] dirs = Directory.GetDirectories(path);
        
        foreach (var file in files)
        {
            try
            {
                cpt  ;
            }
            catch { }
        }

        foreach (string directory in dirs)
        {
            try
            {
                cpt = hello(directory, cpt);
            }
            catch { }
        }

        return cpt;

    }

Code of the function that should be runned every 30 seconds :

    void postResult(int cpt)
    {
        
        var data = new System.Collections.Specialized.NameValueCollection
        {
            ["result_nb"] = cpt.ToString(),
        };

        using (WebClient wc = new WebClient())
        {
            wc.UploadValues("http://127.0.0.1/work/analysis.php", data);
        }
        
        MessageBox.Show(cpt.ToString());
    }

Code that I tried inside the recursive function to run the postResult function every 30 seconds :

        TimerCallback timerCallback = new TimerCallback(postResult(cpt));

        System.Threading.Timer timer = new System.Threading.Timer(timerCallback, null, 1000, 15000);

This code gives me error : "Method Name expected" || here => new TimerCallback(postResult(cpt))

So apparently with timers I can't pass the counter parameters.

CodePudding user response:

So

int cpt = 0; System.Threading.Timer tmr; 
void Init()
{
    tmr = new (postResult, null, 30000, 30000);
    recursive();
    tmr.Dispose();
}
void recursive(string path = "M:\\files\\data")
{
    //cpt  = Directory.GetFiles(path).Length;
    foreach (string file in Directory.GetFiles(path))
    {
        try
        {
            cpt  ;
        }
        catch { }
    }
    foreach (string directory in Directory.GetDirectories(path))
    {
        try
        {
            recursive(directory);
        }
        catch { }
    }
}
void postResult(object? o)
{
    //... 
}

CodePudding user response:

You may try declare your cpt as static so both recursive and postResult can access it

CodePudding user response:

There is no point in having the cpt as a parameter for the recursive function. You could honestly just have it as a static value, that you can access outside the function scope as well as inside.

  •  Tags:  
  • c#
  • Related