Monthly Archives: August 2009

Cinch does ok

I have just been notified my Cinch MVVM article won the best overall article of the month for July 2009 over at www.codeproject.com which is obviously very nice.

The article is question can be located right here, if you fancy a gander

http://www.codeproject.com/KB/WPF/CinchII.aspx

Advertisements

A Look At Tasks In The System.Threading.Tasks VS2010 Namespace

I had a bit of time today to look at the System.Threading.Tasks VS2010 Namespace, and I started throwing a bit of code together and wrote an article about which you can read over at

http://www.codeproject.com/KB/threads/TPLTasks.aspx

But if you just want a quick overview, what the Tasks in the System.Threading.Tasks VS2010 Namespace are all about (from what I read anyhow), is a replacement for queuing work items up using the currently available ThreadPool. The issue with the current ThreadPool is that

  • Work items can’t return a value
  • Can not cancel tasks
  • No waiting on work items
  • Composing a group of items in a structured way
  • Handling exceptions thrown concurrently or any other richer construct built on top of it

 

So Tasks are there to help out in this area. Let’s have a quick look at how to do some common things such as

 

Run A Task That Returns A Value

   1:     /// <summary>
   2:          /// This handler shows you to use a Task that
   3:          /// doesn't use any state inside the Task
   4:          /// 
   5:          /// This Task blocks the calling thread until the 
   6:          /// Task completes
   7:          /// </summary>
   8:          private void TaskFactWithReturnValue_Click(object sender, 
   9:              RoutedEventArgs e)
  10:          {
  11:              Func<ObservableCollection<String>> obtainTaskResults 
  12:                  = TaskWithResults;
  13:              Task<ObservableCollection<String>> task =
  14:                  Task.Factory.StartNew<ObservableCollection<String>>
  15:                  (obtainTaskResults,
  16:                  TaskCreationOptions.DetachedFromParent);
  17:              items.Clear();
  18:              task.Wait(); //Blocks while waiting for Task to complete
  19:              items = task.Result;
  20:              lstItems.ItemsSource = items;
  21:              MessageBox.Show("TaskFactWithReturnValue DONE");
  22:          }
  23:   
  24:          /// <summary>
  25:          /// Runs the Tasks Func delegate, which returns 
  26:          /// a list of ObservableCollection String
  27:          /// </summary>
  28:          private ObservableCollection<String> TaskWithResults()
  29:          {
  30:              ObservableCollection<String> results = 
  31:                  new ObservableCollection<string>();
  32:              for (int i = 0; i < 10; i++)
  33:              {
  34:                  results.Add(String.Format(
  35:                      "TaskFactWithReturnValue Item {0}", i));
  36:              }
  37:              return results;
  38:   
  39:          }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

And we can even chain tasks together using a technique called Continuations, here is an example of that:

   1:         /// <summary>
   2:          /// This handler shows you to use a Task that will 
   3:          /// use 2 chained Tasks the first Task accepts some
   4:          /// State, and the 2nd Task in the chain doesn't
   5:          /// ONLY when both Tasks have completed is the work 
   6:          /// considered done
   7:          /// 
   8:          /// This Task blocks the calling thread until the 
   9:          /// 2 chained Task complete
  10:          /// </summary>
  11:          private void TaskContinue_Click(object sender, 
  12:              RoutedEventArgs e)
  13:          {
  14:              //SYNTAX EXAMPLE 1
  15:              #region SYNTAX EXAMPLE 1
  16:              Func<Object, ObservableCollection<String>> 
  17:                  obtainTaskResultsFunc = 
  18:                  TaskWithResultsWithState;
  19:              Task<ObservableCollection<String>> task =
  20:                  Task.Factory.StartNew(obtainTaskResultsFunc, 
  21:                  new ObjectState<Int32>
  22:                  {
  23:                      CurrentTask = "TaskState",
  24:                      Value = 2
  25:                  });
  26:   
  27:              Func<Task, ObservableCollection<String>> 
  28:                  contineResultsFunc = ContinueResults;
  29:              Task<ObservableCollection<String>>
  30:                  continuationTask =
  31:                  task.ContinueWith<ObservableCollection<String>>
  32:                  (contineResultsFunc,
  33:                  TaskContinuationOptions.OnlyOnRanToCompletion);
  34:   
  35:              continuationTask.Wait();
  36:              items.Clear();
  37:              items = continuationTask.Result;
  38:              #endregion
  39:   
  40:              //SYNTAX EXAMPLE 2
  41:              #region SYNTAX EXAMPLE 2
  42:              //Task<ObservableCollection<String>> taskAll =
  43:              //    Task.Factory.StartNew((o) =>
  44:              //        {
  45:              //            return TaskWithResultsWithState(o);
  46:              //        }, new ObjectState<Int32>
  47:              //            {
  48:              //                CurrentTask = "TaskState",
  49:              //                Value = 2
  50:              //            }).ContinueWith<ObservableCollection
  51:              //              <String>>((previousTask) =>
  52:              //                {
  53:              //                    return ContinueResults(
  54:              //                      previousTask);
  55:              //                },TaskContinuationOptions.
  56:              //                  OnlyOnRanToCompletion);
  57:   
  58:   
  59:              //taskAll.Wait(); 
  60:              //items.Clear();
  61:              //items = taskAll.Result;
  62:              #endregion
  63:   
  64:   
  65:              lstItems.ItemsSource = items;
  66:              MessageBox.Show("TaskContinue DONE");
  67:          }
  68:   
  69:   
  70:   
  71:          /// <summary>
  72:          /// Runs the Tasks Func delegate, which returns a list
  73:          /// of ObservableCollection String
  74:          /// </summary>
  75:          private ObservableCollection<String> 
  76:              TaskWithResultsWithState(Object o)
  77:          {
  78:              var state = o as ObjectState<Int32>;
  79:              ObservableCollection<String> results = 
  80:                  new ObservableCollection<string>();
  81:              for (int i = 0; i < state.Value; i++)
  82:              {
  83:                  results.Add(String.Format("{0} Item {1}", 
  84:                      state.CurrentTask, i));
  85:              }
  86:              return results;
  87:   
  88:          }
  89:   
  90:          /// <summary>
  91:          /// Runs the Contination Tasks Func delegate, 
  92:          /// which returns a list
  93:          /// of ObservableCollection String
  94:          /// </summary>
  95:          private ObservableCollection<String> 
  96:              ContinueResults(Task originalTask)
  97:          {
  98:   
  99:              Task<ObservableCollection<String>> taskOriginal 
 100:                  = originalTask as Task<ObservableCollection<String>>;
 101:   
 102:              ObservableCollection<String> results = 
 103:                  new ObservableCollection<string>();
 104:   
 105:              ObservableCollection<String> orginalResults =
 106:                  taskOriginal.Result as 
 107:                  ObservableCollection<String>;
 108:   
 109:              foreach (var item in orginalResults)
 110:                  results.Add(item);
 111:   
 112:              for (int i = 0; i < 10; i++)
 113:                  results.Add(
 114:                      String.Format(
 115:                      "ContinueResults Item {0}", i));
 116:   
 117:              return results;
 118:          }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

As I say I go through all this a lot more in my article, which is available at the following Url should you want to know more:

 

http://www.codeproject.com/KB/threads/TPLTasks.aspx

Cinch (My MVVM framework) Part 6 is out

I just published part VI of my MVVM framework series over at http://www.codeproject.com .

Here is the link

http://www.codeproject.com/KB/WPF/CinchVI.aspx

 

This time I talk about

What the demo app looks like, and pointing you at all the relevant parts that cover the demo app, as discussed in all the previous Cinch articles.

 

Enjoy

Cinch (My MVVM framework) Part 5 is out

I just published part III of my MVVM framework series over at http://www.codeproject.com .

Here is the link

http://www.codeproject.com/KB/WPF/CinchV.aspx

 

This time I talk about

Unit testing using the Cinch framework

 

Enjoy

Cinch (My MVVM framework) Part 4 is out

I just published part III of my MVVM framework series over at http://www.codeproject.com .

Here is the link

http://www.codeproject.com/KB/WPF/CinchIV.aspx

 

This time I talk about

Developing Models Using Cinch
Developing ViewModels Using Cinch

    * Choosing a ViewModel Base Class
    * ViewModelBase Class
    * How To Use The Exposed Services
          o EventLogger service
          o MessageBox service
          o Open File service
          o Save File service
          o Popup window service
    * Background Tasks
    * Mode Support

 

Enjoy