25 March, 2009

WPF: InvokeRequired = !Dispatcher.CheckAccess()

Update (05.03.2011):
Since this post, .NET 4 has arrived. The <Control>.Dispatcher.CheckAccess() is now made available in Intellisense (in VS).  In addition, an easier code construct than the initial version, is the below (note: ‘btnSplit’ is a Button in this case):

private void HandleUIButtons()
if (!btnSplit.Dispatcher.CheckAccess())
//if here - we are on a different non-UI thread
btnSplit.Dispatcher.BeginInvoke(new Action(HandleUIButtons));
btnSplit.IsEnabled = true; //this is ultimately run on the UI-thread


Original post:
Doing some multithreading coding in WPF – I again today stumbled upon the missing InvokeRequired functionality known from WinForms coding. The InvokeRequired bool property on a control that is used when doing mulithreaded WinForms coding, has been replaced by the Dispatcher.CheckAccess() method call in WPF.

Be aware that the old property (InvokeRequired) returned true if the calling thread was different from the owning thread; whereas the Dispatcher.CheckAccess() methodcall returns true if the calling thread is the SAME as the owning thread. In other words – the response is reversed so to speak.

Note: The Dispathcer.CheckAccess() method does NOT show up in Visual Studio due to a System.ComponentModel.EditorBrowsableState.Never method attribute in the source code from Microsofts hand!

void PopulatePage()
//check whether the calling thread is the thread owning the txtDBConn control
if (!txtDBConn.Dispatcher.CheckAccess())
var popAct = new Action(PopulatePage);
txtDBConn.Dispatcher.BeginInvoke(popAct, System.Windows.Threading.DispatcherPriority.DataBind, null);

this.txtDBConn.Text = ConfigurationManager.ConnectionStrings[Constants.Database.DB_CONN].ConnectionString;

13 March, 2009

Manually remove a windows service

I just experienced a problem uninstalling a Windows Service so I had to remove it manually; it is quite easy though.

Go to Registry: HKEY_Local_Machine\System\CurrentControlSet\Services
and delete the key with your service name.

Restart the computer afterwards.

iPhone/XCode - not all cases are equal!

This bit me! Having made some changes to an iPhone application (Obj-C); everything worked fine in the simulator. But, when deploying the s...