Sunday, October 25, 2009

[.NET] How to check if app is already running

Here's a quick and dirty snippet (and sample usage) to check if your app is already running (using a Mutex).
static class Program
{
private static Mutex mutex;

public static bool IsThisProcessAlreadyRunning()
{
bool createdNew;
mutex = new Mutex(false, Application.ProductName, out createdNew);
return !createdNew;
}


///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
if (!IsThisProcessAlreadyRunning())
{
// whatever you might be doing
// here's some default form initialization
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
{
// do something!
// usually smt like --> BringOnFront();
}
}

}
Bringing the app to the foreground is usually what you'll want to do next in scenarios like this. A post about that might follow soon enough.

Side note - if you're using VB.NET I believe there is a check-box you can tick under application properties somewhere that does the job. I'll avoid making jokes about VB.NET 'cause life is tough enough for the suckers who use it without me being a jerk.

EDIT: 2009-11-01 --> posted How to Bring Window Upfront

Thursday, October 15, 2009

[.NET] XmlDocument VS XmlReader

To cut a long story short I recently learned (thanks to a co-worker) that XmlDocument performance sucks.

It's very handy to manipulate XML stuff with XmlDocument because it comes with all the DOM stuff, and for example you can use XPath for node selection like this:
// load up the xml doc
XmlDocument doc = new XmlDocument();
doc.Load(filename);

// get a list of all the nodes you need with xPath
XmlNodeList nodeList = doc.SelectNodes("root/whatever/nestedWhatever");
The above is quite cool and it works just fine if you're not loading hundreds of files for a total size of hundreds of MBs, in which case you'll notice a lethal blow to performance.

If you need speed you wanna go with XmlReader, a bare bones class that will scan the old way (forward only) element after element your XML file. Bad thing is that you won't have all the nice DOM stuff, so you'll have to manually parse elements retrieving innerText and/or attribute values. An example:
XmlReader xmlReader = XmlReader.Create(fileName);

while (xmlReader.Read())
{
//keep reading until we see my element
if (xmlReader.Name.Equals("myElementName") && (xmlReader.NodeType == XmlNodeType.Element))
{
// get attributes (or innerText) from the Xml element here
string whatever = xmlReader.GetAttribute("whatever");
// do stuff
}
}
I can't be bothered benchmarking as it bores the sh*t out of me - but performance increases a whole lot with XmlReader, and if you want figures to look at you can find plenty on google (this guy here did a pretty good job for example) and here's another good overview of XML classes (from Scott Hanselman's blog).

Anyway - here comes the common sense advice - whatever you're doing go with XmlDocument, if it's too slow for you needs switch over to XmlReader and you'll be grand.

Saturday, October 3, 2009

GIT + GITHUB --> How to setup local repository

Once the code is up on Github (for example if you need to pull your buddy's code from github 'cause he's too cool for SVN), after you install any git package for windows it's quite easy to setup a local repository for git and pull your stuff from github.

Browse to your local repository from gitbash and this is how it's done in just 3 lines:
//init git local repo
git init

//register github user + repo
git remote add origin git@github.com:UsEr/repOsitOry.git

//pull the stuff
git pull origin master
In order to be able to talk to the github servers from your machine you need to setup the ssh stuff, which I am not covering because I can't be bothered and you can read all about it here.

Thursday, October 1, 2009

How to mimic SQL WHERE clause on Google Datastore queries

This puzzled me for a while as I never used JDO before crashing the app-engine party.

Assuming we have an Order class with a member variable named customer of type User:

//get current user
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();

//prepare query
PersistenceManager pm = PMF.get().getPersistenceManager();
String select_query = "select from " + Order.class.getName();
Query query = pm.newQuery(select_query);
query.setFilter("customer == paramCustomer");
query.declareParameters("com.google.appengine.api.users.User paramCustomer");

//execute query
List<OrderDTO> result= OrderUtils.getOrderDTOList((List<Order>) query.execute(user));

//feck-off --> I'll never get an MVP but I can swear on my blog!
pm.close();

Just wanted to share to spare some random googler (you) the same process.