473,397 Members | 1,974 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,397 software developers and data experts.

question life time of an asynchronous thread...


Hi, I have a C# app that creates an AppDomain, enters it, and spawns an
asyn thread to do some work and then block itself. Upon the completion
of the work, the async thread supposedly terminates, then the original
thread unblocks, unloads the AppDomain, and starts the whole process all
over again. I get the System.AppDomainUnloadedException saying that the
AppDomain from which the async thread resides has been unloaded. Now,
if I were to add a Thread.Sleep() command before the main thread returns
and unloads the AppDomain, then everything ran fine. I include the
program below hoping someone would shed some lights on this.

Thanks!
// program begins....

using System;
using System.Reflection;
using System.Threading;

namespace adSample
{
delegate void appDomainDelegate();

class asyncAD: MarshalByRefObject
{
private static AutoResetEvent evtWorkDone = new
AutoResetEvent(false);

[STAThread]
static void Main(string[] args)
{
try
{
for (int ix=0; ix<5; ++ix)
{
AppDomain AD =
AppDomain.CreateDomain("monitorAppDomain", null, null);
asyncAD ad = (asyncAD)AD.CreateInstanceAndUnwrap
(

Assembly.GetCallingAssembly().FullName,

"adSample.asyncAD"
);

ad.wrkStart();

Console.WriteLine("unloading AD\r\n");
AppDomain.Unload(AD);
} // for()
}
catch (AppDomainUnloadedException ex)
{
Console.WriteLine( "caught: {0}", ex.Message );
}

Console.WriteLine("Press Enter to quit the sample\r\n");
Console.ReadLine();
} // Main

//
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++
public void wrkStart()
{
appDomainDelegate dlgtAD = new
appDomainDelegate(wrkSpinoff);

/*
// case 1 ==================> OK
IAsyncResult iAR = dlgtAD.BeginInvoke(null,
null);
while (!iAR.IsCompleted)
{
Thread.Sleep(100);
} // while()

// Call EndInvoke to retrieve the results.
dlgtAD.EndInvoke(iAR);
*/

/*
// case 2 ==================> OK
IAsyncResult iAR = dlgtAD.BeginInvoke(null,
null);

// Call EndInvoke to retrieve the results.
dlgtAD.EndInvoke(iAR);
Thread.Sleep(1000);
*/

/*
// case 3 ==================>
System.AppDomainUnloadedException
IAsyncResult iAR = dlgtAD.BeginInvoke(null,
null);

// Call EndInvoke to retrieve the results.
dlgtAD.EndInvoke(iAR);
*/

/*
// case 4 ==================> OK
IAsyncResult iAR = dlgtAD.BeginInvoke(new
AsyncCallback(wrkCallback),

dlgtAD);

evtWorkDone.WaitOne();
Thread.Sleep(1000);
*/

/*
*/
// case 5 ==================>
System.AppDomainUnloadedException
IAsyncResult iAR = dlgtAD.BeginInvoke(new
AsyncCallback(wrkCallback),

dlgtAD);

evtWorkDone.WaitOne();

Console.WriteLine("exiting from: {0}",
Thread.GetDomain().FriendlyName);
} // wrkStart()

//
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++
public void wrkSpinoff()
{
Console.WriteLine("in wrkSpinoff()");

for (int ix=0; ix<5; ++ix)
{
Console.WriteLine(ix);
Thread.Sleep(500);
} // for(ix)

Console.WriteLine("exiting wrkSpinoff()");
} // wrkSpinof()

//
-------------------------------------------------------------------
private void wrkCallback(IAsyncResult ar)
{
Console.WriteLine("in wrkCallback()");
appDomainDelegate dlgt = (appDomainDelegate)ar.AsyncState;

dlgt.EndInvoke(ar);

Console.WriteLine("exiting wrkCallback()");
evtWorkDone.Set();
} // wrkCallback()

} //asyncAD
} // adSample
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
3 2868
I was able to run through 20 plus iterations without a crash by modifieing
your wrkCallBack method:

private void wrkCallback(IAsyncResult ar)
{
Console.WriteLine("in wrkCallback()");
appDomainDelegate dlgt = (appDomainDelegate)ar.AsyncState;

evtWorkDone.Set();
dlgt.EndInvoke(ar);

Console.WriteLine("exiting wrkCallback()");

} // wrkCallback()

Before your were calling EndInvoke then Set, what I believe was happening
(I'm not up to par yet on some aspects of threading) is that you were
calling EndEnvoke which released your WaitOne in wrkStart, then calling Set
trying to signal the tread (at which point, wrkStart had exited, and maybe
even tried to unload the AppDomain). So random mayem resulted.

Let me know if that fixes it,

Tim

"Keyee Hsu" <ke*******@pacificlife.com> wrote in message
news:Oe**************@TK2MSFTNGP09.phx.gbl...

Hi, I have a C# app that creates an AppDomain, enters it, and spawns an
asyn thread to do some work and then block itself. Upon the completion
of the work, the async thread supposedly terminates, then the original
thread unblocks, unloads the AppDomain, and starts the whole process all
over again. I get the System.AppDomainUnloadedException saying that the
AppDomain from which the async thread resides has been unloaded. Now,
if I were to add a Thread.Sleep() command before the main thread returns
and unloads the AppDomain, then everything ran fine. I include the
program below hoping someone would shed some lights on this.

Thanks!
// program begins....

using System;
using System.Reflection;
using System.Threading;

namespace adSample
{
delegate void appDomainDelegate();

class asyncAD: MarshalByRefObject
{
private static AutoResetEvent evtWorkDone = new
AutoResetEvent(false);

[STAThread]
static void Main(string[] args)
{
try
{
for (int ix=0; ix<5; ++ix)
{
AppDomain AD =
AppDomain.CreateDomain("monitorAppDomain", null, null);
asyncAD ad = (asyncAD)AD.CreateInstanceAndUnwrap
(

Assembly.GetCallingAssembly().FullName,

"adSample.asyncAD"
);

ad.wrkStart();

Console.WriteLine("unloading AD\r\n");
AppDomain.Unload(AD);
} // for()
}
catch (AppDomainUnloadedException ex)
{
Console.WriteLine( "caught: {0}", ex.Message );
}

Console.WriteLine("Press Enter to quit the sample\r\n");
Console.ReadLine();
} // Main

//
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++
public void wrkStart()
{
appDomainDelegate dlgtAD = new
appDomainDelegate(wrkSpinoff);

/*
// case 1 ==================> OK
IAsyncResult iAR = dlgtAD.BeginInvoke(null,
null);
while (!iAR.IsCompleted)
{
Thread.Sleep(100);
} // while()

// Call EndInvoke to retrieve the results.
dlgtAD.EndInvoke(iAR);
*/

/*
// case 2 ==================> OK
IAsyncResult iAR = dlgtAD.BeginInvoke(null,
null);

// Call EndInvoke to retrieve the results.
dlgtAD.EndInvoke(iAR);
Thread.Sleep(1000);
*/

/*
// case 3 ==================>
System.AppDomainUnloadedException
IAsyncResult iAR = dlgtAD.BeginInvoke(null,
null);

// Call EndInvoke to retrieve the results.
dlgtAD.EndInvoke(iAR);
*/

/*
// case 4 ==================> OK
IAsyncResult iAR = dlgtAD.BeginInvoke(new
AsyncCallback(wrkCallback),

dlgtAD);

evtWorkDone.WaitOne();
Thread.Sleep(1000);
*/

/*
*/
// case 5 ==================>
System.AppDomainUnloadedException
IAsyncResult iAR = dlgtAD.BeginInvoke(new
AsyncCallback(wrkCallback),

dlgtAD);

evtWorkDone.WaitOne();

Console.WriteLine("exiting from: {0}",
Thread.GetDomain().FriendlyName);
} // wrkStart()

//
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++
public void wrkSpinoff()
{
Console.WriteLine("in wrkSpinoff()");

for (int ix=0; ix<5; ++ix)
{
Console.WriteLine(ix);
Thread.Sleep(500);
} // for(ix)

Console.WriteLine("exiting wrkSpinoff()");
} // wrkSpinof()

//
-------------------------------------------------------------------
private void wrkCallback(IAsyncResult ar)
{
Console.WriteLine("in wrkCallback()");
appDomainDelegate dlgt = (appDomainDelegate)ar.AsyncState;

dlgt.EndInvoke(ar);

Console.WriteLine("exiting wrkCallback()");
evtWorkDone.Set();
} // wrkCallback()

} //asyncAD
} // adSample
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
Thanks, Tim, yes that seems to fix the problem for case #5. A couple of
things with this scenario, though. One, case #3 still fails without the
callback being used. Second, for case #5, the main thread in wrkStart()
does not continue until the event is set (EndInvoke() itself doesn't
unblock the main thread), so by setting the event before the the
EndInvoke() call in wrkCallback(), it should have caused the main thread
to unblock even sooner and thus unloads the AppDomain even earlier.
Which, I think, should've made the problem worse, not fixing it like it
did. So, I'm even more perplexed now as to what happened behind the
scenes 8-(. What do you think?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
Thanks, Tim, yes that seems to fix the problem for case #5. A couple of
things with this scenario, though. One, case #3 still fails without the
callback being used. Second, for case #5, the main thread in wrkStart()
does not continue until the event is set (EndInvoke() itself doesn't
unblock the main thread), so by setting the event before the the
EndInvoke() call in wrkCallback(), it should have caused the main thread
to unblock even sooner and thus unloads the AppDomain even earlier.
Which, I think, should've made the problem worse, not fixing it like it
did. So, I'm even more perplexed now as to what happened behind the
scenes 8-(. What do you think?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
11
by: zhong | last post by:
Error Message: Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention...
8
by: joels | last post by:
I'm in the preliminary reasearch stage of a large ASP.NET project; I'm looking for some resources (e.g., articles, books). I'm looking to determine the feasability of developing an ASP.NET...
1
by: Silent Ocean | last post by:
Hi All I have following questions regarding C# Assembly and Threading. Let me know the precise answer or lead me to the proper materials. 1. Is memory leakeage possible in .Net Manager...
3
by: RWF | last post by:
I have read that when using asynchronous IO it is best to keep all your operations asynchronous. But why if there is nothing left do in the AsyncCallback method other than to call an IO operation?...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.