This project is read-only.

Async Task Manager documentation

  • Getting started
private readonly EventAggregator _eventAggregator;
public MainViewModel()
{
    _eventAggregator = (EventAggregator)ServiceLocator.Instance.GetService(typeof(IEventAggregator));
    TaskManager = new TaskManagerViewModel(_eventAggregator);
}
<Window ...
        xmlns:views="clr-namespace:TaskManager.View;assembly=TaskManager">

     ...
     <views:TaskManagerView DataContext="{Binding TaskManager}" />
     ...

</Window>
  • Create a simple task
/// <summary>
/// Start a new simple task
/// </summary>
public void StartTask()
{
    Func<object> beginAction = () =>
                                    {
                                        return "OK !!!";
                                    };

    Action<Task<object>> endAction = (res) =>
                                            {
                                                Text = res.Result as string;
                                            };

    var task = new TaskViewModel("Simple task !", beginAction, endAction);

    if (_eventAggregator != null)
        _eventAggregator.GetEvent<TaskEvent>().Publish(new TaskEventArgs(TaskEventArgsType.Started, task));
}
  • Create a repetitive task
/// <summary>
/// Start a new repetitive task
/// </summary>
public void StartTask()
{
    if (_eventAggregator == null)
        return;

    var task = new TaskViewModel("Tick task !", null, null, null, false);

    Func<object> beginAction = () =>
    {
        Thread.Sleep(250);
        return true;
    };

    Action<Task<object>> endAction = () =>
    {
        Text = "Ok";
    };

    task.BeginAction = beginAction;
    task.EndAction = endAction;

    _eventAggregator.GetEvent<TaskEvent>().Publish(new TaskEventArgs(TaskEventArgsType.Started, task));

    var max = 50;
    for (var i = 0; i < max; i++)
    {
        _eventAggregator.GetEvent<TaskEvent>().Publish(
            new TaskEventArgs(TaskEventArgsType.Updated, task.Id, (i * 100) / max));
    }

    _eventAggregator.GetEvent<TaskEvent>().Publish(
                new TaskEventArgs(TaskEventArgsType.Ended, task.Id));
}

Last edited Feb 24, 2013 at 9:23 AM by lordinaire, version 7

Comments

No comments yet.