Connecting Tech Pros Worldwide Forums | Help | Site Map

C#, Thread Problem

Newbie
 
Join Date: Oct 2009
Posts: 4
#1: 4 Weeks Ago
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.

Expand|Select|Wrap|Line Numbers
  1. Program.cs
  2.   public delegate void ThreadStart();
  3.         [STAThread]
  4.         static void Main()
  5.         {
  6.             Application.EnableVisualStyles();
  7.             Application.SetCompatibleTextRenderingDefault(false);
  8.             Application.Run(new frmCOMNameRIC());
  9.  
  10.         }
  11.  
  12. Form1.cs
  13.  
  14.        public partial class frmCOMNameRIC : Form
  15.         {
  16.         public double tm=0.0; 
  17.  
  18.  
  19.         public frmCOMNameRIC()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void cmdStart_Click(object sender, EventArgs e)
  25.         {
  26.             Thread t = new Thread(new ThreadStart(UseCOMNameThread));
  27.             t.SetApartmentState(ApartmentState.MTA);
  28.             t.Start(); //Start thread t
  29.             t.Join();  //Wait until thread t finishes
  30.             lblTime.Text=tm.ToString();
  31.         }
  32.  
  33.  
  34.         public void UseCOMNameThread(){
  35.  
  36.             DateTime current;
  37.             MyCOMObject lst = new MyCOMObject();
  38.  
  39.             try
  40.             {
  41.                 //do the job
  42.                 tm=Convert.ToDouble(result);
  43.             }
  44.             catch(Exception ex)
  45.             {
  46.                 //log error message
  47.             }
  48.           System.Runtime.InteropServices.Marshal.ReleaseComObject(lst)
  49. }
  50.  
  51. private void cmdExit_Click(object sender, EventArgs e){
  52.          this.Close();
  53.         }    
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's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,791
#2: 4 Weeks Ago

re: C#, Thread Problem


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.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,791
#3: 4 Weeks Ago

re: C#, Thread Problem


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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.             Thread t); // Now has a scope of the entire program
  3.  
  4.         private void cmdStart_Click(object sender, EventArgs e)
  5.         {
  6.             t = new Thread(new ThreadStart(UseCOMNameThread)
  7.             t.SetApartmentState(ApartmentState.MTA);
  8.             t.Start(); //Start thread t
  9.             t.Join();  //Wait until thread t finishes
  10.             lblTime.Text=tm.ToString();
  11.         }
  12.  
  13.         private void Form1_Closing(object sender, EventArgs e)
  14.         {
  15.              t.abort();  // Abort the thread before the application closes.
  16.         }
  17.  
  18.  
Newbie
 
Join Date: Oct 2009
Posts: 4
#4: 4 Weeks Ago

re: C#, Thread Problem


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
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,791
#5: 4 Weeks Ago

re: C#, Thread Problem


Quote:
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.

Quote:
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.
Newbie
 
Join Date: Oct 2009
Posts: 4
#6: 4 Weeks Ago

re: C#, Thread Problem


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

Ilya
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,791
#7: 4 Weeks Ago

re: C#, Thread Problem


Did you try the thread.abort() as suggested?
Newbie
 
Join Date: Oct 2009
Posts: 4
#8: 4 Weeks Ago

re: C#, Thread Problem


Yes tlhintoq I did, same problem as before.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,791
#9: 3 Weeks Ago

re: C#, Thread Problem


I'm out of ideas. Sorry
Reply