Tuesday, August 7, 2012

C# Programming Tips

Calling a new process within a process:

Process commandLineProc = Process.Start(“FileName”, “Argument1 Arguemtn2”);
Process oMyOwnProcess = Process.Start("MyOwnProcessExePath");

Not allowing to run multiple processes at the same time:

In a windows app, if we want to allow only one instance of the app, we could do easily in program.cs as following:

static class Program
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
            // -------------- Checking how many instances are running of the same application ------------

            System.Diagnostics.Process oCurrentProcess = System.Diagnostics.Process.GetCurrentProcess();
            System.Diagnostics.Process[] arrProcess = System.Diagnostics.Process.GetProcessesByName(oCurrentProcess.ProcessName);
            if (arrProcess.Length > 1)
                MessageBox.Show("Unable to start multiple applications.", "App Already Running", MessageBoxButtons.OK, MessageBoxIcon.Error);
            // --------------------------------------------------------------------------------------

            Application.Run(new MyForm());

Converting value to Enumeration:
MyEnumeration oMyEnumeration = (MyEnumeration)Enum.Parse(typeof(MyEnumeration), MyStringToConvert);

 Binary To Decimal conversion:  

 string binaryNum = "101";
 int decimalNum = Convert.ToInt32(binaryNum , 2);

 Decimal to Binary conversion:

int decimalNum = 42; string binaryNum = Convert.ToString(decimalNum, 2);
  Checking Stored Procedure Exists or not:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StoredProcedureName]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[StoredProcedureName]

Continued …

No comments:

Post a Comment