YosiFZ
YosiFZ

Reputation: 7890

C# perform method after delay

I want to create a method that receives a method as a parameter and delay var. And execute the method after delay:

    public static void performSelectorAfterDelay<T>(Action<T> method, T parameter, double delay)
    {
        Thread thread = new Thread(delegate()
        {
            Thread.Sleep(TimeSpan.FromSeconds(delay));
            uc.BeginInvoke((Action)(() => method(parameter)));
        });
        thread.Start();
    }

I want it to be Generic so i will be able to call it for these two methods:

    public void funcA()
    {

    }

    public void funcB(int num)
    {

    }

When i call it:

performSelectorAfterDelay(new Action(funcA), null, kSecondsWaitAfterTransferToFollowersPage);
performSelectorAfterDelay(new Action(funcB), 5, kSecondsWaitAfterTransferToFollowersPage);

I get this error:

The type arguments for method performSelectorAfterDelay(System.Action, T, double)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Any idea what can be the problem?

Upvotes: 0

Views: 91

Answers (2)

djdd87
djdd87

Reputation: 68456

The compiler can't work out the type, because you're passing null as the 2nd parameter.

performSelectorAfterDelay(new Action(funcA), null, kSecondsWaitAfterTransferToFollowersPage);

Anyway, after you resolve that issue you will end up with another error, because funcB takes an int, so it cannot be passed into new Action because Action requires a parameterless method.

You may be able to get it working using Action<object> as per the other answer, but that will mean you can't leave funcA without a parameter. However, this solution seems redundant to me unless I'm misunderstanding something. You can just pass an actual Action that calls funcA/funcB with their parameters like so:

public void performSelectorAfterDelay(Action method, double delay)
{
    Thread thread = new Thread(delegate ()
    {
        Thread.Sleep(TimeSpan.FromSeconds(delay));
        method();
    });
    thread.Start();
}

public void test()
{
    performSelectorAfterDelay(() => funcA(), kSecondsWaitAfterTransferToFollowersPage);
    performSelectorAfterDelay(() => funcB(1), kSecondsWaitAfterTransferToFollowersPage);
}

Upvotes: 1

koviroli
koviroli

Reputation: 1453

The error message tells something. Here is the fixed version sir:

    public static void funcA(object obj)
    {
    }

    public static void funcB(int num)
    {
    }

    public static void performSelectorAfterDelay<T>(Action<T> method, T parameter, double delay)
    {
        Thread thread = new Thread(delegate()
        {
            Thread.Sleep(TimeSpan.FromSeconds(delay));
            uc.BeginInvoke((Action)(() => method(parameter)));
        });
        thread.Start();
    }

    public static void SomeCall()
    {
        performSelectorAfterDelay(new Action<object>(funcA), null, kSecondsWaitAfterTransferToFollowersPage);
        performSelectorAfterDelay(new Action<int>(funcB), 5, kSecondsWaitAfterTransferToFollowersPage);
    }

Upvotes: 0

Related Questions