Archive for the 'Tech Stuff' Category

Q: How can I stop BitmapImage, in .NET Windows Presentation Foundation (WPF) from locking the source image file?

 

A: By default BitmapInfo seems to lock the source image file so that you can use the bitmapinfo object and modify or delete the source file at the same (or similar) time. For example, this C# will probably yield a locked image file:

 
var bitmap = new BitmapImage(new Uri(imageFilePath));
// use bitmap…
 

You can get around this locking problem as follows, it’s a little bit more long-winded but it worked for us….

 
var bitmap = new BitmapImage();
var stream = File.OpenRead(imageFilePath);
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
stream.Close();
stream.Dispose();
// Use bitmap…..
 

.NET’s Threading module isn’t the best, it has the look of a model that was designed by the type of software people who like to keep practical multi-threading a good long arm’s length away. It’s workable but sometimes it requires that you jump through hoops to do simple everyday things with threads – like updating a UI element from a non UI thread etc. You may see errors akin to the following:

 

“Must create DependencySource on same Thread as the DependencyObject.”

“This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.”

To get around this all a .NET developer needs to do is to remember a few ritualistic looking invocations to appease the .NET small gods – but of course I can never remember the details so am recording one such here:

 

To dispatch to the UI from a non-UI thread and non-UI code, use something like the following:

 
using System.Windows;

Application.Current.Dispatcher.Invoke(new Action (() => Thumbnails.Add(thumbnail)));

 

In WPF when you try to assign a bitmap to a property that’s bound to a UI element from a worker thread you may get the following error:

 

“Must create DependencySource on same Thread as the DependencyObject”

 

A simple way around this problem without having to jump through hoops and dispatch to the UI thread is to just freeze the bitmap before assigning it, like this:

 
var bitmap = new BitmapImage(new Uri(CurrentImageFilePath));
bitmap.Freeze();
this.CurrentImage = bitmap;
 

I hope this helps someone who’s stuck with this annoying error!

 

A Google App Engine cloud application that we have been involved in developing suddenly started getting snowed under by a storm of GAE’s most excellent DeadlineExceededError exceptions.

 

The app never really suffered from this before (unless a rogue task actually took too long that is) but suddenly these errors started happening on a huge number of requests during the module load or _ah/warmup phases, they didn’t even get to run any of our code before they were killed with this most beautiful of error messages. It literally brought the app to its knees…

 

For example, we kept finding stuff like this in the logs:

 
class ‘google.appengine.runtime.DeadlineExceededError’>:
Traceback (most recent call last):
File “/base/data/home/apps/appname/9993.355980156233423494/warmup.py”, line 1, in module>
import appengine_config # Cache by import
File “/base/data/home/apps/appname/9993.355980156233423494/appengine_config.py”, line 27, in module>
….
 

After a little research it seems that this has started happening to loads of GAE users since December 2011, have a look at this thread:

 

http://groups.google.com/group/google-appengine/browse_thread/thread/369a9a76c394c99e/976722e37ad07d0c

 

Loads of annoyed people out there having the same type of problem. The general recommendation was to move to a high replication datastore (rather than master/slave), this move is not always simple however and I believe will incur higher costs!

 

To cut a long story short, our app was dead in the water so we had no choice other than to migrate it to a high replication datastore and hope that this fixed the problem (and hope that the migration didn’t create too many new problems). Once we carried out the migration the DeadlineExceededError excpetions went away, so that was good but we were a bit peeved that our hand was forced in such a way.

 

So I don’t know what google changed but it seems that they want people on high replication or else! Once again we find ourselves seriously questioning whether google app engine is a sutibaly stable platform for deploying real-world apps – the jury is still out…

 

Anyway if you find yourselves with the same problem they you may be headed the HRD way!

 

If you get the following error when using devel’s dvm() in drupal 7 (or indeed, if you see it when not using dvm()):

 
Notice: Undefined variable: output in drupal_var_export()

Then try putting this line into you settings.php file:

 
ini_set(‘error_reporting’, ‘E_ALL ^ E_NOTICE’);
 

It worked for us – thanks to citytree for the fix!

 

The method for Programmatically setting a link field value (see the Link module) in drupal 7 is slightly different from how it was set it in drupal 6. Here is some code for creating a new node and setting its link field (called field_url):

 
$node = new stdClass();
$node->type = ‘website’;
node_object_prepare($node);

$node->title = $site_name;
$node->language = LANGUAGE_NONE;

$node->field_url[$node->language][0]['title'] = “www.ridgesolutions.ie”;
$node->field_url[$node->language][0]['url'] = “www.ridgesolutions.ie”;

node_submit($node);
node_save($node);

 

Here’s a decent article that I found about programmatically creating content in drupal 7:

 

www.group42.ca/creating_and_updating_nodes_programmatically_in_drupal_7

 

If you are hooking magento up to realex for payment on your eCommerce website then you will most likely be using the payments module kindly provided by StudioForty9 in Cork.

 

As part of configuring this you will need to tell realex what your cart’s ‘return URL’ is, this URL has changed since the introduction of the newer version of this module (v2.0) which it seems is for use with magneto v1.5 and greater. The new URL format for use with the newer module is:

 
http://www.yourdomain.com/realex/response/
 

In case you are still using the old module, the url format for that is:

 
http://www.yourdomain.com/Realex/standard/success
 

I was having some trouble getting Visual Studio 2010 to stop at breakpoints in some unmanaged C++ code that’s in a C++/CLI project that I am working on, and as we all know Software Developments rapidly stops being fun if your debugger is broken!

 

I am using C++/CLI as a thin wrapper around this vanilla C++ functionality so that I could export it to .NET. All was working fine except that the breakpoints in the C++ code did not work. After some searching I found this post:

 

http://rhnatiuk.wordpress.com/2010/11/13/managednative-debugging-in-cc

 

The post suggested that I should set the ‘Enable unmanaged code Debugging’ setting of the start-up project (not the C++/CLI project), so I enabled it and the brakepoints started working fine – so big thanks Roman for the tip!

 

Here is a very handy article that explains how you can alter your app’s setup project in Visual studio to install an Event Log Component so that your non-service app, when installed, can write to the windows event log without getting security errors:

 

http://msdn.microsoft.com/en-us/library/f5dcf6h3(v=vs.80).aspx