0
votes

I have a certain Func<bool> func in C# (.NET Framework 4.8) in a WPF application and want it to be executed. I'd like to have a method that takes this kind of Funcs and returns a bool.

  1. It should run in the background and not block the UI thread. Probably I need Task<bool> for that?
  2. If it takes longer than a certain timeout limit, it should be canceled and return false.
  3. The program should wait for the task to be completed but not wait for the full time limit if it is already finished earlier.
  4. If it runs into an error, error message should be printed, the task should be canceled and the program should not crash or freeze.

Is there any sophisticated method that fullfills these requirements? The solution can also use Task<bool> instead of Func<bool> if this a better solution. It should be usable in a way similar to this:

public class Program
{
    public static void Main(string[] args)
    {
        bool result = ExecuteFuncWithTimeLimit(3000, () =>
        {
            // some code here of a specific task / function
        });
    }

    public static bool ExecuteFuncWithTimeLimit(int timeLimit_milliseconds, Func<bool> codeBlock)
    {
        // run func/task in background so GUI is not freezed
        // if completed in time: return result of codeBlock
        // if calceled due to time limit: return false
        // if error occured: print error message and return false
    }
}
1
When you say "canceled", do you mean aborted?Theodor Zoulias

1 Answers

-1
votes

1: yes you need asynchronus because you want to avoid freez ui maybe use Task you can checked timeer by a feild start and end of method and use try-cache for avoid crash you can check time in catch block and print message and you can call method for canceling program after that use continue key word in catch block.