0

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.

2
  • You need to name a method that should be called for the Timer constructor. That method is called by the timer not you. The timer can't know what value to give to that method (it does not now anything about your ctp integer). So write a method that retrieves the ctp value from somewhere and use that as TimerCallback. An easy way would be 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. Commented Oct 14, 2022 at 15:25
  • @Ralf when trying your code and simple writing MessageBox.Show inside the postResult function as a try, I get like 4 MessageBox per seconds with random numbers, not even growing numbers, that's why I though the recursive function is a problem right there, you could try the code yourself in your environment you will see the behavior of the code Commented Oct 14, 2022 at 15:51

3 Answers 3

1

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) { //... } 
Sign up to request clarification or add additional context in comments.

3 Comments

I can't modidy the structure of the recursive file as you did because i need to add things to do for each file and each directories, please keep the recursive function as it was and update your code + the cpt is not passed into postResult as a parameters so it can't work either
Done as requested
Working perfectly, thanks. I just had to replace "tmr = new (postResult, null, 30000, 30000);" by "tmr = new System.Threading.Timer(postResult, null, 30000, 30000);" and "object? o" by "object o" since i'm running .NET framework
1

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.