all groups > dotnet interop > february 2004 >
You're in the

dotnet interop

group:

TargetException after idle time when sending sourced events via from .net assembly


TargetException after idle time when sending sourced events via from .net assembly asanford
2/26/2004 11:11:08 AM
dotnet interop:
We have a .net assembly that uses C# events for outgoing notifications. We use COM interop to create and access the .net objects via VB6, including recieving events in VB6 using the WithEvents keyword (we use the ComSourceInterfaces attribute to expose the c# outgoing events to COM clients.) The .net object only sends events during calls into the .net object. Everything works fine, unless the VB client stops calling into the .net objects for several minutes (9?). After a certain amount of idle time, if the VB client then calls into the .net object, that call seems to proceed fine until the point in that call that generates events which are supposed to be sent back to the caller (via the C# events.) At this point, we get a TargetException reported, with a callstack showing that it failed during the sending of the event

We are using the .net framework 1.0 SP2, but I think someone also tested the 1.1 framework and got the same results

We have been able to workaround this problem for now by having the VB6 client application "ping" the .net object every few minutes by calling a method on it - this "keep-alive" patch seems to work for now. So, I am convinced this is some sort of object lifetime issue, but not sure of the correct fix

I found the following post that seems related, but doesn't really provide a resolution
http://groups.google.com/groups?q=interop+framework+idle&hl=en&lr=&ie=U
F-8&oe=UTF-8&selm=c66a084b.0209091329.492909f9%40posting.google.com&rnu
=

Any help, Microsoft
thanks
RE: TargetException after idle time when sending sourced events via from .net assembly v-yiy NO[at]SPAM online.microsoft.com (
2/27/2004 8:23:28 AM
Hi,

I have tested the sample in the google reference, but I didn't get the
exception after idling for 15 mins, could you give me a simple sample to
let me reproduce your problem? Also, I pasted my test code below, you may
also test it on your system.(sorry, I'm not good at VB.NET so I translated
the sample into C#)

If you have any updates on this problem please feel free to post it in this
thread.
Thanks!

<code>
using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace Interop
{
[
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)
]
public interface ITimerEvents
{
void Expired();
}

public delegate void ExpiredHandler();

[
ComSourceInterfaces(typeof(ITimerEvents))
]
public class TimerWrapper
{
public event ExpiredHandler Expired;

private System.Threading.Timer wrappedTimer;
public void Start(int interval)
{
try {
wrappedTimer = new System.Threading.Timer(new
System.Threading.TimerCallback(ExpiredCallback),
null,interval,System.Threading.Timeout.Infinite);
}
catch(Exception ex) {
Console.WriteLine(ex.Message);
}
}
private void ExpiredCallback( object state)
{
try {
if (Expired != null)
Expired();
}
catch(Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
}
</code>
Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
RE: TargetException after idle time when sending sourced events via from .net assembly asanford
2/27/2004 9:31:07 AM
Hello
thanks for your reply. Additional info: at least in my case, it appears the problem only occurs if the events are sent back to VB6 from a separate worker thread in .net. I've found the problem happens after waiting 12 minutes, I haven't tried a smaller idle time. Here is a C# assembly that will cause the problem if accessed from VB6
(note, I've been using "regasm netevents2com.dll /tlb:netevents2com.tlb /codebase" to register this for COM)
-----------------------------------------------------------------------------------------
using System
using System.Runtime.InteropServices
using System.Threading
using System.Windows.Forms

namespace netevents2co

[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)
[ComVisibleAttribute(true)
public interface INetTes

int StartThreadedMethod1()


[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)
[ComVisibleAttribute(true)
public interface INetTestEvents

void TestEvent(string sTxt);


[ComSourceInterfaces(typeof(INetTestEvents))
[ClassInterface(ClassInterfaceType.None)
[ComVisibleAttribute(true)
public class CNetTest : INetTes

public CNetTest(



static CNetTest(

AppDomain currentDomain = AppDomain.CurrentDomain
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler)


private int Method1(

Test("Event from .net")
return(4)


public int StartThreadedMethod1(

if (!Monitor.TryEnter(this,0)
return(1)

if (m_methodThread != null

Monitor.Exit(this)
return((int)1)


ThreadStart method1ThreadDelegate = new ThreadStart(this.ThreadedMethod1)
m_methodThread = new Thread(method1ThreadDelegate)
Monitor.Exit(this)
m_methodThread.Start();
return(0)


private void ThreadedMethod1(

Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.MTA

Method1()

lock(this

m_methodThread = null



public delegate void TestEventDelegate(string sTxt)
public event TestEventDelegate TestEvent

private void Test(string sTxt

if (TestEvent != null

TestEvent(sTxt)



static private void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)

tr

Exception e = (Exception)args.ExceptionObject
string sErrorMsg = "An error occurred; please contact the adminstrator with the following information:\n\n"
sErrorMsg = sErrorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace
sErrorMsg += "\n\nThe application will terminate now"
MessageBox.Show(sErrorMsg, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)

catc

tr

MessageBox.Show("Fatal Error: the application will exit now", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)

finall

Environment.Exit(5)



Environment.Exit(4)


private Thread m_methodThread


---------------------------------------------------------------------------
here's the sample VB6 code that illustrates the problem. Try pressing the command button a few times to prove it works, then don't press anything for 12 minutes, then press the button again, and you should see the exception
---------------------------------------------------------------------------
Option Explici

Dim WithEvents netTest As netevents2com.CNetTes
Dim INetTest As netevents2com.INetTes

Private Sub Command1_Click(
Dim i As Intege
i =
i = INetTest.StartThreadedMethod
End Su

Private Sub Form_Load(
Set netTest = New netevents2com.CNetTes
Set INetTest = netTes
End Su

Private Sub netTest_TestEvent(ByVal sTxt As String
MsgBox ("received event from COM object: " + sTxt
End Su
--------------------
finally, here's a link to another report I found that sounds similar to the problem I'm seeing
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=4ca801c37654%24957ddcd0%24a501280a%40phx.gbl&rnum=2&prev=/groups%3Fq%3DTargetException%2Bafter%2Bidle%2Btime%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D4ca801c37654%2524957ddcd0%2524a501280a%2540phx.gbl%26rnum%3D

Re: TargetException after idle time when sending sourced events via from .net assembly Mattias Sjögren
2/27/2004 11:01:05 PM

Sounds like you're seeing this bug.

COM Interop Cannot Properly Deal with A Disconnected Stub
http://support.microsoft.com/?kbid=325699



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
RE: TargetException after idle time when sending sourced events via from .net assembly v-yiy NO[at]SPAM online.microsoft.com (
2/28/2004 8:39:54 AM
Hello,

Thanks for your reply!

I tried your test code on v1.1 and waited for 20 mins for 2 times, but the
program works properly.
Since our MVP had suggest a KB article similiar to this problem in .NET
v1.0 sp2,

Please read the article and decide if you want to try this fix. To get the
fix, you may follow the instructions in the article, or please send email
to (remove "online." from this no Spam email address):
mailto:dscommhf@online.microsoft.com with the following information,

* Put ?¡ãHotFix Request?¡À in the subject line

* Issue ID : 21466917

* e-mail address

*First Name, Last Name

*Phone Number

*Company Name (if any)

It will be a free service request,since you only want a hotfix for this
issue.
Thanks for your patience!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
RE: TargetException after idle time when sending sourced events via from .net assembly asanford
3/10/2004 1:06:14 PM
Hello

Yes, I had already seen this article

COM Interop Cannot Properly Deal with A Disconnected Stu
http://support.microsoft.com/?kbid=32569

from reading it, it doesn't sound like the same problem - we get different exceptions

Anyhow, as you said the repro didn't show the problem for you with 1.1, I went and tested it with 1.1 myself (I had been using 1.0 SP2), and 1.1 appears to fix the problem - I installed 1.1 and the problem went away, then uninstalled it and the problem came back.

Maybe MS will post an knowledgebase article on this issue (or if it actually is the above problem, they will update the details on which exceptions can occur, and also note that 1.1 fixes this)

AddThis Social Bookmark Button