Thursday, September 11, 2014

An Advantage of Using Current SynchronizationContext over Control.BeginInvoke

SynchronizationContext vs Control.BeginInvoke


Windows Forms and WPF framework do not allow modifications to UI elements and controls from a non-UI thread. So if a task thread wants to display a result on the UI or change a control to indicate state of some sort it has to somehow communicate with the UI thread and the UI thread should take care of displaying the UI updates, carry out the desired change to UI controls, etc.

There are two to accomplish this:

  • Capturing the SynchronizationContext as in the code snippet below which is assumed to part of a method in child class of the System.Windows.Form class:
      Task.Factory.StartNew(() =>
      {
        // update UI here
      }, this.cancelToken.Token
       , TaskCreationOptions.None
       , TaskScheduler.FromCurrentSynchronizationContext()
     );
    

  • Using Control.BeginInvok as in the code snippet below which also assumes to be with a child class of the the System.Windows.Form class:
    Task.Factory.StartNew(() =>
    {
        DoSomeWrok();
        Action updateUIDelegate = () =>{ UpdateUI();}; 
        /* ^^ a way of creating a delegate to be passed to BeginInvoke */
        this.BeginInvoke(updateUIDelegate); //will be execute on the UI thread.
        /* ^ this refers to the Form instance */
    }, this.cancelToken.Token
    );
    

    Notice in the above code we did not need to capture the current SynchronizationContext.

One Reason to Prefer Capturing Current SynchronizationContext:


Control.BeginInvok is not available in WPF. So if you are creating classes to be used in both Windows Forms and WPF then using SynchronizationContext is the portable way. Here is more from http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx:
We now have two different APIs for achieving the same basic operation, so how do I write my component to be agnostic of the UI framework?  By using SynchronizationContext.  SynchronizationContext provides a virtual Post method; this method simply takes a delegate and runs it wherever, whenever, and however the SynchronizationContext implementation deems fit.  Windows Forms provides the WindowsFormSynchronizationContext type which overrides Post to call Control.BeginInvoke.  WPF provides the DispatcherSynchronizationContext type which overrides Post to call Dispatcher.BeginInvoke.  And so on.  As such, I can now code my component to use SynchronizationContext instead of tying it to a specific framework.
I was curios how WindowsFormsSynchronizationContext mentioned in the article mentioned above implemented the Post method so I took a look at the code (reflected):


public override void Post(SendOrPostCallback d, object state)
{
 if (this.controlToSendTo != null)
 {
  this.controlToSendTo.BeginInvoke(d, new object[]
  {
   state
  });
 }
}

Tuesday, September 9, 2014

Rendering Partial View from C#

Rendering Partial View from Inside a Controller Action in C#:

Sometimes I find a need to a rendered HTML string from inside an action (a response to AJAX call for example).

The RenderPartialView function below assumes it's a member method of a class derived from the System.Web.Mvc.Controller class.

Function description: Renders the partial view and returns the generated Html. I think it's easier than returning the raw data and having jQuery generates the HTML. Params: partialViewFullPath - The path and extension to the partial view. for example, ~/Views/Controller/ViewName.cshtml    model - Almost always we expect a model Returns: HTML string generated by the the MVC framework itself.

   
public string RenderPartialView(string partialViewFullPath, object model){
    /* to avoid null ref exception when unit testing */
    if (HttpContext == null || HttpContext.Request == null)
    {
        return string.Empty;
    }
    if (string.IsNullOrEmpty(partialViewFullPath))
    {
        throw new ArgumentException("partialViewName");
    }
    ViewDataDictionary viewData = new ViewDataDictionary(model);
    var razorView = new RazorView(this.ControllerContext, partialViewFullPath
    , null, false, new string[] { "cshtml" });
    var sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        using (HtmlTextWriter tw = new HtmlTextWriter(sw))
        {
            ViewContext viewContext = new ViewContext(this.ControllerContext
                            ,razorView,viewData, this.TempData, tw);
            razorView.Render(viewContext, tw);
        }
    }
    return sb.ToString();
}


The RenderPartialView function can be called as the following example code shows

        
[HttpPost]
public ActionResult SomeAction(int id)
{
    ///...
    var result = new AjaxReturnObjectModel();
    result.Success = true;
    result.Html = RenderPartialView("~/Views/Controller/PartialView.cshtml", model);
    return Json(result, JsonRequestBehavior.DenyGet);
}

Hope this helps.