Sponsors

Thứ Hai, 29 tháng 9, 2008

Get rid of "Do you want to close this window?"

You might get a question when opening CRM in Internet Explorer. The question is:The webpage you are viewing is trying to close the window.Do you want to close this window?yes noThis message does appear in CRM 4.0 only when you are using Internet Explorer 7.0 and you have enabled the application mode setting. Nevertheless, it is an anoying message which you can get away!To get rid of this message

Chủ Nhật, 28 tháng 9, 2008

Overview of CRM 4.0 Training & development collateral

Microsoft employee Jon Butler is sending me updates on CRM regularly. This time he has sent an overview of the available training material. Since this is such a usefull list, I thought it would be worth sharing this with you guys!Official Courseware available now These training courses are the Microsoft Dynamics Official Curriculum currently available for CRM 4.0 now, either as soft-copy

Thứ Sáu, 26 tháng 9, 2008

Get the latest hotfixes for the CRM 4.0 outlook client

The product team has posted a blog article about the known issues with their hotfixes on the team blog. I'd strongly recommend everybody to look at this list and to install all applicable hotfixes for your system.The following issues are discussed:1. Outlook hangs during initialization (progress toolbar displays “Loading…” and never finishes)2. With E-mail Auto Promotion Enabled, Outlook may not

Thứ Tư, 24 tháng 9, 2008

Custom entities and attributes in a plug-in

You cannot create a webservice in Visual Studio when you're writing a plugin. Instead you need to use the method context.CreateCrmService(true);. There is only one single issue when using this approach. You cannot leverage the power of IntelliSense in Visual Studio. Visual Studio will not recognize your custom entities and attributes. How to work with this? The only correct answer is "Dynamic

Thứ Hai, 22 tháng 9, 2008

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 Form1()
{
InitializeComponent();
}

int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
new MyTaskQueue(counter++).Enqueue();
}
}

public class MyTaskQueue : TaskQueue
{
public MyTaskQueue(object UserData)
: base(UserData)
{
}
protected override void Task()
{
Thread.Sleep(2000);
Console.WriteLine(UserData.ToString());
}
}

public abstract class TaskQueue
{
public object UserData { get; private set; }
public TaskDelegate TaskDlgt { get; private set; }
protected abstract void Task();

public void Enqueue()
{
TaskDlgt = new TaskDelegate(Task);
lock(lockObject)
{
if(Busy)
_q.Enqueue(TaskDlgt);
else {
Busy = true;
TaskDlgt.BeginInvoke(new AsyncCallback(this.TaskCallback), TaskDlgt);
}
}
}

private static Queue _q = new Queue();
private static bool Busy = false;
private static object lockObject = new object();

public TaskQueue(object Data)
{
UserData = Data;
}

public delegate void TaskDelegate();

private void TaskCallback(IAsyncResult ar)
{
TaskDelegate dlgt = ar.AsyncState as TaskDelegate;
if(dlgt.Equals(TaskDlgt))
dlgt.EndInvoke(ar);
NextTask();
}
private void NextTask()
{
TaskDelegate dlgt;
lock(lockObject)
{
if(_q.Count > 0)
{
dlgt = _q.Dequeue();
dlgt.BeginInvoke(TaskCallback,dlgt);
}
else
Busy = false;
}
}
}
}


I've titled the article "A Simple Task Queue", but simple is a little mis-leading. That's because it's difficult to tell by looking (for the beginner) where the threads are or even how it works. The key to understanding this implementation is to understand the "delegate BeginInvoke" call. I've covered that in other articles on threading, in particular, I refer you to "Threading with .NET ThreadPool Part 4" for a deeper discussion.

Overall, the work horse of this application is the Abstract Class TaskQueue. The class implements all that's needed to queue tasks and execute them in order. All the user need supply is a derived class that overrides "Task()" and a Constructor that passes in some UserData. The application would instantiate a new derived TaskQueue object with the data needed for the task and then call the Enqueue() method.

The TaskQueue works by creating 3 private static control members. There is the queue which holds delegates to run. There's also a Busy member to say when there is an active thread running. There is also a LockObject used internally to synchronize thread access to the the queue and the Busy indicator. This is needed because as one thread completes and tries to update the Busy indicator or take a new delegate off of the queue for execution, the "producer" thread (our UI in this case) may be trying to enqueue another delegate. Since two threads could be accessing these variables simultaneously, we synchronize them with a Mutex on the LockObject.

The multi-threading comes into play when a delegate's BeginInvoke(...) method is called. This method will allocate a thread from the ThreadPool and execute the Task in that thread. BeginInvoke is provided a TaskCallback() function and some state information. In this case, the state information is a reference to the delegate.

The Callback function is responsible for checking the queue and launching the next Task or setting Busy to false.

The magic in all of this is that the delegate is a reference to a specific instance of a TaskQueue object's Task() method. That way, the delegate's Task() method has access to local information about that specific task. So, each delegate will operate with its own version of UserData. Notice, this is also the reason that NextTask() dequeue's a delegate and places the reference in a local variable rather than the TaskDlgt member. TaskDlgt is a reference to one's self, while the delegate taken from the queue is a different delegate. So, each task's completion callback is responsible to start the next task (if any).

Also, notice that the Tasks are highly encapsulated. Once the task is on the queue, all that is available is the delegate. The rest of the task is somewhat hidden, though reflection can be used to make tweaks to the tasks and task data if necessary.

There are many improvements that can be made. Most notably, one could add a way to stop the queue and cancel remaining tasks. One could add controls to their derived class and overridden Task() member to allow individual task control or gross control over the entire queue.

There you have it, a "simple" Task Queue for your amusement and edification. Hopefully, you find it as useful as I have. Also, I have written several other articles on threads and threading. Please feel free to poke around my archives, you may find these articles useful as well.

10/6/2008: Recently, a kind reader pointed out a bug in my code (since fixed) where "Busy" was not being set to "true" anywhere. The obvious behavior was that all tasks ran immediately, no queing was done at all.

Thứ Bảy, 20 tháng 9, 2008

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’t use the Alternation, we can opt for my second approach. We can use the Negative Look-Ahead pattern.

(?!expression)

The negative look-ahead pattern is “non-consuming” and matches strings (or parts of string) that are not followed with the “expression”. You may wish to brush up on negative look-ahead with my article “Regular Expressions in C# - Negative Look-Ahead”. With negative look-ahead and using the OR pattern, we can use double-negative logic to express the original problem. This approach works based upon the logic axiom…

( A AND B ) == NOT ( (NOT A) OR (NOT B) )

Using this rule, the problem can be stated differently. We can state our problem in terms of ORs instead of ANDs. In other words...


If it is not the case that our string is devoid of digits or devoid of uppercase letters or short of 6 characters, then it is a valid string.


Notice I chose terms that suggest negative tests. Take a moment and think about it.

IF ( NOT ( NOT(Has Digit) OR NOT(Has Uppercase) OR NOT(6 or more chars) ) )

This has the same logic result as…

IF ( (Has Digit) AND (Has Uppercase) AND (6 or more chars) )

If you can’t get your mind around this basic programming concept, then re-writing your pattern requirement in terms of ORs will be difficult. So, experiment on paper and with generic logic variables to prove to yourself that your re-written pattern logic means the same thing as the original.

Now let’s look at the actual Regular Expression pattern. The component patterns from which we build look like this...

(^[^\d]*$) string is devoid of digits
(^[^A-Z]*$) string is devoid of uppercase letters
(^.{0,5}$) string has 5 or fewer characters

String them together to get the inner test ...

(^[^\d]*$)|(^[^A-Z]*$)|(^.{0,5}$)

Sometimes you can pause here and simply test for your string to match. If you supply an invalid string, it should MATCH. If you provide a valid string, it should NOT match. So, at this point, we have the opposite of what we really want. (A validator may not allow you to test this way, you would have to write a C# program to use as a “test jig”). So, we now negate the inner pattern with the negative look-ahead syntax...

(?!(^[^\d]*$)|(^[^A-Z]*$)|(^.{0,5}$))

This would work for the IsMatch() approach, but it won't quite work for the Match() approach. We have to add some bounds...

^.*(?!(^[^\d]*$)|(^[^A-Z]*$)|(^.{0,5}$)).*$

This pattern is much harder to decipher than the AND pattern using Alternation. But now, armed with the knowledge from these last two articles, you should be able to discern the meaning of such patterns. We should be able to plug this pattern in to all of the algorithms we covered in previous article and have it validate strings according to the requirement.

That’s all for now, I hope you enjoyed the discussion and find these examples useful.

Thứ Năm, 18 tháng 9, 2008

Unauthorized error after deploying your custom application

I've been working on deploying an application today. I received the error "Unauthorized" all the time and did not know why. The website was deployed as a new virtual directory within the ISV folder, authentication was set to integrated windows authentication and the password which was supplied was correct. Still I was able to get this error message:Server Error in '/ISV/CustomApplication'