Thursday 29 August 2013

Introduction to Microsoft .NET

The .NET Framework is a new computing platform that simplifies application development in the highly distributed environment of the Internet. The vision behind dot net is to provide a feature rich application development platform and a managed, protected execution environment. The current version of .NET framework SDK is 1.0. also the .NET framework SKD 1.1 beta is available.  A few goals of .NET are to:

§  Simplify application development
§  Simplify application deployment.
§  Provide a platform for building web services.
§  Enable universal access of applications from any device.

Simplify application development: Microsoft .NET makes development easier, as the framework provides a new set of tools, including a large set of object-oriented system level classes. The framework helps the developer by a rich set of classes and services that are more easily accessible than any previous windows-based set of APIs.

Simplify application deployment: Typical problems faced in deploying applications involving registering DLLs and the type library versions getting mixed up is addressed in .NET by making components self describing through meta data.

Provide a platform for building web services: The .NET initiative provides the tools and technology for creating and implementing XML web services. In this context an application is no longer tightly coupled, instead it is made up of loosely coupled distributed web services that inter-operate using XML and HTTP.


Enable universal access of applications from any device: Using .NET traditional applications can be rewritten as web services that deliver data as XML over HTTP, using the open standard SOAP. This is a very flexible way to exchange data because XML does not dictate how it is displayed. The use of XML in delivering data will enable communication between applications running on different devices. Each device in turn transforms the XML information into a presentation that is best suited to the device.

Sunday 19 February 2012

Console Output from a WinForms Application.

Console Output from a WinForms Application.

You may wish to enable your WinForms application to run from a console window or command line. And when it does, you probably want to send output messages to the console window that launched your WinForms application.

Unfortunately Console.WriteLine()–the standard method of writing to the console window–by default will not work from a WinForms application. That’s because the console window that launched your WinForms application belongs to the cmd.exe process, which is separate from your WinForms application process.

So to redirect output from a WinForms application to the console window that launched it, use the AttachConsole Win32 method introduced in Windows XP. AttachConsole attaches the current process to the console window of another process. The special parameter ATTACH_PARENT_PROCESS attaches to the parent process, which in this case is the console window that launched the WinForms application.

Simple Example

Following is a simple WinForms application that redirects its output to the console window that launched it:

using System; using System.Runtime.InteropServices; using System.Windows.Forms;  namespace MyWinFormsApp {     static class Program     {         [DllImport( "kernel32.dll" )]         static extern bool AttachConsole( int dwProcessId );         private const int ATTACH_PARENT_PROCESS = -1;          [STAThread]         static void Main( string[] args )         {             // redirect console output to parent process;             // must be before any calls to Console.WriteLine()             AttachConsole( ATTACH_PARENT_PROCESS );              // to demonstrate where the console output is going             int argCount = args == null ? 0 : args.Length;             Console.WriteLine( "nYou specified {0} arguments:", argCount );             for (int i = 0; i < argCount; i++)             {                 Console.WriteLine( "  {0}", args[i] );             }              // launch the WinForms application like normal             Application.EnableVisualStyles();             Application.SetCompatibleTextRenderingDefault( false );             Application.Run( new Form1() );         }     } }

One Gotcha

There is one problem with this approach. If you redirect the console window output to a text file, for example using the redirect arrow:

MyWinFormsApp.exe >output.txt arg1 arg2

In this case, output will not redirect to the “output.txt” text file as expected, but instead will continue to appear in the console window. Please comment if you have a solution to this issue.

Show Continuous Progress with .NET ProgressBar and MarqueeAnimationSpeed

For some operations such as logging on to a web site or downloading a web page, you may not know how long it will take the operation to finish. So instead of showing a progress bar with a specified percent complete, you can set the .NET ProgressBar to cycle continuously.

To make a ProgressBar cycle continuously, set the MarqueeAnimationSpeed property to a positive value (by default, it is set to 0). The value specifies the time period, in milliseconds, that it takes the progress block to scroll across the progress bar. A higher value results in a slower speed, and a lower value results in a faster speed. I’ve found that a value of 30 works pretty well. It’s also important to set the Style property to ProgressBarStyle.Marquee.

this.ProgressBar_Download.MarqueeAnimationSpeed = 30; this.ProgressBar_Download.Style = ProgressBarStyle.Marquee;

To stop the cycle, set the Marquee Animation Speed property to 0. To resume the cycle, set it to a positive value.