/    Sign up×
Community /Pin to ProfileBookmark

Problem with Async thread callback (Activex)

Hi

I am having a hardtime with thread calbbacks.

i have a HTML/Javascript file which has installed a C# Activex dll into clinets local macine on page onload by HTML Objcet Tag.

I have a form which has a apply button on it.When i click the apply button two events are fired.

1) Show a image to clinet that Please wait …. its an animted progress bar (for sample i have taken its a puma.gif)

2) Call the activex method called Open()

Now in ACtivex part when Open method is called it has created a async MethodInvoker and invoked a methodd called HangsForFewSeconds() which takes a long time for processing.

Till this child thread is processing the please wait image is shown to the user.when the child thread finishes it has called back to a method of parent thread close() which is an event which is captured by javascript.

and in javascript due to this event occurance in plugin it will change the image to action complete and close the page.

But i have the problem when i am calling the Close event after the child thread finishes.

Its giving me an exception Object does not support the object type (It seems to be the parent objcet is expired)

I have my following code

1) Html/JavasCRIPT

[code]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN” >

<html>
<head>
<title>WebForm1</title>
<meta name=”GENERATOR” Content=”Microsoft Visual Studio .NET 7.1″>
<meta name=”CODE_LANGUAGE” Content=”C#”>
<meta name=vs_defaultClientScript content=”JavaScript”>
<meta name=vs_targetSchema content=”http://schemas.microsoft.com/intellisense/ie5″>
</head>
<body>

<!– Our activeX object –>
<OBJECT id=”OurActiveX” name=”OurActiveX” classid=”clsid:121C3E0E-DC6E-45dc-952B-A6617F0FAA32″ VIEWASTEXT codebase=”OurActiveX.cab”></OBJECT>

<!– Attaching to an ActiveX event–>
<script language=”javascript”>
function OurActiveX::OnClose(redirectionUrl)
{
alert(redirectionUrl); <!– [url]http://otherwebsite.com[/url] should be returned–>
//window.location = redirectionUrl;
showComplete();

}
</script>

<script language=”javascript”>

objImage = new Image();

function displayimage(){
document.images[“im”].src = “puma.gif”;
}

function showComplete()
{
document.images[“im”].src = “dog.gif”;
}

//Passing parameters to ActiveX object and starting application
function OpenActiveX()
{
try
{

var myctrl1 = document.getElementById(‘OurActiveX’);
myctrl1.Title = ‘DasActivex’;
document.OurActiveX.MyParam = “Hi I am here.” //Passing parameter to the ActiveX
document.OurActiveX.Open(); //Running method from activeX
//showComplete();
//alert(“come”);

//showComplete();
}
catch(Err)
{
alert(Err.description);
}
}

</script>

<form name=”f1″>

<img name=”im” src=puma.gif”<br>
<input type=”button” name=”Prev” value=” Apply ” onClick=”displayimage();OpenActiveX();”>
</form>

</body>
</html>
[/code]

2) My Activex Code

[code]
using System;

using System.Reflection;

using System.Runtime.InteropServices;

using System.Text;

using System.Threading;

using System.Windows.Forms;

using Microsoft.Win32;

using System.Diagnostics;

namespace Kosmala.Michal.ActiveXTest

{

/// <summary>

/// Summary description for Class1.

/// </summary>

[ProgId(“OurActivex”)]

[ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(ControlEvents))] //Implementing interface that will be visible from JS

[Guid(“121C3E0E-DC6E-45dc-952B-A6617F0FAA32”)]

[ComVisible(true)]

public class ActiveXObject

{

private string myParam = “Empty”;

public ActiveXObject()

{

}

public event ControlEventHandler OnClose;

/// <summary>

/// Opens application. Called from JS

/// </summary>

[ComVisible(true)]

public void Open()

{

//TODO: Replace the try catch in aspx with try catch below. The problem is that js OnClose does not register.

try

{

Debug.WriteLine(“Open method called”);

// HangsForFewSeconds();

MethodInvoker SimpleDelegate = new MethodInvoker(HangsForFewSeconds);

AsyncCallback ac = new AsyncCallback(Close);

// SimpleDelegate.BeginInvoke(null,null);

SimpleDelegate.BeginInvoke(ac, null);

//SimpleDelegate.Invoke();

MessageBox.Show(myParam); //Show param that was passed from JS

}

catch (Exception e)

{

Debug.WriteLine(e.Message);

//ExceptionHandling.AppException(e);

throw e;

}

}

[ComVisible(true)]

public void Close(IAsyncResult ar)

{

try

{

{

if (OnClose != null)

{

Debug.WriteLine(OnClose.Target.ToString());

OnClose(“http://otherwebsite.com”); //Calling event that will be catched in JS

}

else

{

MessageBox.Show(“No Event Attached”); //If no events are attached send message.

}

}

}

catch (Exception Ex)

{

Debug.WriteLine(Ex.Message);

}

}

public void HangsForFewSeconds()

{

try

{

for (int i = 0; i < 20; i++)

{

Debug.WriteLine(“Thread started sleeping”);

Thread.Sleep(1000);

Debug.WriteLine(“Thread awake”);

}

}

catch

{

}

}

/// <summary>

/// Parameter visible from JS

/// </summary>

[ComVisible(true)]

public string MyParam

{

get

{

return myParam;

}

set

{

myParam = value;

}

}

/// <summary>

/// Register the class as a control and set it’s CodeBase entry

/// </summary>

/// <param name=”key”>The registry key of the control</param>

[ComRegisterFunction()]

public static void RegisterClass ( string key )

{

// Strip off HKEY_CLASSES_ROOT from the passed key as I don’t need it

StringBuilder sb = new StringBuilder ( key ) ;

sb.Replace(@”HKEY_CLASSES_ROOT”,””) ;

// Open the CLSID{guid} key for write access

RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);

// And create the ‘Control’ key – this allows it to show up in

// the ActiveX control container

RegistryKey ctrl = k.CreateSubKey ( “Control” ) ;

ctrl.Close ( ) ;

// Next create the CodeBase entry – needed if not string named and GACced.

RegistryKey inprocServer32 = k.OpenSubKey ( “InprocServer32” , true ) ;

inprocServer32.SetValue ( “CodeBase” , Assembly.GetExecutingAssembly().CodeBase ) ;

inprocServer32.Close ( ) ;

// Finally close the main key

k.Close ( ) ;

MessageBox.Show(“Registered”);

}

/// <summary>

/// Called to unregister the control

/// </summary>

/// <param name=”key”>Tke registry key</param>

[ComUnregisterFunction()]

public static void UnregisterClass ( string key )

{

StringBuilder sb = new StringBuilder ( key ) ;

sb.Replace(@”HKEY_CLASSES_ROOT”,””) ;

// Open HKCRCLSID{guid} for write access

RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(),true);

// Delete the ‘Control’ key, but don’t throw an exception if it does not exist

k.DeleteSubKey ( “Control” , false ) ;

// Next open up InprocServer32

//RegistryKey inprocServer32 =

k.OpenSubKey ( “InprocServer32” , true ) ;

// And delete the CodeBase key, again not throwing if missing

k.DeleteSubKey ( “CodeBase” , false ) ;

// Finally close the main key

k.Close ( ) ;

MessageBox.Show(“UnRegistered”);

}

}

/// <summary>

/// Event handler for events that will be visible from JavaScript

/// </summary>

public delegate void ControlEventHandler(string redirectUrl);

/// <summary>

/// This interface shows events to javascript

/// </summary>

[Guid(“68BD4E0D-D7BC-4cf6-BEB7-CAB950161E79”)]

[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]

public interface ControlEvents

{

//Add a DispIdAttribute to any members in the source interface to specify the COM DispId.

[DispId(0x60020001)]

void OnClose(string redirectUrl); //This method will be visible from JS

}

}

[/code]

Any help would be a great help to me.

Thanks

Das

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @das_dwipayan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...