garyshort.org


I am a Technical Evangelist for Developer Express, my work blog is here but this one is more fun. :-)

July 2005 Entries

Mutil-Threading and Updating the GUI with C# 2.0

Okay, we all know updating a GUI from another thread is not good practice, but now in .Net 2.0 if you try it the Framework throws an exception. So, how do you do it then? Well I'll show you. First off, I use the MVC pattern so this example is a little more complex than it needs to be, but that's just tough luck I'm afraid. :) First thing to do is to create a delegate in your view class to hold a method that will actually do the updating of the view, like so... public delegate void TreeViewUpdateDelegate(string nodeName, string nodeDisplayString); Then create the method that will acutally do the updating of the view, again do this in the view class, like so... public void SetTreeNodeDisplayText(string nodeName, string nodeDisplayString) { foreach (TreeNode tn in _SearchServiceTreeView.Nodes) { if (tn.Name == nodeName) { tn.Text = nodeDisplayString; } } } After that, create a property, in the view class, to hold the delegate as below... private TreeViewUpdateDelegate _UpdateDelegate; public TreeViewUpdateDelegate UpdateDelegate { get { return _UpdateDelegate; } set { _UpdateDelegate = value; } } Next, in your controller class, when you create the instance of the view, set the property as well, like so... _MainView.UpdateDelegate = new MainView.TreeViewUpdateDelegate( _MainView.SetTreeNodeDisplayText); Now, create a delegate to hold the method that will be executed on the thread like this... delegate void SearchWorkerDelegate(); Then, in your controller class, spin off a thread to do some work and designate a callback method to execute when the thread has completed, as below... SearchWorkerDelegate SearchWorker = this.DoGoogleSearchWorker; SearchWorker.BeginInvoke(this.OnGoogleSearchComplete, null); Finally, in the callback method, call the invoke method to update the GUI on the thread on which it was created... string[] stringParams = new string[2]; if (AppModel.Instance.GoogleSearchResult != null) { stringParams[0] = "_GoogleSearchNode"; stringParams[1] = "Google " + "( " + AppModel.Instance.GoogleSearchResult. resultElements.Length.ToString() + " hits)"; _MainView.Invoke(_MainView.UpdateDelegate, stringParams); } else { stringParams[0] = "_GoogleSearchNode"; stringParams[1] = "Google"; _MainView.Invoke(_MainView.UpdateDelegate, stringParams); } You see, that was nice and easy now wasn't it :)

You’ve got to be Kidding

Over on his site, James Robertson goes through the process of creating an exe file from VisualWave Smalltalk. No one would like to see Smalltalk become a mainstream language more than me, but guys, you gotta sort this out before this can happen. No dev team is going to go through that pain on a daily basis.

The Joys of Pet Ownership

Arwen, our German Wiredhaired Pointer likes to go swimming in the sea. The downside of that is that she has picked up and eye infection from the water. Have you ever tried to put drops in a big dog's eyes when she doesn't want you too? :)

RE: New Windows XP Theme (Energy Blue) Available…

There's a really nice new XP theme out from Microsoft New Zealand. It's based on a media pc theme and it's lovely :) [Via Tim Sneath]

Has J.K. Rowling Lost Her Magic?

Well i finished the HBP over the week-end. My opinion? Not as good as the first four books, but not as bad as OOTP. Can't wait for the final installment. In the meantime though there's the film version of GOF to look forward to :)

Harry Potter and the Half Blood Prince

Yip, I got my copy midnight Friday. See you all when it's finished :)

Static or Dynamic Typing

Over on his blog, James Robertson continues the seemingly endless meme of static versus dynamic typing. I learned to program in Smalltalk around 15 years ago and still use it for my hobby projects. However, I earn my living as a C# programmer so I know the arguments from both sides. Just for the record I prefer Smalltalk's dynamic typing to C#'s static typing, I just haven't come across the problems in Smalltalk that the static typing advocates say dynamic typing will lead to. What I have come across though is the verbose way that static typing makes me code. Take this code snippet for example... void FilterSelectionViewFilterButton_Click(object sender, EventArgs e) { //GS - Get the selected filter type Button b = sender as Button; FilterSelectionView fsv = b.FindForm() as FilterSelectionView; ComboBox cb = fsv.FilterSelectionViewComboBox; AppModel.FilterType ft = (AppModel.FilterType)cb.SelectedItem; //GS - Store the selected filter type AppModel.Instance.CurrentFilterType = ft; //GS - Close the view b.FindForm().Close(); //GS - Process the current filter type ProcessCurrentFilterType(); } The above code is an event handler for a button click running in a controller class. The line that get's on my nerves is FilterSelectionView fsv = b.FindForm() as FilterSelectionView; I declare a variable of type FilterSelectionView then I assign the result of the FindForm() method to it. Now FindForm() returns a System.Windows.Forms.Form and since FilterSelectionView is a subclass of System.Windows.Forms.Form you'd think you'd be able to assign it to the variable fsv, but no you can't, you have to cast it, you have to add the code as FilterSelectionView. Oh joy! If I had a pound for every time I'd written that I could retire :)

Microsoft Seeks to Seed the Shareware Market

At the Shareware Industry Conference in Denver this week, Microsoft officials will be talking up the new Microsoft Shareware Starter Kit. [Via Microsoft Watch from Mary Jo Foley] I've used this starter kit for my "Code you way to the PDC" entry and it's pretty good. :)

Computer Degree or not?

Chris talks here about the the fact that software engineering is not the same as computer science. He goes on to ask what universities should do about their courses. I'd add that the trouble with deciding that degree courses are not relevant and taking professional courses, (MCSD etc), instead, is that you are then left without a degree, which is a show stopper for many jobs and certainly for working abroad, especially in the US. Oops! :)

Lies, Damn Lies and Holywood

James Robertson touches on one of my pet hates here. That is when when films and TV programs portray historical events, but then change the facts for dramatic effect. The absolute worst example of this must the film U571

Microsoft’s Claria Rating Change a Coincidence?

Microsoft officials are claiming the rumored acquisition of Claria and change in Claria's anti-spyware status are completely coincidental. [Via Microsoft Watch from Mary Jo Foley] Sure it is -Ed.

Flurry of Security Patches

It's been a hectic day on the security patching front. Microsoft's bulletins for July include patches for three critical vulnerabilities on the same day that Mozilla releases new security updates for Firefox and Thunderbird. Not to be left behind, Apple fixed two Tiger flaws while Oracle issued a critical database server update. [Via Slashdot: ]

We are not afraid

Check out this website that has captured the mood in the UK and else where after the bombings.

Microsoft Eyes Mom ‘n’ Pop Market

Microsoft thinks there's a lot of money to be made in the small-business software market. There is, and most of it's being made by smaller companies that, until now, considered themselves business partners of the Redmond behemoth. [Via Wired News] This is bad news for ISVs :(

Twitter Updates


    Follow me! :-)
    www.flickr.com
    GaryShort's photos More of GaryShort's photos