C#, Thread Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilyaw77
    New Member
    • Oct 2009
    • 8

    #1

    C#, Thread Problem

    Hi guys, need you advice here. Have to say I’m new to Threading applications and C# in general, so may be it is really simple and just I can not see it.

    I created a really simple project to show my problem (VS 2008 C# WinForm Project)
    I have a 3rd party COM object which I want to run when click on a Start button. The ApartmentState of this COM object is MTA and since WinForm Thread is STA only,
    I have to create a new Thread to be able to work with my COM, so here is the code.

    Code:
    Program.cs
      public delegate void ThreadStart();
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmCOMNameRIC());
    
            }
    
    Form1.cs
    
           public partial class frmCOMNameRIC : Form
            {
            public double tm=0.0; 
             
            
            public frmCOMNameRIC()
            {
                InitializeComponent();
            }
    
            private void cmdStart_Click(object sender, EventArgs e)
            {
                Thread t = new Thread(new ThreadStart(UseCOMNameThread));
                t.SetApartmentState(ApartmentState.MTA);
                t.Start(); //Start thread t
                t.Join();  //Wait until thread t finishes
                lblTime.Text=tm.ToString();
            }
    
    
            public void UseCOMNameThread(){
    
                DateTime current;
                MyCOMObject lst = new MyCOMObject();
    
                try
                {
                    //do the job
                    tm=Convert.ToDouble(result);
                }
                catch(Exception ex)
                {
                    //log error message
                }
              System.Runtime.InteropServices.Marshal.ReleaseComObject(lst)
    }
    
    private void cmdExit_Click(object sender, EventArgs e){
             this.Close();
            }
    Everything working fine and label showing the expected result, but clicking Exit killing the application:

    "The memory could not be read. Click OK to terminate the program.

    I think it might be a problem with thread management but I do not know exactly what is wrong.

    I’m out of ideas, really appreciate your help.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Creating the thread inside the _Click handler means you don't have a convenient reference to it in the future. You might try creating the thread as a program-wide object, but setting it on the click event. That way you can reference it to call a thread.abort against later when your application is closing.

      Code:
                  Thread t); // Now has a scope of the entire program
      
              private void cmdStart_Click(object sender, EventArgs e)
              {
                  t = new Thread(new ThreadStart(UseCOMNameThread)
                  t.SetApartmentState(ApartmentState.MTA);
                  t.Start(); //Start thread t
                  t.Join();  //Wait until thread t finishes
                  lblTime.Text=tm.ToString();
              }
      
              private void Form1_Closing(object sender, EventArgs e)
              {
                   t.abort();  // Abort the thread before the application closes.
              }

      Comment

      • ilyaw77
        New Member
        • Oct 2009
        • 8

        #4
        Hi tlhintoq, thnx for the good tips.
        I tried to make thread t "global" as per your advice but it still give me the same application error on Exit click. Also question, Join() method should exit the thread when it finish its execution, do I need the Abort() method as well in the Exit click procedure?

        Thank you for reply,
        Ilya

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          I tried to make thread t "global" as per your advice but it still give me the same application error on Exit click.
          I never expected that just changing the scope of the thread would fix the issue. The only reason to do that is to be able to reference from outside of the method that created it, so you could call the abort function.

          do I need the Abort() method as well in the Exit click procedure?
          It costs nothing to try it and see if it works. This would be the "trial" part of "trial and error", which is something you can expect to do a LOT in any type of programming.

          Comment

          • ilyaw77
            New Member
            • Oct 2009
            • 8

            #6
            Hi everybody, I still very much appreciate if someone could give me any hint on this problem.

            Ilya

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Did you try the thread.abort() as suggested?

              Comment

              • ilyaw77
                New Member
                • Oct 2009
                • 8

                #8
                Yes tlhintoq I did, same problem as before.

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  I'm out of ideas. Sorry

                  Comment

                  Working...