Recently in .Net Category

FolderSyndication 1.0.3

|

Due to a minor logic error which prevents FolderSyndication from publishing the XML in some cases, I've created a maintenance release. The package will automatically upgrade any previous versions, however, you should manually back up the .config file and any custom XSLT files you have added, as the installation process will overwrite or delete them.

Download Link (Note: This package will not install on Windows 2000 or previous due to its use of the LocalService account. To install on Windows 2000 or previous, use this package instead.)

If you have any questions, comments, or bug reports, please don't hesitate to contact me.

FolderSyndication 1.0

|

Announcing FolderSyndication 1.0. FolderSyndication is a tool that will watch folders and files for changes (new files, modified/renamed files, deletions, etc) and will publish those changes, for instance to an Atom feed (the default, although an RSS 2.0 feed is another provided option). If you don't like Atom or RSS 2.0, you can provide your own XSL transform to output whatever you want--Word document, raw XML, database update, whatever.

Features:

  • Publish format is completely customizable through the use of standard XSLT files.
  • Folders can be watched recursively, and notifications can be filtered by wildcard patterns.
  • Publishing is performed at configurable intervals in order to not cause performance degradations during large file modifications (such as copying or deleting large numbers of files).
  • Windows native FileSystemWatchers are used, meaning even a very large file tree can be watched efficiently. (Not yet tested against very high volumes of file changes.)

As with the other tools available on my website, FolderSyndication is licensed under a Creative Commons By Attribution license, meaning you are free to use and redistribute it as long as you give me credit as the original program author.

Download Link (Note: This package will not install on Windows 2000 or previous due to its use of the LocalService account. To install on Windows 2000 or previous, use this package instead.) Either package is about 400k in size.

If you have any questions, comments, or bug reports, please don't hesitate to contact me.

Several months ago, Roy Osherove posted a discussion of Defensive Event Publishing in .Net that discussed various problems with the "normal" methods of event publishing and raising in .Net. The naive programmer merely calls MyEvent(sender, eventArgs), never suspecting the minefield into which he or she is blithely strolling. Roy's post suggests several progressively more cautious methods of raising events to protect oneself against "bad" clients. At the time I commented that further improvements could be made, specifically to both avoid using Threadpool threads and to detect which callers are bad. I thought I'd finally get around to explaining what I meant and actually providing a solution I've used in the past.

First off, not using Threadpool threads. I'm really not a fan of using the Threadpool for any operation that I don't have absolute control over, because there's a limited number of them. The default number can be increased, but you can't make it infinite (and if you could, it would defeat the purpose of thread pooling anyway). IMO threadpool threads are useful for short, relatively deterministic operations which won't ever call any client code and which either will never fail, or will fail in such a way that you don't care or can't do anything about anyway. Raising events just doesn't fit those qualifications for me. So the solution is to not use threadpool threads; this is a fairly simple thing to do if you're at all familiar with .Net threading. Depending on your implementation, however, and definitely if you use the code I've posted at the end of this article, then there are a few caveats to watch for; I'll note them along the way.

The second way in which we can add to Roy's article is in detecting failed calls. His solution calls a OneWay async Invoke on the delegate; it's a fire-and-forget situation. Unfortunately, especially for an application that needs to stay up 24/7 for long periods of time, it may not be acceptable to just ignore failed calls; the app may want to clean up, or at least rid itself of the bad reference and let the GC pick it up. In order to do that, I use WaitHandles; each thread that I spawn for an individual delegate call will set a WaitHandle when it finishes. (Note that .Net events raised over Remoting automatically time out after a period of time. Using this method with non-remoted events would require additional code to detect timeouts, but would not require any additional code to detect clients that just don't exist anymore.) Here's one of our caveats: WaitHandle.WaitAll can only handle a certain number of handles; on the current .Net implementation (namely .Net 1.0 and 1.1 on Win32) that limit is 64 handles. Calling WaitHandle.WaitAll on > 64 handles will throw an exception. So, should you have more than 64 clients listening to the event, the code will automatically break them up into batches of 64 and wait on each batch sequentially. Another wrinkle is that WaitHandle.WaitAll isn't usable from STA threads--such as those used by Windows Forms--if you're waiting on more than one handle. This can be particularly tricky, as this means you probably can't raise an event using this code on your main Windows Forms UI thread. The code below doesn't handle this case (because our app wasn't a WinForm app and had no STA threads); if your code will be called from STA threads you will need to handle that situation (possibly by raising all events on a new thread).

The final caveat is that only the class that declares an event can modify that event (other than a simple += or -= to add/remove a listener). Thus you can't modify the delegate list to remove a specific listener except from the original class. In order to get around this, my utility function returns a new delegate list that has all of the "bad" clients removed. If your code needs better information about exactly which delegates were removed, you could add either an out param for the "bad" list, or a delegate called for bad clients, etc.

Using the code is fairly simple. The general case looks like this:

    1 using System;
    2  
    3 namespace EventTest
    4 {
    5   public delegate void MyEventHandler(object sender, EventArgs e);
    6  
    7   public class EventRaiser
    8   {
    9     public event MyEventHandler MyEvent;
   10  
   11     public void RaiseEvent()
   12     {
   13       MyEvent = (MyEventHandler)EventRemoter.RaiseRemotedEvent(MyEvent, this, EventArgs.Empty);
   14     }
   15   }
   16 }

Relatively simple, aside from the need to cast the return value and the WaitHandle issues mentioned above.

The code for EventRemoter is available here. If you find it useful, or find a problem or just have a comment, please, let me know!

This code is covered by the same license as other items available from this blog, namely the Creative Commons' "By Attribution 2.0" license.

Addendum: After a brief conversation with someone who had recently asked me about this code, I added a static parameter to control the number of simultaneous threads that will be used by any one event raise, rather than using a magic number sprinkled through the code. The parameter defaults to 64 in order to be correct on Win32, but can be changed in either of two situations. If you want the code to use fewer threads (as the default version will spawn a lot of (very short-lived) threads when raising events to a lot of subscribers), then set the parameter lower. If you are using the code on a platform where WaitAll works with more than 64 handles, then you can set the parameter higher. The new version is at the same location linked above; enjoy!

CopySourceAsHtml

| | TrackBacks (1)

Colin Coller has created a very nice plugin for VS.Net called CopySourceAsHtml that lets you create colorized text by copying source from VS.Net. It produces pure HTML code (not the stuff spat out by Word) using embedded stylesheets:

<style type="text/css">
.csharpcode
{
	font-size: 10pt;
	color: black;
	font-family: Courier New , Courier, Monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0px; }
.rem { color: #008000; }
.kwrd { color: #0000ff; }
.str { color: #006080; }
.op { color: #0000c0; }
.preproc { color: #cc6633; }
.asp { background-color: #ffff00; }
.html { color: #800000; }
.attr { color: #ff0000; }
.alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0px;
}
.lnum { color: #606060; }
</style>
<div class="csharpcode">
<pre><span class="lnum">   167: </span>    <span class="rem">/// <summary></span></pre>
<pre><span class="lnum">   168: </span>    <span class="rem">/// Creates a new socket server object and optionally starts it listening.</span></pre>
<pre><span class="lnum">   169: </span>    <span class="rem">/// </summary></span></pre>
<pre><span class="lnum">   170: </span>    <span class="rem">/// <param name="name">The friendly name for this socket server.</param></span></pre>
<pre><span class="lnum">   171: </span>    <span class="rem">/// <param name="port">The port to listen on.</param></span></pre>
<pre><span class="lnum">   172: </span>    <span class="rem">/// <param name="startListening">Whether to immediately start listening, or wait for a <see cref="StartListening"/> call.</param></span></pre>
<pre><span class="lnum">   173: </span>    <span class="kwrd">public</span> SocketServer(<span class="kwrd">string</span> name, <span class="kwrd">int</span> port, <span class="kwrd">bool</span> startListening)</pre>
<pre><span class="lnum">   174: </span>    {</pre>
</div>

Which turns out looking like this:

   167:     /// <summary>
   168:     /// Creates a new socket server object and optionally starts it listening.
   169:     /// </summary>
   170:     /// <param name="name">The friendly name for this socket server.</param>
   171:     /// <param name="port">The port to listen on.</param>
   172:     /// <param name="startListening">Whether to immediately start listening, or wait for a <see cref="StartListening"/> call.</param>
   173:     public SocketServer(string name, int port, bool startListening)
   174:     {

It's highly configurable and very cool, so if you intend to post code on the web, check it out!

Multithreading is hard.

| | Comments (2) | TrackBacks (2)

Lately at work I've been dealing with a problematic socket server. The currently deployed version has something of a memory leak (to the tune of 140+MB/day), probably due to complications of incorrectly multithreading System.Net.Socket instances (note: they're not thread-safe).

Unfortunately, when I redid the socket server to lock all the sockets and other non-thread-safe resources, I ran into a deadlock. In chasing it down, I used Phil Haack's modification of Ian Griffith's TimedLock class. That enabled me to find where the deadlocks were, and eliminate them. This class is really a very clever tool, with one small problem: it was throwing exceptions on the production server. The test server ran fine for days at a time, loaded down as heavily as I could manage, but the production server locked inside of two hours every time. The first error in the log was always an ArgumentException thrown by the stack trace hashtable, saying that the object being inserted as the key was already in the hashtable.

After several days of debugging, and a few e-mails exchanged with Phil, he said the following to me:

If the object wasn't removed from the hashtable via the dispose method before the second lock is acquired, that could cause the error.

I started to write back, saying "But isn't the whole point of the locking that there is no way any other thread could acquire that lock until Dispose is called, thus calling Monitor.Exit and removing the object from the hashtable?", and then I was, as they say, enlightened. The sequence of events in the TimedLock runs like this:

TimedLock tl = TimedLock.Lock(o);
  Monitor.TryEnter(o);
  StackTraces.Add(o);
...
tl.Dispose();
  Monitor.Exit(o);
  StackTraces.Remove(o);

On a single-CPU machine (such as our test server), this code runs fine, I would guess, 99.99999% of the time. On a dual-cpu machine (such as the production server in question), however, it runs fine only 99% of the time. That 100th time, here's what happens...(assuming o is the same object in both threads)

Thread A                              Thread B
TimedLock tl = TimedLock.Lock(o);
  Monitor.TryEnter(o);
  StackTraces.Add(o);                 TimedLock tl = TimedLock.Lock(o);
...                                     Monitor.TryEnter(o); // blocked
...                                   ...waiting
...                                   ...waiting
tl.Dispose();                         ...waiting
  Monitor.Exit(o);                    ...waiting
                                        StackTraces.Add(o); //******
  StackTraces.Remove(o);

The starred line is where the exception gets thrown. Textbook race condition -- if Thread B doesn't hit that Add() call between Thread A's calls to Monitor.Exit and StackTraces.Remove, then everything looks fine. But every once in a while (such as when processing a send and a receive simultaneously on a socket), it'll hit that tiny little target and blow the whole thing up.

What's worse is that as written, once that target has been hit, that object can't be successfully TimedLocked (even though the original lock has been released) until the TimedLock that hit the exception has been finalized. This is true even if you wrap the TimedLock in a using statement (because the exception will leave using() with a null reference, which it can't Dispose).

The fix? Simple -- swap the order of the Monitor.Exit() and StackTraces.Remove() calls. That ensures that the object will be removed from the hash table before any other thread can try to re-add it.

This all looks very cut and dry now that I've laid it out, but before anyone goes accusing Phil of not knowing his stuff, reread the subject of this post. Multithreading is hard. .Net (and other modern languages) do a good job of hiding some of the complexity; for most WinForms apps, for instance, threading is very easy as long as you remember to use InvokeRequired and Invoke. For something more complex, for instance a server app with multiple long-running threads that must access common resources, you need some help, and writing that help can be very difficult. It took me about 3 full days to find this bug, and all I have to say at the end is that if I weren't using a good helper class like TimedLock, it would have taken me much, much longer.

One other lesson I've (re)learned... always always always test multithreaded code on a multiprocessor machine, because it's so much easier to hit race conditions and other problems on that platform.

Another update to SharpTerminal: this one fixes the large blank spaces on the bottom and right sides of the GUI, as well as a minor startup bug where if you hadn't saved any default settings, and hit Connect without going to the Config tab, you'd get an error. Going to the Config tab and back fixed the problem, but now it shouldn't appear at all.

The GUI bug was an interesting one for me. The computer I write SharpTerminal on had the DPI setting (Display Properties, Settings, Advanced) set to Large (90DPI). So the GUI looked fine on that computer, but it turns out that .Net is smart enough to perform Automatic Control Scaling according to the difference between the developer's settings and the runtime settings. This works great when the developer's settings are Normal and the runtime settings are whatever; Windows Forms scales the GUI appropriately. Things get a little weird when the developer's settings are Large (or possible any non-Normal setting)--as you can see in the screenshot below, on a system set to Normal, the scaling doesn't quite work:

User interface with large blank gaps on the right and bottom edges.

The solution turns out to be fairly convoluted. First, set the developer's computer to use Normal DPI settings and restart the PC. Next, open the solution in VS.Net and go to the code for the form with the issue. Look for a line that says this.AutoScaleBaseSize = new System.Drawing.Size(6, 15); in the Windows Form Designer generated code region, and change the values to 5, 13 (the default values for a Normal system). Open the form in designer mode. Things will likely be very screwed up (controls will run off the bottom and right sides). Fix them. Note that some controls--for instance, the Microsoft ActiveX Web Browser Control--will probably have to be removed and readded in order to work properly. Recompile and the app should look right.

Of course, probably the best idea is for developers to not use strange DPI sizes to develop UIs in the first place. :-P

So today Erik Porter linked to a very silly list of reasons to switch from VB.Net to C#. I say very silly because most--nearly all--of the "reasons" are either nonsense, irrelevant, or outright wrong. As someone who recently switched from working primarily in VB.Net to primarily in C# (not on my own initiative, and having no axe to grind), I feel I can shed a little light on this foolishness. In order, my comments in italics:

For the Developer:

  1. Developers who program primarily in C# earn 26 percent more than those who develop primarily in Visual Basic .NET.
    This is about the only one I have no issue with, as long as the survey data is good. I'm not 100% sure I buy it, but at least it's not completely obviously off-base.
  2. C# just looks more elegant because it was consistently designed. VB.Net was evolved over many years and has inconsistencies.
    You can write C# code that looks like a mishmash of styles (and even languages) because--surprise!--there's more than one way to write programs! Similarly, it is entirely possible to write a completely .Net, FxCop-compliant application in pure VB.Net.
  3. C# is closer to Java which means it is easier for you to move to or from Java. This is good for your career.
    Poppycock. The real differences are not in the syntax--they're in the class library. Going from C# to Java will entail just as much of a learning curve as going from VB.Net to Java (since the BCL is identical for both VB and C#). Any developer worth the crud in their keyboard can learn a new syntax very easily; it's the BCLs that are difficult.
  4. C# is perceived as a ?real? language where VB.Net is still perceived as a ?toy? language.
    Maybe. And I say maybe. But perceptions change.
  5. Microsoft does all of its internal .Net development in C#. Even MS thinks C# is the better language.
    That's a lie. Microsoft internal teams do work in VB.Net. It's true that the majority of them work in C#, but they're coming from a C++ background--C# is more natural. VB.Net is in no way inferior to C# because of this.
  6. C# has the following features which VB.Net doesn?t have:
    • Operator overloading
      Will be added to VB.Net in 2005--and I've never missed it, anyway. How much real development involves overloading the + operator?
    • XML code documentation
      Has been available via a free plugin forever, and will be in 2005. Yee-haw.
    • Ability to write ?unsafe? code for better interoperability.
      In the (extremely rare IME) situation where you need unsafe code, it's trivial to drop down into C# or even C++ for it.
  7. Microsoft is actively adding new useful features to C# including generics, interators, anonymous methods, and partial types.
    Whereas VB.Net is stagnant? Hardly. Generics and partial types are coming to VB.Net at the same time, interators (sic) already exist, and some VB.Net features--Edit and Continue and the My classes, for instance--won't be in C# at all.

For the Manager:

  1. Your code quality will improve because C# catches potential errors (example: variable use allowed before initialization and dead code) that are permitted in VB.Net.
    Am I the only one who's ever heard of Option Strict? As for dead code, yeah, it's nice that the C# compiler catches it, but I bet the VB.Net 2005 compiler will too.
  2. Your developers will be more productive because they will work in a language that they like.
    I really need a "confused" emoticon here. I don't know anyone (at least anyone who's ever actually used it) that doesn't like VB.Net.
  3. If your project is a mix of VB.Net and C# code, your developers will be more productive because they won?t have to switch between languages.
    That doesn't make any fucking sense at all. If the project is a mix, then choosing either language is about as effective.
  4. C# is more portable than VB.Net. It is closer to Java which makes it easier to port code to Java later. It also can run on other operating systems (including Linux) by using the Mono and DotGNU open source projects.
    Again with the portable-to-Java canard. Say it with me: Base Class Library. And Mono should be adding support for VB.Net towards the end of this year.
  5. C# has been submitted as a standard language (ECMA) which makes its syntax more stable. Microsoft could make drastic changes to VB.Net at any time.
    They could make drastic changes to the Office object model, too, but they're not retarded. If they were going to make drastic changes, they'd have done it between VB6 and VB.Net (*cough*AndAlso/OrElse*cough*), not between VB.Net 2003 and 2005.
  6. Microsoft does all of its internal development in C#.
    Repeating it doesn't make it any less wrong.
  7. C# is better because the following features make it easier to write better code faster.
    Yeah, see #6 on the Developer side. Repeating it doesn't make it any less wrong.

Don't get me wrong... I like C# a lot (some days better than VB.Net, some days not). But choosing between VB.Net and C# has nothing to do with the above list (excepting perhaps #1 on the developer side) and everything to do with your background and comfort level. If you know VB6, go with VB.Net. If you know C++ or Java, go with C#. If you know two or more, go hog wild. :)

SharpTerminal 1.0

| | Comments (4)

So yeah, this is my new blog showcasing technology and programming, so that Jenny doesn't have to read all that boring stuff unless she wants to. ;)

For my first post, I'm going to present a little app I've put together called SharpTerminal. As the name suggests, it's a terminal app (essentially a replacement for Hyperterminal) written in C#. It does a number of things Hyperterminal doesn't do, including a command buffer, easy entry of binary (non-printable) data, display of carriage return/line feed, etc. Oh heck, here's the ReadMe:

Features:

  1. View communication as either ASCII text or hexadecimal values
  2. Save full session transcripts in multiple formats for easy analysis
  3. Open previous session transcripts
  4. Easy entry of binary data (prepend with 0x for hex entry)
  5. Unlimited command history (up and down arrow in Send box)
  6. Show or hide connection, control line, etc events.
  7. Colored text for ease of distinguishing between sent and received data
  8. Multithreaded for responsiveness
  9. Prettier than Hyperterminal

Future Enhancements:

  1. Error Handling is not completely up to snuff. It won't crash, but it's not as pretty as it could be.
  2. Allow user to select encoding for "Text" mode.
  3. Enable DTR handshaking
  4. Consider allowing the intermixing of ASCII and binary?
  5. Add automatic crash reporting.

System Requirements:

  1. Microsoft .Net Framework, version 1.1. It might run against 1.0, I haven't tried.
  2. Internet Explorer (any version 4 or later should work AFAIK).
  3. One or more serial ports.

Licensing:
SharpTerminal use is not limited; you may copy it, redistribute it freely, use it in a business, install it on a rocket and shoot it to the moon, or anything else I haven't mentioned here, with the following restrictions:

  1. No claiming it is your own work. You must include this ReadMe.txt file, UNMODIFIED, any time you redistribute it.
  2. Actually, that's pretty much it. If you really need specifics, see http://creativecommons.org/licenses/by/1.0/

Questions? Comments? Bugs? Feature Requests?
Visit http://www.randomtree.org/sharpterminal/ or e-mail code@randomtree.org

All code, text, and images copyright 2004 Eric Means.

If it sounds like something you could use, download SharpTerminal and give it a try (the zip file is about 1.5MB)!

About this Archive

This page is a archive of recent entries in the .Net category.

FolderSyndication is the next category.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.01