0
List<System.Timers.Timer> myTimers = new List<System.Timers.Timer>();
    
private void startFunction()
{    
    for (var parameter = 0; parameter < list.Count; parameter++)
    {
        System.Timers.Timer timer = new System.Timers.Timer(TimeSpan.FromMinutes(10).TotalMilliseconds);

        timer.Elapsed += new ElapsedEventHandler(someFunction);
        timer.Start();
        myTimers.Add(timer);
    }
}

// without the int parameter this code works 
public void someFunction(object sender, ElapsedEventArgs e, int parameter) 
{
    // some code that have to run over time
}

So on the someFunction method I used a parameter (int) and without it, this is working out fine. I just want to send a parameter through this function and with searching online and trying different methods it still does not work.

Anyone having knowledge on how to send a parameter using the function someFunction every 10 minutes?

Craig
  • 2,248
  • 1
  • 19
  • 23
Ando
  • 19
  • 6
  • you can't, the event simply has only the eventargs available. How would you imagine the timer to infer the value for that parameter - it has no clue that param even exists. How is that param populated? Where do you set it and where do you use it? I suppose it has nothing to do with the timer at all. – MakePeaceGreatAgain Mar 28 '22 at 12:11
  • 1
    Where does the value of this parameter come from? – canton7 Mar 28 '22 at 12:15
  • I left that out because I didn't want to write to much code in this example to be more clarified but it seems to be mistake – Ando Mar 28 '22 at 12:19
  • 2
    In that case, just do `timer.Elasped += (o, e) => someFunction(o, e, parameter);`. The lambda you supply to `Elapsed` captures the variable `parameter`, and will then pass it to `someFunction` when it's invoked – canton7 Mar 28 '22 at 12:25
  • better way could be encapsule the timer and your parameter as some class. – Lei Yang Mar 29 '22 at 00:51

1 Answers1

1

Since parameter is just a local variable, you can create a lambda which captures this variable, and passes it to your someFunction method.

private void startFunction()
{    
    for (var parameter = 0; parameter < list.Count; parameter++)
    {
        var timer = new System.Timers.Timer(TimeSpan.FromMinutes((10)).TotalMilliseconds);

        int parameterCopy = parameter;
        timer.Elapsed += (o, e) => someFunction(o, e, parameterCopy);
        timer.Start();
        myTimers.Add(timer);
    }
}

The parameterCopy is required because of this issue.

canton7
  • 37,633
  • 3
  • 64
  • 77
  • This sadly didn't work out for me – Ando Mar 28 '22 at 12:35
  • @Ando Why not? It probably means that there's critical information missing from your question, but we need to understand what that is before we can help – canton7 Mar 28 '22 at 12:35
  • maybe the important information can be that the code above someFunction is also in their own function. And that int parameter is from a list.Count and have to be different for every item in that list.. – Ando Mar 28 '22 at 12:39
  • @Ando Well it's a simple local variable in your question, and there's no "also in their own function", so could you fix your question please? I answered the question as asked – canton7 Mar 28 '22 at 12:41
  • @Ando Thanks. It's currently not valid C# however -- it's not possible to nest a `public void` method inside another method. Can you make sure your question actually contains your real code please? – canton7 Mar 28 '22 at 12:51
  • minor mistakes while editing – Ando Mar 28 '22 at 12:56
  • @Ando Right, but nested functions in C# *are* a thing, so it's not clear whether the mistake is the nesting, or the presence of the `public` – canton7 Mar 28 '22 at 12:58
  • @Ando See my edit – canton7 Mar 28 '22 at 12:59
  • 1
    excuse me, I did another test and your method seems to work indeed! thank you! – Ando Mar 29 '22 at 06:48