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.
Task<bool>
for that?false
.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
}
}
After a time of experimenting I found a solution via aborting threads. I know its not recommended, but it works fine and does its job for me.
public class Program
{
public static void Main(string[] args)
{
Task.Run(() =>
{
if (!ExecuteTaskWithTimeLimit(1000, () => { return DoStuff1(); })) return;
if (!ExecuteTaskWithTimeLimit(1000, () => { return DoStuff2(); })) return;
// ...
});
}
public static bool ExecuteTaskWithTimeLimit(int timeLimit_milliseconds, Func<bool> codeBlock)
{
Thread thread = null;
Exception exception = null;
// do work
Task<bool> task = Task<bool>.Factory.StartNew(() =>
{
thread = Thread.CurrentThread;
try
{
return codeBlock();
}
catch (Exception e)
{
exception = e;
return false;
}
});
// wait for the task to complete or the maximum allowed time
task.Wait(timeLimit_milliseconds);
// if task is canceled: show message and return false
if (!task.IsCompleted && thread != null && thread.IsAlive)
{
ConsoleWriteLine("Task cancled because it timed out after " + timeLimit_milliseconds + "ms.");
thread.Abort();
return false;
}
// if error occured: show message and return false
if (exception != null)
{
MessageBox.Show(exception.Message, "Exeption", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
// if task has no error and is not canceled: return its return value
return task.Result;
}
}