McKelt.com

Remembering Thoughts

 

Recent comments

Authors

Categories


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

WPF UI Thread Dispatcher

A simple implemention for calling asych methods from the UI

 

Examples

 

1.

dispatcher.ExecuteOnMainUIThread(CommandManager.InvalidateRequerySuggested);

 

2.

dispatcher.Execute(() =>
{
SomeLongRunningMethodHere();
});

 

The interface

 
using System;

namespace Mvvm
{
public interface IDispatcher
{
void Execute(Action action);

void ExecuteOnMainUIThread(Action action);
}
}

Synchronous for use in Testing

 
using System;

namespace Mvvm
{
public class SynchronousDispatcher : IDispatcher
{
public void Execute(Action action)
{
action();
}

public void ExecuteOnMainUIThread(Action action)
{
action();
}
}

}

Asynchronous for use by the application at run time

 
using System;

namespace Mvvm
{
using System.Windows;
using System.Windows.Threading;

public class AsynchronousDispatcher : IDispatcher
{
public void Execute(Action action)
{
action.BeginInvoke(CallBack, action);
}

public void ExecuteOnMainUIThread(Action action)
{
Dispatcher dispatcher;

if (Application.Current != null)
{
dispatcher = Application.Current.Dispatcher;
}
else
{
dispatcher = Dispatcher.CurrentDispatcher;
}

dispatcher.Invoke(action);
}

private void CallBack(IAsyncResult result)
{
try
{
((Action)result.AsyncState).EndInvoke(result);
}
catch (Exception ex)
{
// Need to raise the exception on the main thread
ExecuteOnMainUIThread(() =>
{
throw ex;
}
);
}
finally
{
result.AsyncWaitHandle.Close();
}


}
}
}


Categories: .Net
Posted by chris on Wednesday, May 26, 2010 12:15 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Rhino Mock Constraints -- AssertWasCalled

Rhino Mock Constraints allow use to test a methods parameters were called with the correct arguments.

public interface IDocumentService
{
void Save(string userName, Document document, Stream stream);
}
 
 

Some ways to ensure the method that contains the save method passes the correct internally constructed arguments include
 
documentService.AssertWasCalled(
a=>a.Save("chris", doc, adaptor.InputStream),               
b => b.Constraints(Is.Equal(“chris”), Is.NotNull(), Is.AnyThing()));
 
 
Passing in Property.AllPropertiesMatch(this.MyTestObjectWithPropertiesThatShouldMatch)
will check values against each object
 


Posted by Chris on Thursday, May 20, 2010 5:55 PM
Permalink | Comments (0) | Post RSSRSS comment feed

CFA – Accounting Ratios

Liquidity ratios

 

\mbox{Current ratio} = \frac {\mbox{Current Assets}} {\mbox{Current Liabilities}}

 

 

\mbox{Quick (Acid Test) Ratio} = {\mbox{Cash and Cash Equivalent} + \mbox{Marketable Securities} + \mbox{Accounts Receivable}\over \mbox{Current Liabilities}}

Cash ratio is the same as Quick without the accounts receivable

 

 

Solvency ratios

 

Long term debt to equity =  total debt / total equity

 

Debt to equity = total debt / total equity

 

Total debt ratio = total debt / total assets

 

Financial leverage ratio = total assets / total equity


Categories: CFA
Posted by chris on Saturday, May 08, 2010 11:42 AM
Permalink | Comments (1) | Post RSSRSS comment feed