Multithreading problems in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DiDoria
    New Member
    • Mar 2007
    • 21

    Multithreading problems in C#

    hey,

    When I run my application in C# with 2threaders (one for the GUI and one for met DAL) it works pefectly fine on my Windows XP professional. But when I run the same program on a windows 2000 (on a VMware) system I get an error when my thread starts (a memorie error and I can not try catch it because its a real windows error (windows asks me to debug the application). Is there something wrong with my programma or is it the VMware?

    thx!!!
  • DiDoria
    New Member
    • Mar 2007
    • 21

    #2
    no body?...

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      Welcome to the site. Please post the relevant code. My money is on the code rather than VMWare. Are there any other differences in platform between the two system, e.g IIS & Framework versions?

      Comment

      • DiDoria
        New Member
        • Mar 2007
        • 21

        #4
        hey,

        thx for the reply:

        the code is a lot but I'll try,

        In my ProgressPanel (that is attached to a FORM I call progressAll() from another Panel just be switching to that panel.

        Code:
        public void processAll()
                {
                    try
                    {
                        rootWindow.btnNext.Enabled = false;
                        thread = new Thread(new ThreadStart([B]ApplicationDirector.Instance.processAll[/B]));
                        thread.Start();
                        new Thread(new ThreadStart(blockNextButton)).Start();
                        rootWindow.btnNext.Enabled = false;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("An Error Occured the file processing.", "Error during processing!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        log.Error("An Error Occured the file processing.", ex);
                    }
                }
        I also start an other thread to see when the thread ends. When the thread ends it wil enable the "NEXT" button and to some other stuff

        Code:
                public void blockNextButton()
                {
                    try
                    {
                        while (thread.IsAlive)
                        {
                            Thread.Sleep(2000);
                            if (thread.IsAlive)
                                thread.Suspend();
                            Thread.Sleep(5);
                            if (thread.IsAlive)
                                thread.Resume();
                        }
                        rootWindow.btnNext.Enabled = true;
                        rootWindow.btnNext.Focus();
                        pgbGatheringData.Visible = false;
        
                        ApplicationDirector.Instance.writeToGui("Removing temporary files... ");
                        log4net.LogManager.Shutdown();
                        //Directory.Delete(Constants.rootDir,true);
                        ApplicationDirector.Instance.writeToGui("Temporary files deleted... ");
           
                        reportPanel.fillReportScreen();
                        ApplicationDirector.Instance.writeToGui("Data collection finished! ");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("An error occured during removing files : \r\n\r\nSome files could not have been deleted in " + Constants.rootDir + "\r\n\r\nException : "+ ex, "Error Removing Files!",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                }
        Like you can see when I start my thread I call "ApplicationDir ector.Instance. processAll" With is my Facade and the code of that methode is (I know its al long methode but I also checks if the right option in de GUI *selectedOption List* is selected en when It's selected you can execute it):

        Code:
        internal void processAll()
                {
                    try
                    {
                        bool proccess = false;
                        foreach (string executableName in executableNames)
                        {
                            if (selectedOptionList.Contains(executableName))
                                proccess = true;
                        }
                        if (proccess == true)
                        {
                            logProgress("Collecting interfaces...");
                            [B]executables = WindowsSystemScan.Instance.Executables;[/B]                    logProgress("Interfaces Collected...");
        
                            foreach (Executable executable in executables)
                            {
                                if (selectedOptionList.Contains(executable.GetType().Name))
                                {
                                    logProgress("Interface \"" + Path.GetFileNameWithoutExtension(executable.FileName) + "\" is being processed.");
                                    Logger.Instance.FoundedInterfaces.Add(Path.GetFileNameWithoutExtension(executable.FileName));
        
                                    if (executable.GetType().Name.Equals("NormalExecutable") && selectedOptionList.Contains("NormalExecutableDB"))
                                        ((NormalExecutable)executable).WriteDBinfo = true;
        
                                    executable.callHandler(executable);
                                }
                            }
                        }
                        proccess = false;
                        foreach (string dbName in dbNames)
                        {
                            if (selectedOptionList.Contains(dbName))
                                proccess = true;
                        }
                        if (proccess == true)
                        {
                            logProgress("Processing database information...");
                            foreach (string selectedOption in selectedOptionList)
                            {
                                switch (selectedOption)
                                {
                                    case "PL/SQLTriggers":
                                        logProgress("Triggers are being processed.");
                                        GeneralInfoDBManager.Instance.processTriggers();
                                        break;
                                    case "PL/SQLFunctions":
                                        logProgress("Functions are being processed.");
                                        GeneralInfoDBManager.Instance.processFunctionFiles();
                                        break;
                                    case "PL/SQLProcedures":
                                        logProgress("Procedures are being processed.");
                                        GeneralInfoDBManager.Instance.processProcedureFiles();
                                        break;
                                    case "PL/SQLPackages":
                                        logProgress("Packages are being processed.");
                                        GeneralInfoDBManager.Instance.processPackageFiles();
                                        break;
                                    case "GeneralInfo":
                                        logProgress("Extra interface information is being processed.");
                                        GeneralInfoDBManager.Instance.processVersionFile();
                                        GeneralInfoDBManager.Instance.processAllDbFiles();
                                        break;
                                }
                            }
                        }
                        logProgress("All data is collected...");
                        logProgress("Creating zip-file...");
                        Zip.Instance.zip(Constants.rootDir);
                        logProgress("Zip-file created...");
                        if (Logger.Instance.ZippingEnded == false && Logger.Instance.MailZipFile == true)
                        {
                            UploadZipFile();
                        }
                        else
                        {
                            if (Logger.Instance.ZippingEnded == true)
                                logProgress("Zip-file was not created!");
                            if (Logger.Instance.MailZipFile == false)
                                logProgress("Zip-file was not mailed.");
                            Logger.Instance.ZippingEnded = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        logProgress("Error during processing files.");
                        log.Error("Error during processing files.", ex);
                    }
                }
        The ERROR occurs in the class executables = "WindowsSystemS can.Instance.Ex ecutables;" but somethings in the beginning of the class sometimes an the end. He gives a memorie exception in windows I thing which I cant try, catch.
        The strangest thing is. When I call "WindowsSystemS can.Instance.Ex ecutables;" BEFORE I start my thread in my panel he doesn't give an error but gives the error in the next class :p

        I thank you for your time :) . I can also email you the application (it's 42 classes but only 949kb)

        Comment

        • DiDoria
          New Member
          • Mar 2007
          • 21

          #5
          I also have these warnings, can that be the reason? But something that works on XP should also work in 2000, not?

          Code:
          Warning	1	'System.Runtime.InteropServices.FILETIME' is obsolete: 'Use System.Runtime.InteropServices.ComTypes.FILETIME instead. http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\ShellLinkNative.cs	44	25	Data Collector II
          Warning	2	'System.Runtime.InteropServices.FILETIME' is obsolete: 'Use System.Runtime.InteropServices.ComTypes.FILETIME instead. http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\ShellLinkNative.cs	45	25	Data Collector II
          Warning	3	'System.Runtime.InteropServices.FILETIME' is obsolete: 'Use System.Runtime.InteropServices.ComTypes.FILETIME instead. http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\ShellLinkNative.cs	46	25	Data Collector II
          Warning	4	'System.Runtime.InteropServices.FILETIME' is obsolete: 'Use System.Runtime.InteropServices.ComTypes.FILETIME instead. http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\ShellLinkNative.cs	62	25	Data Collector II
          Warning	5	'System.Runtime.InteropServices.FILETIME' is obsolete: 'Use System.Runtime.InteropServices.ComTypes.FILETIME instead. http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\ShellLinkNative.cs	63	25	Data Collector II
          Warning	6	'System.Runtime.InteropServices.FILETIME' is obsolete: 'Use System.Runtime.InteropServices.ComTypes.FILETIME instead. http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\ShellLinkNative.cs	64	25	Data Collector II
          Warning	7	'log4net.Appender.FileAppender.FileAppender(log4net.Layout.ILayout, string)' is obsolete: 'Instead use the default constructor and set the Layout & File properties'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\ApplicationDirector.cs	35	41	Data Collector II
          Warning	8	'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\ApplicationDirector.cs	149	60	Data Collector II
          Warning	9	'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\ApplicationDirector.cs	150	59	Data Collector II
          Warning	10	'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\ApplicationDirector.cs	151	59	Data Collector II
          Warning	11	'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\InternetCommunication.cs	228	17	Data Collector II
          Warning	12	'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\InternetCommunication.cs	228	39	Data Collector II
          Warning	13	'System.Web.Mail.MailFormat' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage.IsBodyHtml. http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\InternetCommunication.cs	233	34	Data Collector II
          Warning	14	'System.Web.Mail.SmtpMail' is obsolete: 'The recommended alternative is System.Net.Mail.SmtpClient. http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\InternetCommunication.cs	237	17	Data Collector II
          Warning	15	The variable 'ex' is declared but never used	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\WindowsSystemScan.cs	201	21	Data Collector II
          Warning	16	'System.Threading.Thread.Suspend()' is obsolete: 'Thread.Suspend has been deprecated.  Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources.  http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\gui\ProgressPanel.cs	134	25	Data Collector II
          Warning	17	'System.Threading.Thread.Resume()' is obsolete: 'Thread.Resume has been deprecated.  Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources.  http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\gui\ProgressPanel.cs	137	25	Data Collector II
          Warning	18	The private field 'InterfaceCollector.domain.DAL.DirectoryManager.qbridgePath' is never used	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\DirectoryManager.cs	15	24	Data Collector II

          Comment

          • DiDoria
            New Member
            • Mar 2007
            • 21

            #6
            Removed some error's

            Code:
            Warning	1	'log4net.Appender.FileAppender.FileAppender(log4net.Layout.ILayout, string)' is obsolete: 'Instead use the default constructor and set the Layout & File properties'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\ApplicationDirector.cs	35	41	Data Collector II
            Warning	2	'System.Threading.Thread.Suspend()' is obsolete: 'Thread.Suspend has been deprecated.  Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources.  http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\gui\ProgressPanel.cs	134	25	Data Collector II
            Warning	3	'System.Threading.Thread.Resume()' is obsolete: 'Thread.Resume has been deprecated.  Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources.  http://go.microsoft.com/fwlink/?linkid=14202'	C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\gui\ProgressPanel.cs	137	25	Data Collector II

            Comment

            • DiDoria
              New Member
              • Mar 2007
              • 21

              #7
              Oké, I have just One warning on that log4net but that oké... but he keeps craching on the same problem!!

              This is the exception in my logFile

              Code:
              &<?xml version="1.0" ?><log4net:events version="1.2" xmlns:log4net="http://logging.apache.org/log4net/schemas/log4net-events-1.2"><event logger="InterfaceCollector.domain.DAL.ApplicationDirector" timestamp="2007-03-19T21:02:14.2479872+01:00" level="INFO" thread="4" domain="Data Collector II.exe" username="AGVMSTUDENT\Administrator"><message>Collecting interfaces...</message><properties><data name="log4net:HostName" value="agvmstudent" /></properties></event>
              <event logger="InterfaceCollector.domain.DAL.WindowsSystemScan" timestamp="2007-03-19T21:02:15.8603056+01:00" level="ERROR" thread="4" domain="Data Collector II.exe" username="AGVMSTUDENT\Administrator"><message>Error processing .Ink File.</message><properties><data name="log4net:HostName" value="agvmstudent" /></properties><exception>System.Runtime.InteropServices.COMException (0x80010105): The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
                 at InterfaceCollector.domain.DAL.IShellLinkA.GetPath(StringBuilder pszFile, Int32 cchMaxPath, WIN32_FIND_DATAA&amp; pfd, SLGP_FLAGS fFlags)
                 at InterfaceCollector.domain.DAL.ShellShortcut.get_Path() in C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\ShellShortcut.cs:line 80
                 at InterfaceCollector.domain.DAL.WindowsSystemScan.processInk(FileInfo nextFile) in C:\Documents and Settings\AWYDZ\My Documents\Visual Studio 2005\Projects\Data Collector II\Data Collector II\domain\DAL\MainFlow\WindowsSystemScan.cs:line 172</exception></event>
              &<?xml version="1.0" ?><log4net:events version="1.2" xmlns:log4net="http://logging.apache.org/log4net/schemas/log4net-events-1.2"><event logger="InterfaceCollector.domain.DAL.ApplicationDirector" timestamp="2007-03-19T21:03:58.7482512+01:00" level="INFO" thread="8" domain="Data Collector II.vshost.exe" username="AGVMSTUDENT\Administrator"><message>Collecting interfaces...</message><properties><data name="log4net:HostName" value="agvmstudent" /></properties></event>
              <event logger="InterfaceCollector.domain.DAL.WindowsSystemScan" timestamp="2007-03-19T21:03:59.5994752+01:00" level="ERROR" thread="8" domain="Data Collector II.vshost.exe" username="AGVMSTUDENT\Administrator"><message>Error processing .Ink File.</message><properties><data name="log4net:HostName" value="agvmstudent" /></properties><exception>System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
                 at InterfaceCollector.domain.DAL.IShellLinkA.GetPath(StringBuilder pszFile, Int32 cchMaxPath, WIN32_FIND_DATAA&amp; pfd, SLGP_FLAGS fFlags)
                 at InterfaceCollector.domain.DAL.ShellShortcut.get_Path() in C:\Documents and Settings\Administrator\Desktop\Data Collector II\Data Collector II\domain\DAL\MainFlow\ShellShortcut.cs:line 80
                 at InterfaceCollector.domain.DAL.WindowsSystemScan.processInk(FileInfo nextFile) in C:\Documents and Settings\Administrator\Desktop\Data Collector II\Data Collector II\domain\DAL\MainFlow\WindowsSystemScan.cs:line 163</exception></event>
              &

              My Panel changed to

              Code:
                      public void processAll()
                      {
                          try
                          {
                              ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback([B]ApplicationDirector.Instance.processAll[/B]));
                              rootWindow.btnNext.Enabled = false;
                          }
                          catch (Exception ex)
                          {
                              MessageBox.Show("An Error Occured the file processing.", "Error during processing!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                              log.Error("An Error Occured the file processing.", ex);
                          }
                      }
                      public void blockNextButton()
                      {
                          try
                          {
                              rootWindow.btnNext.Enabled = true;
                              rootWindow.btnNext.Focus();
                              pgbGatheringData.Visible = false;
              
                              ApplicationDirector.Instance.writeToGui("Removing temporary files... ");
                              log4net.LogManager.Shutdown();
                              //Directory.Delete(Constants.rootDir,true);
                              ApplicationDirector.Instance.writeToGui("Temporary files deleted... ");
                 
                              reportPanel.fillReportScreen();
                              ApplicationDirector.Instance.writeToGui("Data collection finished! ");
                          }
                          catch (Exception ex)
                          {
                              MessageBox.Show("An error occured during removing files : \r\n\r\nSome files could not have been deleted in " + Constants.rootDir + "\r\n\r\nException : "+ ex, "Error Removing Files!",MessageBoxButtons.OK,MessageBoxIcon.Error);
                          }
                      }
              And at the lastline in my ApplicationMana ger I have added
              rootGUI.blockNe xtButton(); //UNBLOCKs it (name has to be changed)

              Comment

              • kenobewan
                Recognized Expert Specialist
                • Dec 2006
                • 4871

                #8
                I was hoping that there was a small amount of relevant code. I'm not going to be able to go through that much code for you, you will need to make do with debugging techniques (unless someone else can quickly spot the problem). Have you migrated this application to asp.net 2.0?

                Comment

                • DiDoria
                  New Member
                  • Mar 2007
                  • 21

                  #9
                  It's 100% C# .NET 2.0

                  Comment

                  • DiDoria
                    New Member
                    • Mar 2007
                    • 21

                    #10
                    It's by the way not that mutch code. Just the treading code is relevant (that are 5 classes +- 100 lines and 50 used.

                    Comment

                    • DiDoria
                      New Member
                      • Mar 2007
                      • 21

                      #11
                      Problem solved.

                      Always see if the code of you shellInterface is thread save :) (the solution can be found in my other topic)

                      Comment

                      Working...