November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
Call .Wait() instead, or .Result if the Task yields a result (and you are interested in that result).
Call Wait() instead, or .Result if the Task yields a result (and you are interested in that result).
That works, but I was wondering if there is any way to run the scheduled jobs asynchronously? This task takes about 3 minutes to complete, so having it block for that long does not seem ideal. I may be fundamentally misunderstanding how the scheduled jobs work - perhaps there would be no performance difference using async / await?
Please use a bit advanced version (keep in mind exceptions that might occour once anync "comes" back):
public static class AsyncHelper { private static readonly TaskFactory _myTaskFactory = new TaskFactory(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default); public static TResult RunSync<TResult>(Func<Task<TResult>> func) { return _myTaskFactory.StartNew(func) .Unwrap() .GetAwaiter() .GetResult(); } public static void RunSync(Func<Task> func) { _myTaskFactory.StartNew(func) .Unwrap() .GetAwaiter() .GetResult(); } }
as to performance penalty - think case here (might be wrong) is that this thread will be occupied with idling while waiting for the results from async method. other scheduled jobs should run normally on scheduled basis..
What is the best way to use asynchronous code in a scheduled job? I'm currently using the following code, but it seems to me that there should be a better way to do this than using "RunSynchronously()"
Edit: This code doesn't actually work, since it results in "System.InvalidOperationException: RunSynchronously may not be called on a task not bound to a delegate, such as the task returned from an asynchronous method." I could use other approaches to get the code to run synchronously, or add a synchronous version of the UpdateFundBalances() method, but that's not really the point.