473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

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.
Oct 30 '09 #1
8 2153
tlhintoq
3,525 Expert 2GB
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.
Oct 30 '09 #2
tlhintoq
3,525 Expert 2GB
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.  
Oct 30 '09 #3
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
Oct 30 '09 #4
tlhintoq
3,525 Expert 2GB
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.
Oct 30 '09 #5
Hi everybody, I still very much appreciate if someone could give me any hint on this problem.

Ilya
Nov 2 '09 #6
tlhintoq
3,525 Expert 2GB
Did you try the thread.abort() as suggested?
Nov 2 '09 #7
Yes tlhintoq I did, same problem as before.
Nov 2 '09 #8
tlhintoq
3,525 Expert 2GB
I'm out of ideas. Sorry
Nov 3 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
6
by: Tomaz Koritnik | last post by:
I have a class that runs one of it's method in another thread. I use Thread object to do this and inside ThreadMethod I have an infinite loop: While (true) { // do something Thread.Sleep(100);...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
4
by: fred | last post by:
I use a Synclock in a secondary thread and also stop the thread using the abort method. If the abort occurs while the thread is in the Synclock will the SyncLock always be released before the...
51
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() ...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
7
by: Sin Jeong-hun | last post by:
Hi. I'm writing a Client/Multi-threaded Server program on Windows Vista. It worked fine on Windows Vista, but when the server ran on Windows XP, I/O operation has been aborted because of either...
34
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.