Tuesday, November 22, 2011

Windows Application Error Handling & Check Allready Application Running

Windows Application Error Handling :

In the windows entire application or while opening an application if you get any exception means the below code will help you to capture the error log. The application error or exception handling you can identify the error message. This will help you to understand the problem in your windows application using C#.Net. The below code is start up point of the win application. The program.cs class only first fire in the application.



Application Error Log


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
 
namespace MyProject
{
    static class Program
    {
        /// 
        /// The main entry point for the application.
        /// 

 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
          
            Control.CheckForIllegalCrossThreadCalls = true;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
 
            //get the name of current process, i,e the process 
            //name of this current application
 
            string currPrsName = Process.GetCurrentProcess().ProcessName;
 
            //Get the name of all processes having the 
            //same name as this process name 
            Process[] allProcessWithThisName= Process.GetProcessesByName(currPrsName);
 
            //if more than one process is running return true.
            //which means already previous instance of the application 
            //is running
            if (allProcessWithThisName.Length > 1)
            {
                MessageBox.Show("The Application Already Running""MY APP"MessageBoxButtons.OK);
                Application.Exit();
               // allProcessWithThisName[0].Kill();
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
            else
            {  
                frmMain mainForm = new frmMain(); //this takes ages 
                Application.Run(mainForm);
            }
        }
 
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject != null)
                ErrorLog((Exception)e.ExceptionObject);
        }
 
        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            if (e.Exception != null)
                ErrorLog(e.Exception);
        }            
 
    }
}





 //Windows application Error Log

using System.IO;
       
 public static void ErrorLog(string sMessage)
        {
            StreamWriter objSw = null;
            try
            {
                string sFolderName = Application.StartupPath + @"\Logs\";
                if (!Directory.Exists(sFolderName))
                    Directory.CreateDirectory(sFolderName);
                string sFilePath = sFolderName + "Error.log";

                objSw = new StreamWriter(sFilePath, true);
                objSw.WriteLine(DateTime.Now.ToString() + " " + sMessage + Environment.NewLine);

            }
            catch (Exception ex)
            {
                Comman.ErrorLog("Error -" + ex.Message);
            }
            finally
            {
                if (objSw != null)
                {
                    objSw.Flush();
                    objSw.Dispose();
                }
            }
        }





Check Allready Application Running:

string currPrsName = Process.GetCurrentProcess().ProcessName;

//Get the name of all processes having the
//same name as this process name
Process[] allProcessWithThisName
= Process.GetProcessesByName(currPrsName);

//if more than one process is running return true.
//which means already previous instance of the application
//is running
if (allProcessWithThisName.Length > 1)
{
MessageBox.Show("Application Already Running", "Test", MessageBoxButtons.OK);
Application.Exit();
}
else
{
Application.Run(new frmMain());
}

-->

No comments: