Dhwanil

Hi,

Here is the scenario that I need help in implementing -

I have two objects. Their resources are as follows -

objA -
Default-Thread
WindowsForm
UnBlock-Thread-ZZZ button

Thread-ZZZ thread

objB -
DefaultThread
WindowsForm
Call-Obj-A-and-wait button

objB needs to call objA on its Thread-ZZZ thread and get blocked till objA releases Thread-ZZZ. This call should not block objA's Default-Thread i.e. user should be able to click on a button etc.

Can anyone provide sample code / project / approach which could implement the above mentioned scenario

Thanks & Regards,

Dhwanil Shah



Re: Common Language Runtime Threading - block a method call but continue execution

Vivek Ragunathan

Hi

Does this help

Code Snippet

class Test
{
private Thread t1;
private Thread t2;
private ManualResetEvent manualEvent;

private A objectA;
private B objectB;

static void Main()
{
manualEvent = new ManualResetEvent(false);
t1 = new Thread(new ThreadStart(Thread1));
t2 = new Thread(new ThreadStart(Thread2));

objectA = new A();
objectB = new B();

t1.Start(objectA);
t2.Start(objectB);
}

void Thread2(object arg)
{
B obj = arg as B;
if (obj == null)
{
return;
}

manualEvent.WaitOne();

// Event signalled, objectA free to use.

// Code that uses objectA......say B.SomeMethod(objectA);
}

void Thread1(object arg)
{
A obj = arg as A;
if (obj == null)
{
return;
}

// Code that uses objectA;

manualEvent.Set();

// Other code but that does access objectA;
}
}

Regards





Re: Common Language Runtime Threading - block a method call but continue execution

IsshouFuuraibou

What you might be looking for is a WaitHandle (AutoResetEvent, ManualResetEvent, Mutex) that you can call a WaitOne() on which will block the thread until the wait handle is signaled.

Another scenario is using Mutex/Lock to block waiting for a resource to be free





Re: Common Language Runtime Threading - block a method call but continue execution

Dhwanil

I am not really sure if this helps or not.

I want objB to call a method on objA ---- and stay there till objA allows it to go. It has to stay there without doing anything and it should still allow objA to be "interactive".

The actual scenario is objA = "a .NET windows-control-embedded-in-IE" and objB = "InternetExplorer/JS".

IE/JS makes a call to .NET-control, and I don't want that call to return till a user presses some button on the .NET-control. So I want the .NET-control to hold IE/JS's method call while still staying "free" to allow the user to interact with its UI.

Ideally, I would have used events, but the default .NET CAS security for Intranet / Internet restricts .NET-control from raising events that are trapped in a COM i.e. IE/JS. The "Allow calls to un-managed assemblies" is by default denied. My scenario's requirements are that I cannot change the default .NET CAS security.

Same security also blocks my efforts to pass a JS function pointer to .NET and .NET calling that JS function pointer when required.

So, I am trying to simulate "events" by this way! Only other way I can think of simulation of events other then this scenario is to have my JS continually poll my .NET-control and then the JS can react to that poll as required.





Re: Common Language Runtime Threading - block a method call but continue execution

Dhwanil

Sorry, I am not very familar with multi-threading ( or its concepts ) ..... this probably is my first try at doing something via multi-threading......



Re: Common Language Runtime Threading - block a method call but continue execution

nobugz

Vivek's code sample should solve your problem. Please explain why you have trouble implementing it.





Re: Common Language Runtime Threading - block a method call but continue execution

Dhwanil

Please refer to my reply to Vivek's post.

Since then, I tried out his code in the actual scenario I have.

My Thread-2 needs to be invoked from JS. After invoking this thread, the call returns to JS instead of waiting on Thread-2 to come out of "wait" state.

Here is the sample code Ive written based on Vivek's post.

UserControl code -

Code Snippet

Imports System.Threading

<SYSTEM.RUNTIME.INTEROPSERVICES.COMVISIBLE(< FONT>True)> _

Public Class UserControl3

Private manualEvent As ManualResetEvent

Private t2 As Thread

Private mVar As Integer

Public Sub New()

' This call is required by the Windows Form Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call.

manualEvent = New ManualResetEvent(False)

t2 = New Thread(AddressOf MakeJSWait)

End Sub

' called from JavaScript

Public Sub StartThread()

t2.Start()

' Thread.Sleep(0)

End Sub

Private Sub MakeJSWait()

mVar = 0

manualEvent.WaitOne()

mVar = 1

End Sub

Public Property Val() As Integer

Get

Return mVar

End Get

Set(ByVal value As Integer)

' do nothing

End Set

End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If CheckBox1.Checked = True Then

manualEvent.Set()

Else

MessageBox.Show("Hello!!")

End If

End Sub

End Class

HTML / JS -

Code Snippet

<HTML>
<script>

var mConManager;

function OnBodyLoad()
{
mConManager = document.getElementById("controlManager");
}

function CheckForCommands()
{
var cmd;

mConManager.StartThread();

cmd = mConManager.Val;

alert(cmd);

}

</script>

<BODY onload="OnBodyLoad();">

<object id="controlManager" classid="bin\Debug\WindowsControlLibrary2.dll#WindowsControlLibrary2.UserControl3"></object>

<br><br><br>

<input type="button" id="btnStart" value="Start" onclick="CheckForCommands();"/>

</BODY>
</HTML>






Re: Common Language Runtime Threading - block a method call but continue execution

CommonGenius.com

There is no need to call MakeJSWait from another thread if you want the call to block. Simply call it directly.




Re: Common Language Runtime Threading - block a method call but continue execution

Dhwanil

I still want my .NET user-control to be responsive. I want to make the JS wait ---- till ----- the user "creates an event" i.e. for example clicks on a button on my user-control.

If I call the MakeJSWait from JS, and block that call..... then I think my UI will become non-responsive.





Re: Common Language Runtime Threading - block a method call but continue execution

CommonGenius.com

I don't think you're going to be able to do that. Javascript executes in the message loop for IE. Your user control gets messages from that same loop. So if you block Javascript, you are also blocking the main message loop, and your user control will be unresponsive. You might be able to hack something up using DoEvents, but it would be ugly, and it would probably make your CPU usage spike.




Re: Common Language Runtime Threading - block a method call but continue execution

Dhwanil

Yes, from whatever I have read of "async" IE/JS on the Web, it does look that way. I was trying to see if there was any way around it.

Currently, we have implemented a periodic JS poll to the .NET user-control to see if some event has occured in the .NET user-control for which the JS needs to take action on. Once JS takes the required action, it goes and posts the results of that action back to the .NET user-control. Currently, we are only adding new .NET user-controls via JS and making the JS return the object back to .NET user-control. The .NET user-control, can then directly talk to the newly added .NET user-control.





Re: Common Language Runtime Threading - block a method call but continue execution

CommonGenius.com

What you really want to do is allow the UserControl to fire events and attach a javascript handler to them, just as you would in a managed application. See this link for a possible solution:

http://www.thescripts.com/forum/thread621020.html






Re: Common Language Runtime Threading - block a method call but continue execution

Dhwanil

My .NET user-control comes from a intranet IIS server. .NET Framework / IE give my .NET user-control the LocalIntraNetZone ( which is set to LocalIntranet permission-set ) security zone.

If I use events from my .NET user-control and attach a JS event-handler to them, then it means that somewhere internally, my event goes to JS via a delegate. This means that is a call to an un-managed application i.e. IE/JS. LocalIntranet permission-set, does not allow calls to un-managed applications.

Silly thing is that un-managed applications i.e. IE/JS can make calls freely to my .NET user-control ---- just my .NET user-control cant raise events that can be trapped by IE/JS.

We dont want to change the security settings on the clients, as we have various customers who do not provide Admin access to their users. Also, we want to be able to access our .NET user-control applications from public terminals ( like free web-surfing terminals available at Changi, Singapore airport ) over which the users may not have a lot of control. Of course the public terminals would need to have .NET Framework to make this work, but we are betting that sooner or later they would have the same.





Re: Common Language Runtime Threading - block a method call but continue execution

CommonGenius.com

In that case, you are probably going to be stuck with the polling solution you previously described.