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.

Sunday, 5 February 2012

Jobs for .Net Freshers in Hyderabad.


Company        Magna Infotech Pvt.Ltd
Website          www.magna.in
Eligibility        B.E/B.Tech, MCA

Experience     2-4 Years

Location         Hyderabad
Magna Infotech Pvt.Ltd
Job Role: Software Engineer/ Programmer - .Net Developer With 2-4 years Exp
Job Summary:Ability to come out with good technical solutions

Ability to troubleshoot critical technology issues

Good knowledge of App server, Web server, DB server tuning

Knowledge of at least one Code profiling tools

Ability to guide team on technology