Re: System.Timers.Timer() sometimes gets stuck and disappears??



Willy,

Thank you so much for your help. You were right. The activeX was blocking
the call. Although I still unsure why, I devised a solution. We called it
with a timeout function. Basically, we used this code found here:

http://weblogs.asp.net/israelio/archive/2004/06/19/159985.aspx

But modified it so that there was no classes and I could fit in inline.
Then I called my code like this:

.... in the timer event handler:
bool success = StartWithTimeout(DoHeartBeatStuff, 60);

.... here is my hb function:
private void DoHeartBeatStuff()
{
.....
// the call to the acitveX control
}


..... then here is my timeout function:
private bool StartWithTimeout(RunThisFunctionDelegateTimeout
whichFunctionToRun, int timeoutSeconds)
{
System.Threading.Thread th = new System.Threading.Thread(new
System.Threading.ParameterizedThreadStart(DoTimeoutFunctionStart));

// Sets the state of the specified event to non-signaled.
evnt.Reset();

// start thread
th.Start((object)whichFunctionToRun);

// wait with timeout on the event
if (evnt.WaitOne(TimeSpan.FromSeconds(timeoutSeconds), false))
{
// Sucess - timeout did not occurred
return true;
}
else
{
LogStuff.LogError("[256] Function Timed out! Aborting
call...");

// Failure - Timeout - do cleanup
try
{
th.Abort();
}
catch (Exception exec)
{
LogStuff.LogError("[257] Could not abort timed out
function! Details: " + exec.Message);
}

return false;
}
} // end StartWithTimeout ()

.... and later:

public void DoTimeoutFunctionStart(object whichFunctionToRunObj)
{
RunThisFunctionDelegateTimeout whichFunctionToRun =
(RunThisFunctionDelegateTimeout)whichFunctionToRunObj;

whichFunctionToRun(); // launch our function

// notify evnt.WaitOne that we are done with our operation
evnt.Set();

} // end DoTimeoutFunctionStart ()
....
Like I said, I got a lot of this material from that website.

Thank you very much Willy for pointing us in the right direction! :~}

Rob K

"Willy Denoyette [MVP]" wrote:

Rob, see inline.

Willy.

"RobKinney1" <RobKinney1@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:9F36EBE4-3D13-479D-BE58-8026ECA4D8B7@xxxxxxxxxxxxxxxx
|I agree Willy. Thank you for responding. As I was explaining to Steve
| above, I think that this is getting stuck at the activeX control.
| Unfortunately, I am unable to say which activeX control I am using because
of
| all this confidentiality red tape (*sigh).
|
| But what I can say is that this control was made by a 3rd party (that I am
| growing to really despise). It appears it never returns from its call.
At
| this point in the HB function:
|
| // query an activeX control to check statuses
| int myValue = activeXControl1.getMyValue();
|
| This is where it blocks (as you suspected). So... upon thinking about
this
| more, is there a way in .NET 2.0 C# to cause this call to timeout? If I
| could timeout the entire DoHeartBeat function or just timeout this single
| call in-line, it would be wonderful! I checked the net and cannot find
| anything so far that allows this.
|
| I was also was pondering the threading stuff you were talking about. So
I
| made sure that I called the activeX control through the main thread that
it
| was called on my changing my code from:
|
| int myValue = activeXControl1.getMyValue();
|
| to
|
| int myValue = 0;
| activeXControl1.Invoke((System.Windows.Forms.MethodInvoker)delegate
| {
| myValue = activeXControl1.getMyValue();
| });
| // now I have myValue!!! :-)
|

Ok good move, now you are effectively delegating the call,
Now you should not call Invoke on the AX instance reference, you should use
a Forms control instance (say the Form itself). Another thing you should
look at is the point of declaration/definition of activeXControl1, this
variable (guess it's a static) should be initialize/assigned on the UI (STA)
thread. If it's assigned on another thread, the variable will hold a proxy
instead of a direct reference. And to make sure we are dealing with a real
AX control (I'm confident it is), you could check the registration of the
control, watch the "ThreadingModel" in the registry (it should be Apartment)
or use OleView to check this out.



| Hopefully this means it is executing this on the main thread. I tested
and
| ran this and for a while thought this was the solution... but then it
locked
| in this thread again.
|
| Any ideas on a timeout?
|
No, if the call is a non returning COM call it's just like another blocking
function call, if the function blocks (or enters en endless loop), nothing
can stop it other than destroying the thread it runs on. Note that I'm not
sure it's the call who blocks in native code, it's also possible that the
call doesn't get dispatched to the object code (of the COM object) because
of marshaling issues, that's why it's important to understand what I said
above.
To summarize, an AX instance must live on an pumping STA thread (like the
UI), and calls should be direct calls, not marshaled using a COM proxy
(cross apartment/thread).



Willy.

.



Relevant Pages

  • ActiveX problem
    ... Everytime I start Excel it asks me if I want to run this Active X control. ... how ActiveX controls are initialized within Office applications. ... another format the link reference to the ActiveX control might not convert ...
    (microsoft.public.windowsxp.perform_maintain)
  • RE: ActiveX as a Service with events
    ... > Have an ActiveX created in Delphi 7 and need to run it in a service by ... > If I just add the reference to the control at design time using the TLBImp ... > Private WithEvents myOCX as AxmyControl.AxmyOCX ... > Private Sub OnStart() ...
    (microsoft.public.dotnet.framework.interop)
  • Re: User Controls (Active X) - Da Process
    ... ActiveX control itself. ... Interfaces), defined in other Binaries. ...
    (microsoft.public.vb.general.discussion)
  • RE: Microsoft Baseline Security Analyzer exploit (Exposed vulnerabilities list)
    ... >security setting of the browser. ... >whether to launch ActiveX that is signed by a specific signer. ... There IS a need for low security for the rouge ActiveX control to be ... Admin downloads and runs MBSA. ...
    (Vuln-Dev)
  • AHCI timeouts on S3 resume
    ... I can usually hit a recoverable timeout. ... vendor = 'Intel Corporation' ... <ACPI PCI bus> on pcib0 ... <Enhanced SpeedStep Frequency Control> on cpu0 ...
    (freebsd-stable)