Saturday, July 14, 2012

Visual Studio Debug VS Release mode



When we want to deploy our web application to live/local server, then we have two options for making built – Release mode and Debug mode. Both the modes have own importance and characteristics. The details about Release mode and Debug mode are as:

Debug Mode

Developer use debug mode for debugging the web application on live/local server. Debug mode allow developers to break the execution of program using interrupt 3 and step through the code. Debug mode has below features:
  1. Less optimized code
  2. Some additional instructions are added to enable the developer to set a breakpoint on every source code line.
  3. More memory is used by the source code at runtime.
  4. Scripts & images downloaded by webresource.axd are not cached.
  5. It has big size, and runs slower.

Release Mode

Developer use release mode for final deployment of source code on live server. Release mode dlls contain optimized code and it is for customers. Release mode has below features:
  1. More optimized code
  2. Some additional instructions are removed and developer can’t set a breakpoint on every source code line.
  3. Less memory is used by the source code at runtime.
  4. Scripts & images downloaded by webresource.axd are cached.
  5. It has small size, and runs fast.

Note

There is no difference in functionality of a debug dll and a release dll. usually, when we compile code in debug mode, we have a corresponding .pdb (program database) file. This .pdb file contains information that enables the debugger to map the generated IL (intermediate language) to source code line number. It also contains the names of local variables in the source code.

Set Compilation Mode in Visual Studio

We can set the compile mode in visual studio as shown in below fig.

Summary
In this article I try to explain, release mode advantage over debug mode. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Visual Studio 2008, 2010, 2011, 2012 Shortcuts Keys


Shortcut keys play an important role to increase the output. Developers work at least 6 to 8 hours on daily basis on Visual Studio. If the developer knows the shortcut keys, he can keep away from using of mouse and absolutely it will enhance productivity and reduce headache to use of mouse again and again. I am sharing some interesting and useful shortcut keys to work with Visual Studio.
General Shortcut Keys
Shortcut
Description
Ctrl-X or Shift-Delete or Ctrl-L
Cuts entire line or Cuts the currently selected item to the clipboard
Ctrl-Del
Delete next "word"
Ctrl-C or Ctrl-Insert
Copies the currently selected item to the clipboard
Ctrl-V or Shift-Insert
Pastes the item from the clipboard at the cursor location
Ctrl-Z or Alt-Backspace
Undo the previous editing action
Ctrl-Space
To see intelligence dialog
Ctrl-Y or Ctrl-Shift-Z
Redo the previous undo action
Ctrl-S
Saves the current selected file
Ctrl-Shift-S
Saves all files and projects
Ctrl-P
Displays the Print dialog
F7
Switches from the design view to the code view in the editor
Shift-F7
Switches from the code view to the design view in the editor
Shift-F8 or F8
Navigate to compile time errors
Alt-Shift-A
Add Existing Item(file) to selected project
Ctrl-Shift-A
Add New Item(file) to selected project
Shift-F9
Display the selected item quick output means contains value while debugging
F12
Moves the cursor to the selected method, variable, class definition.
Shift-F12
Finds the reference to the selected method, variable, class or the item under the cursor
Ctrl-}
Match curly braces, brackets or compiler directives
Ctrl-Shift-}
Select text between matched braces, brackets or compiler directives
Text manipulation
Shortcut
Description
Tab
Indents the currently selected line or lines by one tab stop. If there is no selection, this inserts a tab stop
Tab-Tab
Complete the syntax of an item. Example- Write for and now press Tab-Tab, then for loop syntax will be automatically completed. Similarly it works for property, index, class syntax.
Shift-Tab
Moves current line or selected lines one tab stop to the left
Backspace or Shift-Backspace
Deletes one character to the left of the cursor
Ctrl-K, Ctrl-C
Marks the current line or selected lines of code as a comment, using the correct comment syntax for the programming language
Ctrl-K, Ctrl-U
Removes the comment syntax from the current line or currently selected lines of code
Ctrl-K, Ctrl-D
Do proper alignment of all the code of design view and code view
Ctrl-T or Shift-Enter
Swaps the characters on either side of the cursor. (For example, AC|BD becomes AB|CD.) Available only in text editors
Ctrl-U
Changes the selected text to lowercase characters
Ctrl-Shift-U
Changes the selected text to uppercase characters
Ctrl-Shift-Right Arrow
Select the entire word with space from start to end
Ctrl-Right Arrow
Move the cursor in forward direction word by word
Shift-End
Select the entire line from start to end
Shift-Home
Select the entire line from end to start
Shift-Alt-Arrows or Alt-Mouse
Column wise text selection for text manipulation
Ctrl-Delete
Deletes the word to the right of the cursor
Ctrl-Backspace
Deletes the word to the left of the cursor
Ctrl-G
Jump to line number or go to line
Project related
Shortcut
Description
Ctrl-Shift-B
Builds the solution
Ctrl-N
Displays the New File dialog. Note: files created this way are not associated with a project. Use Ctrl-Shift-A to add a new file in a project
Ctrl-Shift-N
Displays the New Project dialog
Ctrl-O
Displays the Open File dialog
Ctrl-Shift-O
Displays the Open Project dialog
Ctrl-M-O
Collapse all the methods, classes, regions in the current code behind or class file
Ctrl-M-P or Ctrl-M-L
Expands all the methods, classes, regions in the current code behind or class file
Ctrl-F
Displays the Find dialog
Ctrl-H
Displays the Replace dialog
Ctrl-Shift-F
Find the reference of selected item into entire solution.
Ctrl-Tab
Move from one opened file to another opened file in visual studio.
F9
Sets or removes a breakpoint at the current line
Ctrl-F9
Enables or disables the breakpoint on the current line of code. The line must already have a breakpoint for this to work
F5
Runs the code with invoking the debugger.
Ctrl-F5
Runs the code without invoking the debugger.
F4 or Alt-Enter
Displays the Properties window, which lists the design-time properties and events for the currently selected item
Ctrl-Alt-S
Displays the Server Explorer window, which allows you to view and manipulate database servers, event logs, message queues, web services, and many other operating system services
Ctrl-Alt-L
Displays the Solution Explorer, which lists the projects and files in the current solution
Ctrl-Alt-X
Displays the Toolbox, which contains controls and other items that can be dragged into editor and designer windows
Ctrl-Alt-I
Displays the Immediate window, where you can find the controls or variables values or can do data manipulation during debugging

Tuesday, July 10, 2012

Refresh / Update WPF controls


Sometime in the past, a friend asked me how to update a control to show status while his code is doing a loop of stuff.  Essentially changing the text of a label (or sophisticatedly we can say a text-based progress bar).  In my past coding with MFC and WinForms, it's fairly easy enough, you just invalidate and do an update (Invalidate / UpdateWindow in MFC or Invalidate / Update in WinForms).  This approach also coincides with how Windows UI operate, where you specify the region that needs to be redrawn and then you send a message to the message pump for that control to paint itself.

So, I expected something similar (if not exactly the same) to also be present in WPF; much to my surprise, there is no equivalent.   All my internet searches actually shows how to do this using background thread - it is the approach that needs to be taken in a proper programming context, however there are times when you just want to do something quick & dirty or you want to augment an existing app / port where you don't want to introduce new elements.  There are also considerations to be made when both UI and worker thread access the same data, especially with regard to data binding.

So, I've decided to add a helper method to refresh a WPF control.  I really appreciated the Refresh method in WinForms (which executes both Invalidate & Update), so I'm renaming my method to be Refresh as well.  The code snippet below also show some C# specific techniques, namely: anonymous delegates and extension methods.


public static class ExtensionMethods
{
   private static Action EmptyDelegate = delegate() { };
   public static void Refresh(this UIElement uiElement)
   {
      uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
   }
}
private void LoopingMethod()
{
   for (int i = 0; i < 10; i++)
   {
      label1.Content = i.ToString();
      label1.Refresh();
      Thread.Sleep(500);
   }
}

The LoopingMethod is just the method I use in my Window class to update the label (updating the progress) and then the code does some heavy lifting (Sleep ).  The Refresh method is the extension method that takes any UI element and then calls that UIElement's Dispatcher's Invoke method.  The trick is to call the Invoke method with DispatcherPriority of Render or lower.  Since we don't want to do anything, I created an empty delegate.  So how come this achieves refresh functionality?

When the DispatcherPriority is set to Render (or lower), the code will then execute all operations that are of that priority or higher.  In the example, the code already sets label1.Content to something else, which will result in a render operation.  So by calling Dispatcher.Invoke, the code essentially asks the system to execute all operations that are Render or higher priority, thus the control will then render itself (drawing the new content).  Afterwards, it will then execute the provided delegate (which is our empty method).

Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts

DotNetJalps