Use PromptBox to get simple user input in Silverlight

When we create Silverlight application we will run into a situation where we need to get very simple input from a user apart from traditional ok or cancel. If you are after the simple ok or cancel then message box serve the purpose but if you need to get some kind of data from the user as simple as a number or a string, then we do not need to create popup window or child window in Silverlight if you are using component one controls.

Out of the box, component provides a user control called C1PromptBox which as the name suggests, prompt the user for an input and goes on it merry way without cluttering code with complicated code.

Let’s look at this with simple example;

C1PromptBox.Show(“Enter your age”, “Age prompt”, (s, r) =>
{
If (r ==MessageBoxResult.OK)
{
//age will be in s, parse the age out
}
else
{
// user pressed cancel
}
}

If you expect the last two line to execute after getting user input, then if condition has to be inside the C1PromoptBox.Show bot outside like the code shown

above. The proper way to write the code

In the above example, when the code executes it will prompt the small message box to enter the age and you parse or handle the value on the event user press ok button.

One thing we need to be mindful, the Show event is a delegate so if you have any instructions that need to execute after prompt is successful, it has to be inside the MessageBoxResult.OK condition not outside the show delegate since UI will take control back after show event is successful.

Here is a scenario;

  1. public void PromptAge()
  2. {
  3. int age = 0;
  4. C1PromptBox.Show(… get age);
  5. if (age <= 30)
  6. HandleLessThanThirtyAge();
  7. }
 

In the above example we are expecting the dialog to be shown for user to enter the age. After the user enters the age, line 05 gets control and executes HandlesLessThanThirtyAge method if the age is less than or equal to 30. But that is not how it works. When PromptAge executes, it execute prompt box and while it wait for user to enter something, the UI thread continues and executes 05 through so age will be always 0 and it never executes the method on 06. The reason for that is, the behavior on how to handle ok or cancel is delegate inside the prompt box. So for it work properly. ie, after user enters the ages, get the age and based on the age to execute the HandleLessThanThirtyAge, we need to move the if condition inside the PromptBox delegate itself as shown below

  1. public void PromptAge()
  2. {
  3. C1PromptBox.Show("msg", "prompt", (s, r) =>
  4. {
  5. if (r == MessageBoxResult.OK)
  6. {
  7. //parse age from s
  8. if (age <= 30)
  9. HandleLessThanThirtyAge()
  10. }
  11. };
  12. }
One of the feature missing in this is default value for the prompt, I requested this feature with Component One and it is schedule be part of their feature release. By providing most commonly used value as default value, we could save one key stoke for the user. Once the feature is available I will update this blog.

Debugging infinite loops with Visual Studio

Once in a while we run into a situation where the program run into infinite loop. Best symptom is the application seemed to be frozen. There are lot of different ways to approach it. One simple way is to put as much log as possible and dump it to a log file and have a UDP log viewer like log4net viewer and see what is happening. I like this approach, I call it layback debugging. This requires you need to know where this loop is happening and add as much log as possible. By the way there is nothing wrong with having more logs, but remember to make sure production is not noisy with all these logs. There are other brute force debugging methods like message boxes, putting break point and walking the code etc.,

Recently I ran into a situation where one of our Silverlight application fezzes after some time, there is no special pattern so we do not know exactly where it is happening but we had a ball park idea. To identify the problem area I used Visual Studio rather than logging or message box approach. So in our example, I am going to debug a Silverlight application which might be freezing after some work.

To do this, start the application and keep running it till it freezes. Now launch Visual Studio and open the solution of the application.

Goto Debug -> Attach to process (make sure ‘Show process in all sessions’ is checked) This will open a window and there scroll down and identify the process that you want to attach debugger to. In our case type will be ‘Siverligt’

Now that the frozen application is attached to the debugger, we can put break point and find out where the application is failing. But we do not know where is the current process is. If we do not know where the current process is how do we go about find out where the execution is? The answer is break at the current execution line, this can be achived by

Going to Debug -> Break All, this will stop at the current executing line but it will not stop the execution of the program, which means everything you could do in debugger mode is available to you at this time and you can resume execution by pressing F5 (Run). I would recommend not to use the first line where the program might be as the culprit causing it to go in the loop, rather, press F5 (Run) again and let it run and then break all again. See where is application breaks, keep doing it couple of times, this should give you a very good idea which part of the code might be the culprit for the infinite loops. The beauty of this is, it gives you the call stack as well, so you can see how the execution got to that state as well.

Visual Studio has ton features to help debug code and if you want to learn more about how to use these tools and features, head to Microsoft Debugging in Visual Studio.

Hope this help somebody. If there are better ways of debugging or anything related to this subject please send me a note, I love to hear it.