Monday, November 28, 2005

 

Multithreading in VS2005

As I keep saying, I'm working on a program that captures images using the Canon SDK and processes them. This weekend, I set out to add progress dialogs. So I created a single dialog, with a progress bar and properties/methods to set text and the progress state. My strategy was to do all my processing on another thread, and to have my progress dialog as a member variable of my main window. My code passes in this instance to the other thread, then calls DoModal, the idea being that the progress dialog is manipulated by the worker thread, and it's modal state stops the main thread from doing anything apart from making sure my windows are drawn properly. Part of my problem was definately not to let the user do anything while this other thread worked. I could probably have achieved this on one thread with Application.DoEvents, but this seemed better at the time.

First of all, has anyone at Redmond tried debugging a multithreaded app ? I set a break point, and when it hits, the IDE does not respond for a good two minutes, excepting the little tool tip that comes up to say it's busy, and if this happens often I should tell Microsoft. Then, when my breakpoint hits, I can do no evaluation of variables anywhere, because 'an operation has timed out, you will need to restart execution for evaluation to work'. Great. Except that when I try to step to the next line, the program never responds again, and any attempt to break into it will break into the thread that is stuck calling DoModal on my progress dialog.

All of this only after I got the code to work at all. In VS2005, the framework detects if a UI element is used across two threads, and throws an exception. To stop this behaviour, you need to set CheckForIllegalCrossThreadCalls = false;, which is a property on the Control class. I found this info easy enough. I was even able to see a mock class for the Control class when I chose 'Go to definition' on the base class of my control. I just wish that the bug I reported had been fixed ( which is that if I create a library, use it in a project that I add to the solution, and then choose go to source on a library method in the other project, instead of taking me to the code, it creates a stub class with no code in it, and I need to manually go to the actual code, which is part of the same solution and therefore should be visible ).

Friday, November 25, 2005

 

Yet another LINQ post ( sort of )

As I'm a Microsoft MVP, I had the pleasure of seeing a demonstration of LINQ a few months ago. I seem to always be last through the gate on new stuff, mostly because I am so busy I guess. So, months later, I am finally taking the time to learn about LINQ. As it stands for Language INtegrated Query, I think it should be called LIQ, but that's another story.

Actually, the only reason I'm making time is that I agreed to do a user group presentation. I find that getting involved in things like this, or web articles, is a good way to motivate myself towards tasks that have no immediate cash reward, otherwise the paying work just takes over and I don't manage to make the time.

I've just been looking at a few articles around on LINQ, and the comments on some of them. Amongst all the 'wow that's amazing' type stuff, was a question : does this mean that my XML and database access is going to be faster ?

*sigh*

While I value C# for a lot of things, and one of those things is definately rapid development, I miss C++ for a lot of things, too. One thing I miss is a rich standard library, especially as it relates to containers and algorithms. I think that balance is being redressed, especially with LINQ. However, I've seen people on message boards say 'we're so lucky to be programming now, we don't have to understand pointers, and all that rubbish'. Well, I beg to differ. Years ago, I wrote a paint program, with alpha blended brushes, and all manner of effects. As I was coming to an end, I found GDI+ lurking in the PSDK, along with virtually no documentation. I wrote a series of Code Project articles on GDI+ as a result, for once I was ahead of the curve. And I rewrote all my tools to use GDI+. I'm of the school that says every programmer should, for example, write a linked list class, but never, ever use it. You need to know how this stuff works, but you should use the stuff that your language provides, and assume it's been put under closer scrutiny than your class, and survived. So, I am very glad that I learned how to write all of those filters and tools using GDI only, although nowadays, given the option, I'd rather use GDI+, the point is that I have a pretty good idea what I'm asking GDI+ to do, and what it's likely to cost me.

So, back to my friend who thinks that LINQ will make his database faster. I've not looked into DLINQ in any depth yet, but how is it even possible that having an extra layer that dynamically generates SQL is going to make things FASTER ? I'd be praying for no change in speed, personally, especially as we're now sending dymanically generated SQL to the database, whereas any DB code I write is calling stored procedures, which SQL Server will precompile, and which results in less network traffic for the request.

Don't get me wrong, I think LINQ is an awesome idea, but like a lot of ideas ( managed code in SQL Server springs to mind ), it's a tool that no doubt can be abused, as well as being really useful if done with care. I will be doing some testing of DLINQ to see how it fares, speed wise, and to better understand how using it will affect my database security ( I like to run as a user who can only call stored procs, which greatly limits the harm anyone can do ). No doubt, when I find time to do this, I will make the results known here. For the time being I'm focusing on the stuff where I expect it most to shine, that is, in creating a framework that allows me to write reusable algorithms for filtering and processing collections and XML.

Friday, November 18, 2005

 

ListViewItem - what a good idea....

As I may have mentioned, I work on a fair size WinForms app in C#. This app up until now has shown images from only one source, but I've added the ability to import still frames from video files. We wanted to be able to show only video imports, so I added a field to the XML metadata that we store, and wrote a filter. At the moment there is a modified listview to show images, so I added a property to tell me which items to draw. As the listview organises items itself, it seemed logical to store all images in a seperate list, and then give the listview only the images it was supposed to display, so the drawing code would not need to be changed.

A ListView contains a ListViewItemCollection, and a ListViewItemCollection can only be created by telling it the ListView which it belongs to. So, in order to create a second collection, I needed to create a second ListView instance, inside my Listview derived class. That's fine. However, and this is my point, I discovered that the ListViewItemCollection has a method called AddRange. Just looking at it in C# 1.1, it appears to have one signature only, which takes an array of ListViewItems. However, in C# 2.0, it has a second signature. It takes a ListViewItemCollection, and also indexes to say which items to move.

Now, if you call this method, trying to copy items from one ListViewItemCollection to another, you get an error message, as it's not possible to place a ListViewItem into one ListView ( and therefore ListViewItemCollection ) without removing it from the other one. So, a method has been added to the framework which is useless, as it does not remove the item from the old collection before moving it to the new, which means it can never work.

Naturally, this was not a major impediment to my problem, which is now solved. It does lead me to question the quality control that goes on when these class libraries are designed, or perhaps written ( given that it's possible to write a method that does this and works ).

It almost makes me feel inspired to rave again about how terrible the implimention of the AudioVideoPlayback namespace in DX9 is.

Wednesday, November 09, 2005

 

Deprecated stuff in C# 2.0

I just spent a half hour rewriting my emailing code to use the new System.Net.Mail namespace instead of System.Web.Mail ( so now we have System.Web and System.Net, that's not confusing at all ). I'm a real fan of 0 warnings on my projects. Now all my warnings are the same:

Warning 1 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'

Now, I copied some examples out of MSDN, but the System.Configuration.ConfigurationManager class doesn't actually seem to exist, at least not according to my compiler ( the same one that tells me off for using the 'old' System.Configuration.ConfigurationManager class. )

How can the IDE know that I should be using a class that does not exist ?

 

Thanks to Shawn Burke !!!

Shawn has posted a comment to my entry on the forms designer crashing, which I reproduce here because it helped to solve my problems. It had not occured to me to debug the IDE, and doing so helped me to track down a few instances where the form designer didn't like my controls, even though they always work perfectly at runtime. The controls in question were not even visible, but in the new IDE, they were bringing things to an abrupt halt.


Two things:

1) Go to your project directory, list the hidden files and look for one ending in .suo. Delete that file. This file just tracks what you had open, so you won't lose anything important.

2) You should now be able to start VS. Start two instances of VS, attach one to the other (Debug >> Processes >> devenv.exe).

3) Choose Exceptions >> CLR Exceptions >> Break when exception is thrown

4) Go the the other version of VS (the one that's being debugged) and open your form.You'll start breaking on exceptions. Hit continue until you see an exception of type ArgumentException.Look at the stack, and post it here. If it's something in your code you can fix it but in either case the designer shouldn't crash there, so the stack will tell us where we're letting an exception sneak through. We did a bunch of work to catch all of those, but there's a lot of possible nooks and crannies.

So, basically, debug the IDE and you'll see where the designer is having trouble. Change your code so that the designer is happy, and all will be well. Certainly at this stage it seems that my form is able to show itself over and over again as a result of the changes I have made ( at one point, my code shows a bitmap via a property, and if null was passed in, it would simply not process it. The designer seems to be passing in a Bitmap with a pixel format of Undefined, which was crashing my code, so now I check for this and reject it also ).

Monday, November 07, 2005

 

Another VS2005 niggle

One other thing that happened when I installed VS2005 - my source safe disappeared. Now, this was a nightmare because as well as connecting to a VSS database or two at work, I run one on my notebook, for my own source code and contract work. Where that to disappear, I'd be in a lot more trouble than if I'd organised a sensible backup schedule.

Luckily, I found that VSS is still there, just the start menu group was deleted. Only, for some reason, VSS is also no longer able to be detected by VS2003, let alone VS2005. It's there, I can run it, and use it, but integration in the old IDE is gone, and in the new IDE does not exist to start with. I'm surprised this sort of thing was not tested, I installed the betas without these sort of problems.

Friday, November 04, 2005

 

VS2005 annoyances, part 1

As I may have mentioned, I do a fair bit of work in C#, and one of my big jobs is to maintain a pretty large image processing system in C#. I moved this sytem to VS2005, mostly because we wanted some of the new UI features. When moving a system from 2003, VS2005 has some most amusing bugs. Namely, my app has one main form where just about everything happens. The different parts of this form are written as user controls. All the user controls load fine and can be edited with no problems. When I start VS2005, I can load the mainform, and edit it. If I navigate away from that screen and go back, I get a message box that says 'Parameter is incorrect', and the IDE closes. Now, here's the fun bit. If I reload the IDE, it remembers that this form is open, so it opens it, but does not start with it visible. So, if I forget and click on it, the IDE crashes AGAIN. I need to go to the windows dialog, close the window from there, close the IDE and start it again in order to edit my main form.

Isn't that just brilliant. Just to add insult to injury, I did not notice this at first, and I told someone on the Microsoft forums that they needed to look into their own code before blaming Microsoft. So, I look like an idiot. Again.

Tuesday, November 01, 2005

 

Visual Studio Team System install problems

Well, I've gone from 2 replies, to 1, to none. My audience has dwindled, and just when I have something useful to share.....

I'm a Microsoft MVP, which means I keep up with all the beta stuff flying around. I've been running VS2005 beta 2 for months, with WinFX and the LINQ preview. VSTS trial edition is finally available from the MSDN site ( I'll get the full one in the mail ), so I downloaded it last night. My hope was that, as VS2002, VS2003 and VS2005 beta 2 all co-exist happily on my hard drive, I could install 2005, install WinFX, and keep beta 2 for direct comparison of bugs I find, and also for LINQ.

No such luck. Instead, I got a list as long as my arm of the order in which to uninstall stuff. WinFX and LINQ are not on the list, although a lot of other stuff I don't have installed is. I work through the list, a couple of things, including J#, hang in the uninstall and I have to kill them. I get to the bottom of the list, the .NET framework refuses to uninstall, I have some thing that needs it, but it can't tell what. How does it know I have something, if it can't tell what ?

It turns out that WinFX installed three things, two under Microsoft WinFX, and one under just WinFX ( at the bottom of the uninstall list ). So, finally, I can remove the .NET framework 2.0 beta 2. Now, I start the install.

After a couple of minutes, a dialog box comes up telling me I have beta 2 stuff still installed. It gives a list of things it COULD be, but says it could basically be anything. I go back to the docs. They tell me that there are a couple of uninstalls that could fail, and give me command line arguments to complete them. One is J#, which I vaguely remember having trouble with, and either way, I'm stuck, so I give them both a try. Both report I am trying to remove something that is not there.

I go back through the docs again, after looking through add/remove programs 10 or so times. I discover that Microsoft have a tool for removing the last remnants of prior versions. It's not guarenteed to work, or not to do harm. I desperately want it to not hurt VS2003, but I try it anyhow. here is the link : http://go.microsoft.com/fwlink/?linkid=47598. This tool runs, tells me I still have stuff installed, and proceeds to uninstall, you guessed it - J#. Now, I uninstalled this thing, and ran the uninstaller again from the command line. I guess J# feels lucky enough that someone bothered to install it at all ( it must be there by default, I can't imagine actually SELECTING it ), and clings on for dear life. I know I'd fear for my job if I was on the J# team.

So, 5 hours after starting, I am about half way through the actual install of VSTS2005. I am desperately hoping they fixed the bugs that I found yesterday ( moved a 2003 C# project to 2005, IDE crashed every few minutes ), so I can get some actual work done.

This page is powered by Blogger. Isn't yours?