Saturday 4 June 2016

Call a Exe from another Console Application and pass the input arguments

In this post we are going to see how to call a exe form the console application and pass the input arguments.

For this first we will create a Exe using the console application then later we will invoke that application from another application and passing the input paramteres.

First Console Application:

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Reading command line arguments");

            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine(string.Format("Arg {0}, Value {1} ", i, args[i]));

            }

            Console.Read();
        }
}

In this code you can see that command line arguments is starts with 0, because in Winforms 0 index have exe path , from 1 index we can iterate the input values passed from another application

Second Console Application:
Create a another Console application project and paste the following code in the main.cs

            Process pro1 = new Process();
            pro1.StartInfo.FileName @"C:\ParamApp\ParamConsole\bin\Debug\ParamConsole.exe";
            pro1.StartInfo.Arguments = "Rajesh suresh hanish/";
            pro1.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            pro1.Start();
            
          
            pro1.WaitForExit();


output:
***********
Console 1 app is running from Console 2 App




From this post you can see how to create a Application to call an Exe and pass the input paramteres.

Call Winforms Forms application from console application and pass the input parameters

In this post we are going to see how to call a winforms application from a console application and pass the input paramteres to the winforms

First we develop a Winforms application with one form where it will have two textbox which is a username and password.

Winforms Design:
******************





Winforms Code :
******************

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var data = Environment.GetCommandLineArgs();

            if (data.Length == 3)
            {
                textBox1.Text = data[1];
                maskedTextBox1.Text = data[2];
            }
        }
    }

In the above you can see that i am assigning the index 1 and 2 , because on 0 index CommandLingArgs will have the path of the Executable in winforms 



Console Application :
******************
Create a new project in console application, then create the following code , which will invoke the instance of a process.

            Process pro = new Process();
            pro.StartInfo.FileName = @"C:\amApp\ParamApp\bin\Debug\ParamApp.exe";
            pro.StartInfo.Arguments = "Rajesh Password";
            pro.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            pro.Start();
            pro.WaitForExit();


Output:
***********





In this post we can see how to call a winforms application from the Console application

Thursday 2 June 2016

Microsoft Bug An UnHandled Microsoft .Net Framework Exception occured in devenv.exe in design mode of winforms forces the Visual Studio to Close

In this post we are going to see a error which forces the development environment crashes and  close because of user code and makes the developers to stand in a some crucial situation.



Click Here to see bug info
https://connect.microsoft.com/VisualStudio/feedback/details/2769909/an-unhandled-microsoft-net-framework-exception-occured-in-devenv-exe-in-design-mode-of-winforms-forces-the-visual-studio-to-close














    Normally Development environment DevEnv.exe is used to develop the code which needs to run in windows Machines , ya it is Visual studio exe. The purpose of this Development environment is used to give a users a sophisticated way for development like Giving intellisense, showing performance reports, Exeception handling like capturing the errors , notifying to users in run time as well as in design time. In some cases it is fails Let we see the cases where it is failed


Example Design time errors capture in Component in Visual studio while development










     Developing a component is usually a good idea in a application, because we can add anything to the control what ever we thought, like wise developing we have to check each and every stage of that control by dragging it in to Form and see the appearance and properties.After finishing the development we will run the Form and see the visual appearance of the control in the UI.



     When i am developing a custom control , when i tried to drag and drop and see the UI, it looks good . Then later again i changes something in the  Control. Now i got an error says "Not implemented" in the  control which is placed on the form, Then i implemented that functionality, Then again the control in the UI, shows another error in design time like Null Reference, again i implemented that steps for the Control, Again it works correctly, so whenever we design a control development environment visual studio will capture the design time errors and show it in Control itself in design.








    But in this case Now the Form Freezes and closes after a certain time, again when ever i open the form which have that custom control crashes the DEvEnv.exe. I get wondered how The development can gets crashes for a user code which is in development mode, because devenv.exe will capture the design time errors and show it in UI, because then only developers can develop the thing in that development editor , that is the purpose of that editor.









     When the UI freezes , it launches a pop screen with  a message that An UnHandles Microsoft .Net Framework Exception occured in devenv.exe in design mode of winforms, Ya here i am using the Winforms as platform.







            I raised a issue to microsoft , i know that my code is buggy, because it is still under development, but how editor gets closed for used buggy code in development, if it is runtime it is acceptable because we are running in CLR, but in design editor should handle everything and shows it to users













  


  After raising the issue i got a feed back from microsoft like below, it is ideally should not crash visual studio but happening in this case, they are saying cases for unhandling the exceptions.



Click Here to see bug info
https://connect.microsoft.com/VisualStudio/feedback/details/2769909/an-unhandled-microsoft-net-framework-exception-occured-in-devenv-exe-in-design-mode-of-winforms-forces-the-visual-studio-to-close

















Note : 
We cant rely on users code to be correct while developing or in design mode, because it is under changes, so devenv.exe should capture the errors in design mode and show in control in design mode, if it is runtime or running the app we can say that we are rely on users code to be correct for stack overflow exceptions .... 




From this post you can see a Microsoft Bug which persists in visual studio , which is an unhandled exception






Wednesday 1 June 2016

Convert the Non Nullable types to Nullable Types and Nullable Types to Non Nullable Types in C#

In this post we are going are going to see how to convert a Non Nullable Types and Nullable Types to Non Nullable Types in C#. Generally in C# programming there might be a chance that we have to assign a value from Non Nullable to Nullable or Nullable to Non Nullable , to do it easily now we are going to see an Extension Method , which can be useful very handy.


public static class Extensions
    {
        public static T ConvertTypeTo<T>(this object obj)
        {
            Type typ = typeof(T);
            Type Ntype = Nullable.GetUnderlyingType(typ);
                
             if (obj == null)
                    return default(T);
if (Ntype == null) { return (T)Convert.ChangeType(obj, typ); } else { return (T)Convert.ChangeType(obj, Ntype); } } public static T? ToNullable<T>(this object x) where T:struct { return x == null ? null : (T?)Convert.ChangeType(x, typeof(T)); } }



We can use this Extension class like below, this method will reflect in each and every object.


class Program
    {
        public static Nullable<bool> IsLogin { set; get; }

        static void Main(string[] args)
        {
            int total = 10;
            bool ischeck = false;
            

            Nullable<int> res1 = total.ConvertTypeTo<Nullable<int>>();
            int res3 = res1.ConvertTypeTo<int>();

            if (res1.HasValue)
            Console.WriteLine(res1);

            Console.WriteLine(res3);

            Nullable<bool> res2 = ischeck.ToNullable<bool>();

            Console.WriteLine(res2.Value);

            bool _login = IsLogin.ConvertTypeTo<bool>();

            Console.WriteLine(_login);

            Console.Read();

        }
    }



The output of this code is 



From this post you can see how to convert a Nullable to NonNullable Types, Non Nullable Types to Nullable