Task Queue Revisited

Several years back, I wrote about  A Simple Task Queue  and made a sort of promise that if I got time, I would enhance it a bit. Here is my enhanced version. public class TaskQueue { public delegate void TaskDelegate(); public TaskDelegate TaskDlgt { get; private set; } public TaskQueue(TaskDelegate dlgt) : this(dlgt, null) { } public TaskQueue(TaskDelegate dlgt, Queue<TaskDelegate> q) { _Q = q; TaskDlgt = dlgt; } public void Enqueue() { lock(_Q) { if(Busy) _Q.Enqueue(TaskDlgt); else { Busy = true; TaskDlgt.BeginInvoke(TaskCallback, TaskDlgt); } } } private void NextTask() { TaskDelegate dlgt; lock (_Q) { if (_Q.Count > 0) {

.NET 2.0 Extension Methods

I regularly write programs in C# on Visual Studio 2008. The default Framework for the environment is 3.5. But sometimes I must re-target 2008 to Framework 2.0. Is it possible to take advantage of the new "Extension Method" language feature of 2008 when required to target 2.0? This is a good question and applies to more than the extension methods. But for now, let's answer the question for extensions. Yes. But, you need to do some of the work yourself. The compiler's support for extension methods is really unconnected with Framework 3.5. But maybe you've seen the error... Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll? To write an extension, you need the ExtensionAttribute somewhere, anywhere. The compiler is looking for it in the System.Runtime.CompilerServices namespace which is in System.Core.dll.

An Interesting Delegate Usage

An interesting thing happened while I was surfing the MSDN C# forums. I was completely thrown for a loop when I came across some code posted by a regular contributor. It went something like this... private void ACallBackFunction() { if(this.InvokeRequired) this.Invoke(new ThreadStart(ACallBackFunction)); else this.label1.Text = "Something or other"; } The programming pattern should be very familiar to those using threads, because this is how a child thread updates the GUI (only the thread on which the GUI is created can update the UI). But, what seemed very strange was the use of "new ThreadStart(...)". I thought, what does a ThreadStart() delegate have to do with this operation? The answer is ... nothing. But the application is none the less interesting. Here, the programmer decided to draw from a handy delegate in the C# tool chest just to get that required delegate to call "Invoke()" with. "

Regular Expressions in C# - Password Validator Revisited

Sometimes we make life more difficult than it needs to be. Lately, I've learned a little more about Regular Expressions and "Negative Look-Around". I've used it a lot, but it only recently dawned upon me that I was not making full use of it. Case in point, take a look at my earlier regular expression article where I explain how to validate a password with multiple requirements ( Regular Expression Alternations ). This article discusses the absense of the boolean AND in regular expressions and provides a complex IF-THEN-ELSE approach to test a string for conforming to multiple "password" constraints. And my subsequent article ( Regular Expression Double Negatives ) gets more complex with an "inside-out" approach to compensate for the lack of an AND operator. But, let's go back even further to the article where I "explain" Negative Look-ahead . In this article, I provide the simple Negative Look-ahead pattern... “(?!pattern)” .

Callbacks And Delegates

It's been a couple of weeks since last writing. Besides being very busy, I've had a touch of "writer's block". Nothing too serious, I should get over it soon. Lately, I've been intrigued by the abundant use of callback functions and delegates in the .NET Framework. If you want to do anything interesting, you gotta know callbacks. I'll try to describe callbacks and delegates in simplistic terms. Maybe it will help you over the hump. Consider the following code... List list = new List (); //Populate list with some MyClass objects for(int i=0; i<10; ++i) list.Add(new MyClass(){ ID=i, strValue=i.ToString() }); //Sort the list list.Sort(); foreach(MyClass c in list) Console.WriteLine("{0}",c.strValue); ... public class MyClass { public int ID; public string strValue; public override string ToString() { return strValue; } } This code throws an exception at the line

A Simple Task Queue

I’ve spent the last 4 posts talking about Regular Expressions and some difficult patterns. But, this is a C# blog, so I really want to be talking about C#. Today, I hope to provide you with a nice little start on a multi-threading "Task Queue" application. A Task Queue will place task requests in a queue that will be serviced Asynchronously and in the order received. Rather than keep you in suspense, here’s the code up front. If you'd like an explanation, I've attempted that below. (Update 10/6/2008: Sorry folks about the bug below. The Enqueue method must set the the Busy field to true when queing the first task in order to avoid the thread race. It's now been fixed.) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace TaskQueuePOC { public partial class Form1 : Form { public

Regular Expression Double Negatives

In my last article titled "Regular Expression Alternation" we discussed Alternation as one means of performing the AND operation in a regular expression pattern. (?(expression)yes|no) But, what if you don't have this pattern in your version of RegEx? Some of you may be reading to gain knowledge that you can apply in other programming languages besides C# or the .NET environment. The pattern I described in the last article does not work in all versions of .NET Regular Expressions. In particular, it does not work in JScript Regular Expression Syntax. And, when you use tools like ASP.NET’s RegularExpressionValidator, you are implicitly using JScript. So then, what do you do? To restate the original requirement, we want to validate a string that has at least one digit, at least one uppercase letter, and at least 6 characters. The problem statement is clearly an AND problem, so it begs to be written with the Alternation pattern. But in situations where we can