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.
new System.Threading.Timer((_) => postResult(cpt), null, 1000, 15000);If your recursive method has anything todo with your problem tell us how. If not remove it from the question.