|
|
|
The General Winforms Interview Questions consists the most frequently
asked questions in Winforms. This list of 100+ questions guage your familiarity
with the Winforms platform. The q&a have been collected over a period
of time from various blogs, forums and other similar Winforms sites
|
54. Framework Tips Threading
|
| 54.1 Why do I have
to use the STAThread attribute for my main method in my app.?
|
| 54.2 When I close
my application, my secondary thread does not exit.
|
| 54.3 I call invoke
Abort on my threads, but they still do not terminate.
|
54.1 Why do I have to use the STAThread attribute for my main method in my
app.?
|
|
While the STAThread is required (as the documentation states) and pertinent
only to applications that use COM interop, 1.0 version of the .Net framework
has some bugs that makes it necessary for you to specify the STAThread
attribute:
1) Ole Drag Drop will not work without STA. You can check this by turning on
drag and drop in a form and try to run it.
2) Invoking a method in a type using Reflection will not work either.
These bugs have however been resolved in the 1.1 version of the framework
(Everett). Which means you then do not have to specify this attribute for your
main method.
|
54.2 When I close my application, my secondary thread does not exit.
|
|
In most cases, this can be resolved by making the thread a background thread
through it's IsBackground property.
By default, managed threads are created as foreground threads, while unmanaged
threads are created as background threads. When all of the foreground threads
in an application have terminated, the CLR invokes Abort on the background
threads that are still running.
|
54.3 I call invoke Abort on my threads, but they still do not terminate.
|
|
There are several possible reasons for this.
Invoking Abort on a suspended thread will have no effect until the thread is
resumed. Once it is resumed, then the Abort will be carried out.
Also, when closing your application, you can make sure that the secondary
threads are terminated by calling Join (myWorkerThread.Join) immediately after
invoking Abort on the thread. For instance, this would be necessary if the
thread is in the middle of an intensive operation, or even if it is simply
sleeping.
|
|