Threading troubles

  • Thread starter Thread starter Shell D00d
  • Start date Start date
S

Shell D00d

Hi,

I'm building a shell replacement for Windows with .net. I'd like to be
able to start components (loaded from a class library) in a thread.
That is to say, I want the thread to start the component and wait
around till the component quits or the thread is forced to abort.

What is the best way to accomplish this? From my experience, I've
never been able to keep a thread alive and wait for a function to
return without freezing the thread except for calls to
Form.ShowDialog(). What goes on behind the ShowDialog function that
allows the component (in this case a form) to keep running AND ALSO
causes the thread from which it was called to pause at that function
call. How can I implement something like this myself?

I can't freeze the thread using synchronization techniques because
this would freeze my component as well and that is not acceptable.
 
Hi,

If I got you right, you shouldn't freeze the auxiliary thread at all, as the
launched component is running on this thread. What you need instead is a
means to determine that the component has finished its job, and shut down
the thread as soon as the component has finished. If you could provide more
info on how components are launched, I think it would be easier to give more
specific advice.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

news:[email protected]...
 
Hello,

OK, in the past couple of days I've managed to nudge somewhat closer
to my goals. This is what my thread procedure looks like:

Public Sub MyThread()
Console.WriteLine("Starting thread")
bar = remoteLoader.CreateBar()
bar.InitBar()
bar.ShowBar()
context = New ApplicationContext()
Application.Run(context)
bar.UnloadBar()
Console.WriteLine("Thread ended")
End Sub

And this is what I use to stop the thread:

Public Function Unload() As Boolean
context.ExitThread()
threadBar.Join()
AppDomain.Unload(barDomain)
End Function

As you can see, I still don't know when my component quits... but I
have managed to keep my thread alive and not frozen till I choose to
make it stop.

The 'bar' is basically a class that has a private reference to a form
object. I cannot use Application.Run(form) because the assembly is
hosted in a separate AppDomain. The form already inherits from
Windows.Forms, so I cannot make it also inherit from
MarshalByRefObject. I, therefore, cannot afford to have that form
marshalled across an AppDomain boundary.

My code causes a threading error when I unload the domain:

An unhandled exception of type 'System.AppDomainUnloadedException'
occurred in Unknown Module.

Additional information: The application domain in which the thread was
running has been unloaded.

The thread '.NET SystemEvents' (0x340) has exited with code 0 (0x0).

Unhandled Exception: System.AppDomainUnloadedException: The
application domain in which the thread was running has been unloaded.

The program '[1964] IonCore.exe: IonBars' has exited with code 0
(0x0).
The program '[1964] IonCore.exe' has exited with code 0 (0x0).

These errors occur only if I call bar.ShowBar(). This function is
implemented as follows:

Public Function ShowBar() As Boolean Implements IIonBar.ShowBar
frm.Show()
Return True
End Function

For a more thorough discussion of the problem, you might want to check
out my posts in microsoft.public.dotnet.framework.clr newsgroup. The
subject is 'Issues regarding AppDomain usage'.

Looking forward to your valuable input. Thanks in advance.
 
Back
Top