3

I have a form and several external classes (serial port, file access) that are instantiated by the form.

1) What's the simplest way to run an instance of an external class in its own thread?

2) Is the instance's thread automatically terminated when the form closes?

H H
  • 263,252
  • 30
  • 330
  • 514
OIO
  • 363
  • 7
  • 16
  • 2
    Class-instances (objects) don't run on threads. Their methods do, and that could be on multiple threads (at once). – H H Aug 29 '10 at 23:27
  • Does this happen automatically, or do I have to set the methods to run on multiple threads? The form Main() has the STAThread method attribute, do all methods in a form run in a single thread? Cause using Thread.Sleep in a form will freeze it. – OIO Aug 29 '10 at 23:51
  • 1
    Regarding STAThread: "COM threading models only pertain to applications that use COM interop. Using this attribute in an application that does not use COM interop has no effect." http://msdn.microsoft.com/en-us/library/system.stathreadattribute.aspx – Andras Vass Aug 30 '10 at 00:14
  • `"do all methods in a form run in a single thread?"` Well, the default behavior is that events are handled on the dedicated UI thread (that also does windows message pumping.) If you do some long operation on this thread, message pumping stops, so the window becomes unresponsive. See also Jon Skeet: http://www.yoda.arachsys.com/csharp/threads/winforms.shtml – Andras Vass Aug 30 '10 at 00:18

3 Answers3

2

1) What's the simplest way to run an instance of an external class in its own thread?

Instances of classes do not "run". Methods do.

As such, you may want to look into the APM pattern and the BackgroundWorker class.

2) Is the instance's thread automatically terminated when the form closes?

It depends on how the threads were started. A thread can be a background thread or a foreground thread - the latter prevents the application from terminating.

Andras Vass
  • 11,478
  • 1
  • 37
  • 49
  • I'm checking the Event-based Asynchronous Pattern and the BackgroundWorker class. – OIO Aug 30 '10 at 00:06
1

If it's just a couple of lines of code you want to call asynchronously, probably the best way is ThreadPool.QueueUserWorkItem. See: What's the difference between QueueUserWorkItem() and BeginInvoke(), for performing an asynchronous activity with no return types needed

Community
  • 1
  • 1
Shlomo
  • 14,102
  • 3
  • 28
  • 43
  • I don't think that's what I want, and the classes can be as big as they need to be. I'm looking for a simple way to start asynchronous instances from a parent form, and terminate them when the form closes. – OIO Aug 29 '10 at 23:35
0

See if you are working with managed Environment, when an object is instantiated it will automatically dispose off if it is out of scope. The Disposal is actually taken care of by Garbage collection.

If you are using UnManaged objects, its your responsibility to close resources before making the object out of scope.

Garbage collection periodically turns on and start collecting all the objects that are out of scope. If you need to work on large objects, you can try using WeakReference class which will hold the object but also expose it for Garbage collection.

Read about WeakReference and garbage collection from here: http://www.abhisheksur.com/2010/07/garbage-collection-algorithm-with-use.html

I hope this would help you.

abhishek
  • 2,975
  • 23
  • 38
  • Not sure if this applies to the second question, automatically terminating threads started from a form. – OIO Aug 29 '10 at 23:41
  • No matter in which thread you are, Garbage Collection works on all threads or irrespective of Threads. When your form closes, you can explicitly set the instance variables to null to make them available to Garbage collection. – abhishek Aug 30 '10 at 08:18