| Chris Skardon 的个人资料Chris Skardon日志列表 | 帮助 |
|
|
2007年6月 Generic Parameterized ThreadsI do quite a bit of multi-threading at the moment, which a long with all the inherent problems (concurrency, complexity etc) makes for some very interesting work. With .NET 2.0 we were gifted with the ParameterizedThreadStart delegate for us to use, which saved a lot of work-arounds involving member variables (and in one case - an intricate queuing system). One thing still puzzles me though - why didn't Microsoft didn't make the ParameterizedThreadStart generic, instead of taking just an object as an argument. The ParameterizedThreadStart delegate is defined as so: public delegate void ParameterizedThreadStart(object obj); This is used when you want to create a new thread and pass an argument to it, (in itself, a big step up from the .NET 1.1 days, when all we had was 'ThreadStart'). The problem is, this now adds an element of un-compile-time-safety to the code, I can pass anything, and will only know about it at run-time. I guess the ideal solution to this from my point of view would be something like: public delegate void ParameterizedThreadStart<T>(T arg); Which would allow me to then call the method I want to start on a new thread type-safely. So, lets start back at the beginning, how do we use the current ParameterizedThreadStart? I have the following method that I want to run in a new thread: private void Inty(int anInt) It's a very simple method, that writes out a message from a for loop, and sleeps for a set time. For me to use this with the current ParameterizedThreadStart I would do this: Thread t = new Thread(new ParameterizedThreadStart(Inty)); Which also requires a change to the 'Inty' method: private void Inty(object obj) Which means I lose all my typesafety, as I can now call my thread such: t.Start("HULLO!"); All valid compile-wise, not good at run-time, as a consequence, I should add some validity checks into 'Inty'. But I just would rather not have to do this. This brought me to write a wrapper for the Thread class, I called it 'Thread' (but in a different namespace), so the code would look the same; First I needed a generic ParameterizedThreadStart delegate: public delegate void ParameterizedThreadStart<T>(T argument); Now I've got that, it's time to create a wrapper, we can't extend System.Threading.Thread as it's a sealed class, so wrapping is our lot. The idea is that I want to be able to create my new thread, and then call 'Start' on it just as a normal thread. My solution is below;
public class Thread<T> I wrap a System.Threading.Thread which I construct and initialise in the constructor. The ParameterizedThreadStart<> which I pass in to the constructor I store as a member variable, ready to be run in the private 'Run' method. The Run method is a match to a standard ParameterizedThreadStart delegate, and performs a cast to the correct type. This cast is safe, as the whole class is made 'generic'. So, to use the new Thread class: Thread<int> t = new Thread<int>(new ParameterizedThreadStart<int>(Inty)); I don't have to make any changes to 'Inty' which is nice, and the compiler will complain if I try to call 'start' with any value other than an int. Equally, if I try to set up the ParameterizedThreadStart<> with anything other than an int the compiler will complain. The only thing I don't really like is the way I have to put 'int' 3 times in that constructing row, I would prefer something like: Thread t = new Thread(new ParameterizedThreadStart<int>(Inty)); but, for type safety I can live with it. 2007年6月 That towel trick...Hrumph! Not effective in my case... The ol' XBox is packed up and ready for shipping to MS' base in the UK. More weeks wait for Forza! 2007年6月 XBox 360 Red Lights of Death...So, here we are, my second xbox 360 has died now, though this time (due to the support line being closed) I perused the masses of online stuff about it and came across a 'Towel' trick - well, to be honest, my girlfriend remembered reading an article in the Guardian. As a result I googled for 'PacoDG towel' and found many-a-page, but in particular an entry by PacoDG on XBox 360 Rally. Currently the xbox is under the towels, so to speak - 30 seconds to go... 2007年6月 First Laptop post!Huzzah! For the first time ever I finally own a laptop! A Sony VAIO VGN-C290, a nice dual core pc ready for my development trials! Hopefully I'll be able to get a lot more stuff done now and write to this blog more, though we'll have to see how that pans out :) 2007年6月 Synchronizing with external changes...Has anyone else seen this message? It appears when go back to my Visual Studio instance containing my (admittedly too large) solution of 170 odd projects (or maybe it's not too large??). The mouse cursor goes into 'busy' mode and I basically can't do anything with visual studio (it goes into 'Not Responding' mode - hence the pale look of the screen shot). I use Sourcegear Vault as my source provider, and also run 'Resharper 2.5' and the SQL 2005 addins in this instance. It's really frustrating, and I haven't seen anything online about this, i.e. I have yet to find anyone else who has discussed it. A couple of my colleagues here do suffer from the same problem, so at least I know it's not isolated to me. Anyone else suffer from this pain? TypeLoadExceptionsOccasionally we get TypeLoadExceptions from .NET code, you can try all manner of programs to try to figure out what the problem is - 'Dependency Walker' is always a good start - but I would recommend before even going down that route -
Always, and I mean always clean the solution (right-click -> clean) and then rebuild if you get a TypeLoadException. This will rule out a large number of problems. Clearly this isn't the only way to get this exception, but it's a good thing to check first. 2007年5月 WinForm Panels and the Designer - GRRRRRI've inherited some user interface (UI) code created by another developer a while ago, and it's a pretty standard affair - you know the sort - functional. It doesn't look pretty, but it does what it says on the tin and well, that's all it needs to do. Anyhews, one of the 'core' features of the UI is how a 'set' of controls (nicely wrapped in a panel) are displayed based on the selection of a drop-down box. Now, one of the inherent troubles with this design is the way these panels are displayed - one behind the other. This makes for interesting times when using the WinForms Designer. Specifically what I had done was move one of the panels (we'll call 'A') to see the panel behind it (sticking with convention this shall be 'B'). After doing the editing I needed to, I dragged the panel A back over the B, ensuring the positioning was dead centre - easy to do with the VS2005 designer. So, time to start the form, ahh. Panel A is visible, B is invisible, no matter what the combobox says. Curiously, it doesn't show anything at all, just a blank panel. This isn't good - I didn't really want to have to undo all my changes. Needless to say - a long drawn out procedure of checking my code, running through the debugger began. Yes, the panel was having it's visibility changed:
but, stepping through the debugger, the actual value wasn't changing... Why not??!!? Ok, has my code made it gone wrong? - lets try seperating the panels, and putting them next to each other -> start debugging -> works fine... hmmm, ok retry dragging B back into position. Debug. @#$%!! Not there again. What's going on??! Then suddenly it hits. It is me, when I've been dragging the panels, I've been dragging panel B not over panel A, but into panel A. Panel B was actually inside Panel A. Whooops. I guess because the size of the two panels was exactly the same I couldn't see the fact that B was inside A. The solution - ensure that neither was in the other, then set the Location property of both to be the same, rather than drag it. Nuts. 2007年5月 Orcas ExperiencesI've been tinkering with Visual Studio 2007 - "Orcas" now for a couple of weeks, a little bit here, a little bit there, when I have a spare moment at work - to keep up with new technologies etc. I have to say, I'm impressed, the IDE is as per normal, excellent, and the new features of C# (LINQ, Extension methods etc) are integrated nicely. I've been basing my tinkering on Eric White's - Composition using Functional Programming Techniques in C# 3.0 tutorial, which has been excellent, and a great way to get into LINQ. LINQ has been my main focus whilst experimenting, as it seems (to me at least) to be one of the biggest things in C# 3.0, and if I'm right about it, it will prove to be useful from a work perspective pretty much as soon as we get the RTM build of VS 2007 - we do a lot of querying of XML data, and anything to make that more intuitive would be great. I'm also greatly interested in getting my hands dirty with WCF, again we do a lot of communicating in the system I'm working on, predominantly using Remoting, but also MSMQ based. It would be nice if we could remove a lot of the code we have which currently sets up the tranmission protocols, channels etc. 2007年5月 .NET 3.5 Installation IssuesRecently I've downloaded the 'Orcas' Beta 1 release of Visual Studio, so I can play around with .NET 3.5 and the new Visual Studio. I chose to install this on a VPC, good practice and all! So I setup a Vista Enterprise VPC, from scratch, got it all setup, added it to my companies domain and then set about installing Orcas. Then I ran into trouble, I kept on getting an error when attempting to install:
I searched through many a google result, many a 'Live search' result, but to no avail, I found lots of people who had, had different problems installing, but no-one had mentioned my problem... The question really was; Which service wasn't running? Obviously this would have been easier if the error message had actually mentioned this! I found a lot of people saying that they needed to install the vista updates from the Orcas CD, so I tried to do this, but again got the 'Service cannot be started' message. WHAT SERVICE!!!! Right, ok, these are 'updates' to vista, so the liklihood is that it's in the Windows Update, so I can at least give that a go... Start -> Windows Update, Error! This isn't good - have I damaged Vista in some way??? Best hope not... Fortunately this time I got an error code - ( though admittedly I've forgotten it here :( ) - however the help on Vista found the error code perfectly, and stated that the Windows Update service was stopped. Ok, restart it then - Services -> Windows Update Service -> Disabled... Disabled? Marvellous, then it suddenly clicks into place, presumably this is the service that .NET 3.5 and Orcas have been complaining about, and the service is disabled as a policy of the company. Right, so remove the VPC from the domain, and stick it into a workgroup, reboot, Windows Update - YAY - we're back in business. Now running the Orcas setup it appears to be running, and yes, literally as I just typed this the 3.5 framework has been installed, so fingers crossed the rest will work just fine. 2007年5月 DotNetRocksI've known about DotNetRocks (a podcast 'radio' for .NET developers) for a couple of years now, but I always thought - naaaa, that would just be too geeky and dull! Then this weekend just past, I was flying up to Edinburgh (Scotland) and wanted something non-music to listen to on the old mp3 player, so decided to download a recent DNR show featuring a guy called Jeff Atwood (author of a great blog called Coding Horror which I read pretty much daily), and now I'm a convert! I keep a small collection of the shows on the mp3 player, and listen to them on the trains back and forth to work. It's hard to describe what they're like without listening yourself, but in a nutshell, I'd say it was two .NET gurus - Richard Campbell and Carl Franklin, who clearly know what they're talking about, and this makes the interviews much better, as they ask the questions you might not have thought to ask about, and the interview, general commentry is good, clear and at times funny :) I think if you're a .NET developer you should give DNR a go and see how it goes - worst case, you get 20 minutes in - think this isn't for me and stop the show, or you never know - you might just fire up your browser and download the next show, and then the next and ne.... 2007年5月 Support / Software Engineer?I've only worked in 2 companies in my long history of employment as an IT professional, the first was as a support engineer for a large oil company in Aberdeen (Scotland), and my current - a software engineer for a small finance company. In both companies there has been a development team, and an infrastructure team, with the former developing things, and the latter supporting all manner of things - exchange servers down to printers and other prehipherals, but in the smaller firm, the lines are far more blurred. I spend quite a bit of my time helping others, looking at printers etc (maybe in part to my previous experience), generally because if there isn't a support engineer to hand, then people quite naturally ask me, or another member of the development team, (As a side note, someone once did actually ask one of my colleagues to plug in a Lamp for him, she did it, probably due to being too shocked that someone would actually ask for that to be done.) The blurryness is quite good, and I think all devs would benefit from a bit of support engineering. Quite often we can do things (accidently), or need things set up that we really should be able to do ourselves. Other benefits include actually being able to know the other side of the development process, the much overlooked hardware side. 2007年5月 Fully Vista'd up nowI've finally taken the plunge at work, after having my XP machine die 2-3 times a week I took the executive decision to wipe the machine and start from scratch. Luckily this machine is pretty well specced, and easily matched the requirements for Vista. Installation was a piece of the proverbial really - well, easy enough anyways, for some reason this machine doesn't have a DVD drive, which isn't particularly helpful when my MSDN subscription is DVD only - So after a bit of a hunt, I managed to get the ISO CD images from one of the DVD's on another pc, burned them and was good to go. I chose the enterprise edition - basically because we have a volume user license here, and that's spiffy. I'm actually not sure what the enterprise edition actually has, I presume that it's equivalent to the Business edition, I'll have to check that out some point... I've been using Vista at home for the past 5 months now (Ultimate and Home Premium) and I've been pretty impressed with it, sure I kinda suspect that there is nothing that amazing under the covers, but it functions fine and as it doesn't cost anything (from an MSDN point of view, I dunno if I would pay for it as an O/S unless I had a pretty hot machine to run it on). From a working on Vista perspective, there don't appear to be any drawbacks, at least none that I've noticed. I suspect there are things in Visual Studio that i'm unable to do, but as I haven't come across those things yet, so I think it's going to be ok from my perspective. SQL 2005 Management went on alright, as long as the SP2 was applied, and everything else I use (Office 2007 etc) were basically built for it! Not the most interesting of posts I fear :p 2007年4月 Time zones in .NETThe problem: converting a given string representing a date and time to a UTC time. Time zones are funny things, annoyingly complicated and frustrating to program with. Obviously there are solutions and workarounds, mainly working with UTC (Universal Time Code - which is the same as GMT (Greenwich Mean Time) and WET (Western European Time) and I'm sure another 100 or so others!). For those who are taking their first steps into the minefield of time zones in .NET they might be lulled into a false sense of security with the 'TimeZone' class available to developers. It seems promising, hell, a whole class called TimeZone! Easy money - surely something like: TimeZone est = new TimeZone("EST"); //-4/5 hours from UTC Or something like: DateTime estTime = TimeZone.Convert("GMT", "EST", gmtTime); But no, we unfortunately don't have this capability built in. It's a bit annoying, as we know the hosting O/S of the framework (be it *nix or Windows) knows the TimeZone conversion information, we've all played with the clock once or twice in the past and seen the world displayed with all its zones. So, with the framework leaving us high and dry, what can we do? Firstly – looking around the interweb for time zone related information takes us on a tour of many sites dedicated to the world’s time zones, such as ‘The World Clock’, ‘World Time Zone’ and the ‘Time Zone Converter’ sites – generally just information providers – useful for finding out times etc, but not too practical for the programmatical (I’m pretty certain that isn’t a word) approach. Then a bit of delving takes us to the Zoneinfo page (also known as TZ), a public domain database containing the relevant time zone information. Now we’re getting somewhere. It has an impressive amount of implementations, from the GNU C Library, to Mac OS X, in fact, ‘The World Clock’ website (linked above) uses TZ as a backend. Ok, so we’ve got our database, what next? Well – looking down the list of libraries etc using TZ there aren’t any .NET based ones at all – nuts – this means we might have to create the API to the TZ database ourselves, or if we’re lucky – someone has already done this for us. So a quick search for: ‘zoneinfo C#’ brings up a likely candidate – TZ4Net Library. Ahh HA! This is more like it – hopefully with this we can do the conversions we were after! Time now for a quick program just to check this does indeed do what we’re after, and yes! It provides exactly what I wanted. The OlsonTimeZone class will use any ‘DateTime’ passed as an argument as a DateTime from the same zone. Perhaps better explained: If you create two OlsonTimeZone instances: OlsonTimeZone zoneEST = OlsonTimeZone.GetInstance("EST"); And then convert an arbitrary DateTime (in this case 9am) DateTime toConvert = new DateTime(1902, 1, 1, 9, 0, 0); We get the output: Converted: 09:00:00 So, zoneEST read the 09:00:00 as 9am EST time, zonePST as 9am PST. The reason this is important is because it makes no difference what the DateTime class thinks its own TimeZone is, whether it’s UTC or local, OlsonTimeZone will just ignore it. The sample program below goes through a collection of values stored in an array printing out some information about the time zone selected – nothing special at all. using System; class Program string currTimeZoneInfo = (TimeZone.CurrentTimeZone.IsDaylightSavingTime(now)) ? Console.WriteLine("Time: " + now + ", " + currTimeZoneInfo); foreach (string timezone in timeZones) bool isDst = zone.IsDaylightSavingTime(now); Console.WriteLine("UTC: " + utc); Console.WriteLine("Press ENTER to Exit"); All that’s left is to say thanks to Zbigniew Babiej for writing the API! 2007年4月 Into ProductionBusy times at the company at the moment. We've just pushed out the newest version of our system (the one I've spent the last 3 years working on), into a new domain. On a scale of 1-10 (10 being the best) this week has been a 7, we were due to go into production on the Monday, but due to unforseen problems with an external source (always good when the delay isn't due to your code!) we had to delay until Tuesday. Tuesday brought (unfortunately) code issues - and with it, another 24 hour delay. Wednesday though - well - that brought excitement and joy - as it finally went live. Admittedly - there were some teething problems, but nothing a couple of hours of sarcastic jokes and fretful debugging couldn't solve. The system is now back on the shelf - awaiting analysis of the days activity - and a couple more minor bug fixes. But hopefully from next Tuesday onwards - we'll be looking at a fully live situation! I realise this isn't the most interesting post, and by not actually mentioning what the system is, it's mainly pointless. Meh. 2007年4月 Finally finished my first WPF appThat's Windows Presentation Format for those not in the know! It's the new presentation format for .NET developers (due for release with the next version of Visual Studio - "Orcas"), and also buildable using Microsoft 'Expression'. Expression is a new collection of tools aimed more at 'designers' than 'developers' - that basically allows you to design new GUI (Graphical User Interface) elements using the Vista stylings. Well - that's a bit of a simplification, but it's close enough. Anyhews - I love playing around with UI elements on my pet projects, (generally leading to a 'form over function' build arriving much before anything else), but I hadn't really messed around with any of the WPF stuff until this past week or so. I attended a conference in february (DevWeek 2007) and whilst there picked up a new book - 'Foundations of WPF' (by Laurence Moroney). I started working my way through it, and aside from it being a bit out of date now (technology does move fast) I've been finding it good and easy to get to grips with. I've only built one app, and that's using Expression - so I'm looking forward to getting my hands dirty in the Visual Studio Environment - but I have to say how impressed I am with the Expression program (Expression Blend). The ability to create pretty impressive looking stuff in next to no time is phenomenal. Even just setting up a simple reflection in WinForms code would take - well - ages, in Expression - 2 minutes. The plan is to continue one of my other projects (a maze solver) and then wrap it up with a nice WPF front end - only a couple of hundred pages to go! |
|
|