Showing posts from June, 2006

TreeViews and Multi-Threaded Madness!

Disclaimer: The following information is for educational purposes only. Don't use at the track.

I'm currently working on an app (in C#) that performs three, related tasks. Each of the tasks is performed by an object, and the app's main form has as member variables these three objects. The form also spawns three threads, one for each object, so the tasks can be performed simultaneously. The objects in turn have events, which are handled by the main form. These event handlers take care of, among other things, updating the controls on the form to display the status and progress of the various tasks. One of the controls is a TreeView.

I ran into an interesting predicament a little while back when setting these event handlers up: when fired, the events could call the functions within the form class to remove nodes from the TreeView, or alter the text of the nodes, but when they tried to Add nodes, I received the following exception:

"The action being performed on this c…

A C# IsNumeric Function

As much as I love the .NET Framework and C# (and VB.NET too), sometimes I just have to scratch my head and think "Why?" For example, why no InputBox() function in C# (click here for my custom C# InputBox class)? Also, why no IsNumeric() function in C#? VB has had one for years. So, I set out to write my own.

My first inclination was simply to have a function that would attempt to convert a value from either an object or string to a numeric type, wrapped in a try block, and return false if an exception was caught. But as another developer pointed out to me, and as I experienced first hand, when the exception is thrown there is a noticable pause. A much better solution is to use the TryParse() method of the Double class.

The TryParse method attempts to convert a value to a double, but instead of throwing an exception if the conversion fails, it simply returns false. This seems like enough right there, but the function takes an out parameter, so you need at least two lines…