private void buttonloadhex_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();


            New_Firmware_Hex = new HexFile();

            // Set filter for file extension and default file extension 
            dlg.DefaultExt = ".hex";
            dlg.Filter = "HEX Files (*.hex)|*.hex";
            dlg.InitialDirectory = @"C:\Users\astark\MPLABXProjects\MCP2515DM-BM_Firmware\Source\dist\default\production";


            // Display OpenFileDialog by calling ShowDialog method 
            Nullable<bool> result = dlg.ShowDialog();

            int i = 0;
            // Get the selected file name and display in a TextBox 
            if (result == true)
            {

                try
                {
                    New_Firmware_Hex.LoadFile(dlg.FileName);

                    ListboxPIC.addItem(" ");
                    ListboxPIC.addItem(string.Format("New Hex File : {0} is loaded", New_Firmware_Hex.Filename.Substring(New_Firmware_Hex.Filename.LastIndexOf('\\') + 1)));
                    while (New_Firmware_Hex.HexFileLines[i].Address < 19)
                    {
                        i++;
                    }
                    i++;
                    ListboxPIC.addItem(string.Format("New Firmware Start Adress : 0x{0:X4}", New_Firmware_Hex.HexFileLines[i].Address));
                    ListboxPIC.addItem(string.Format("New Firmware Size : {0} Bytes", New_Firmware_Hex.Filesize));
                    ListboxPIC.addItem(string.Format("Number of Errors in Checksum : {0} ", New_Firmware_Hex.ErrorCounter));
                    if (New_Firmware_Hex.ErrorCounter != 0)
                    {
                        ListboxPIC.addItem("There are Errors detected in the Checksum of the Hex File ! ");
                    }

                    Listbox_Scroll_End(ListboxPIC);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Fehler '{0}' beim Laden der Datei", ex.Message));
                    //return 1;
                }
                // Open document 

            }

        }
        public MainWindow()
        {
            InitializeComponent();
               
               USB_Event.DoWork += USB_Event_DoWork;
               USB_Event.RunWorkerCompleted +=USB_Event_RunWorkerCompleted;

               USB_Data_Gatherer.DoWork += USB_Data_Gatherer_DoWork;
               USB_Data_Gatherer.WorkerReportsProgress = true;
               USB_Data_Gatherer.WorkerSupportsCancellation = true;
               USB_Data_Gatherer.ProgressChanged += USB_Data_Gatherer_ProgressChanged;
               USB_Data_Gatherer.RunWorkerCompleted += USB_Data_Gatherer_RunWorkerCompleted;

               SQLRecord_Inserter.DoWork += SQLRecord_Inserter_DoWork;
               SQLRecord_Inserter2.DoWork += SQLRecord_Inserter_DoWork;

               CSV_Importer.DoWork += CSV_Importer_DoWork;
               CSV_Importer.WorkerReportsProgress = true;
               CSV_Importer.WorkerSupportsCancellation = true;
               CSV_Importer.ProgressChanged += CSV_Importer_ProgressChanged;

               Baud_Rate_Detect.DoWork += Baud_Rate_Detect_DoWork;
               Baud_Rate_Detect.WorkerReportsProgress = true;
               Baud_Rate_Detect.WorkerSupportsCancellation = true;
               Baud_Rate_Detect.ProgressChanged += Baud_Rate_Detect_ProgressChanged;
               Baud_Rate_Detect.RunWorkerCompleted += Baud_Rate_Detect_RunWorkerCompleted;


               Device_Programer.DoWork += Program_Bootloader_device;
               Device_Programer.ProgressChanged += Device_Program_Progess_changed;
               Device_Programer.WorkerReportsProgress = true;
               
               Datagrid_Filler.DoWork += Datagrid_Filler_DoWork;
               Datagrid_Filler.RunWorkerCompleted += Datagrid_Filler_RunWorkerCompleted;

               CAN_Device_Notifications = new CAN_Monitor_Classes.Connection_Monitoring();
               CAN_Device_Notifications.SetRecording(false);
               CAN_Device_Notifications.SetConnection(false);
               CAN_Device_Notifications.SetConfigMode(false);
               CAN_Device_Notifications.SetisPrograming(false);
               CAN_Device_Notifications.SetNormalMode(false);

               DataContext = CAN_Device_Notifications;

               CAN_Device = new CAN_Monitoring_Device(0x04D8, 0x0070);
               Bootloader_Device = new Bootloader_Device(0x04D8, 0x003C);
               New_Firmware_Hex = new HexFile();
               timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
               timer.Interval = 1000;
               

               
               // Add a listener for usb events
               CAN_Device.usbEvent += new CAN_Monitoring_Device.usbEventsHandler(usbEvent_receiver);
               Bootloader_Device.usbEvent += new CAN_Monitoring_Device.usbEventsHandler(usbEvent_receiver);


               // Perform an initial search for the target device
               CAN_Device.findTargetDevice();
               Bootloader_Device.findTargetDevice();
               
    

        }