Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Main
- {
- void test()
- {
- TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext();
- var runTask = new RunTask();
- var task = runTask.CreateTask(doSomething, scheduler)
- .ContinueWith<bool>(..., scheduler);
- task.Wait();
- }
- }
- public class RunTask
- {
- public RunTask()
- {
- }
- public Task CreateTask(Action action, TaskScheduler scheduler)
- {
- _scheduler = scheduler;
- return Task.Factory.StartNew(() => executeAction(action), CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
- }
- private void executeAction(Action action)
- {
- var t1 = Task.Factory
- .StartNew(action)
- .ContinueWith<Task>(mainTaskCompleted);
- var t2 = Task.Factory
- .StartNew<bool>(waitMainTask)
- .ContinueWith(progressTaskCompleted, _scheduler);//progressTaskCompleted - show and hide progress operation in UI
- var tasks = new[] {t1, t2};
- Task.WaitAll(tasks);
- if (t1.Result.IsFaulted)
- {
- throw t1.Result.Exception;
- }
- }
- private bool waitMainTask()
- {
- var showProgress = false;
- //...calculate working time
- return showProgress;
- }
- private void progressTaskCompleted()
- {//not step here with _scheduler
- //Show and Hide IPopup window
- }
- }
- public Task CreateTask(Action action, TaskScheduler scheduler)
- {
- // I'm assuming progressTaskStarted does UI stuff to show progress,
- // and progressTaskCompleted hides it when the work is done.
- return Task.Factory.StartNew(progressTaskStarted)
- .ContinueWith(ignoredTask => action(), TaskCreationOptions.LongRunning)
- .ContinueWith(ignoredTask => progressTaskCompleted());
- }
Add Comment
Please, Sign In to add comment