473,788 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Com Interop Event blocked by WaitOne

I've got a (VC++ 6.0) com object which does something asynchronously (writes
a DVD image, actually).

It rasies COM events to indicate progress, including completion.

I've got this running in a c# application, with the events coming up and
being caught (The idea is to use this to Set a ManualResetEven t which will
alow the rest of the processign to happen).

However, when I put an event after the call to start the async process off,
the events seem never to be caught.

When the WaitOne times out, I get a whole swathe of debug prints from the
COM dll, but the Debug.WriteLine from the (COM) event handler are never
called.

Clearly it's some threading / synchronisation issue, but I've not found
anything obviously relevant in the docs or through google.

Can any one help, please?

Iain
Nov 15 '05 #1
6 3088
Hi,

As your application is most likely marked with [STAThread], the events
raised by the COM object are handled on the single UI thread your
application has. Once you block the UI thread with WaitOne(), it will never
be able to process further events from the COM object.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Iain" <id********@dir con.co.uk> wrote in message
news:Oe******** ******@tk2msftn gp13.phx.gbl...
I've got a (VC++ 6.0) com object which does something asynchronously (writes a DVD image, actually).

It rasies COM events to indicate progress, including completion.

I've got this running in a c# application, with the events coming up and
being caught (The idea is to use this to Set a ManualResetEven t which will
alow the rest of the processign to happen).

However, when I put an event after the call to start the async process off, the events seem never to be caught.

When the WaitOne times out, I get a whole swathe of debug prints from the
COM dll, but the Debug.WriteLine from the (COM) event handler are never
called.

Clearly it's some threading / synchronisation issue, but I've not found
anything obviously relevant in the docs or through google.

Can any one help, please?

Iain


Nov 15 '05 #2
Thanks Dmitriy.

But what do I DO?

Obviously the com dll is mutlithreaded ('both'). I've been trying to find
something on this attribute in the docs but haven't hit anything I find
particularly useful. Nor with MTAThread which is what I presume I should
use.

The test program is a windows forms program and I've changed the attribute
on Main() to [MTAThread] which has made no difference. I've also changed
the attributes of the methods that seem relevant (and the event handler) in
my class that does all the work - also to no avail.

Lastly, I've replaced the wait with a sleep loop. which I would have
thought would stand some chance of working. but no.

Sorry. still stuck.

Iain

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:OQ******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

As your application is most likely marked with [STAThread], the events
raised by the COM object are handled on the single UI thread your
application has. Once you block the UI thread with WaitOne(), it will never be able to process further events from the COM object.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Nov 15 '05 #3
I've put a bit of debug code just before the creation of the Com object (and
after the invocation of thee method in the Com object) and the Thread at
those points is MTA.

So it looks like the problem is elsewhere.

The underlying COM object has been coded to work in VB 6 which means that
the events are raised from the main thread (basically the worker thread
sends the event information into a Windows Message loop owned by COM Object.
when this is received it raises the event). So the events are effectively
marshalled on to the initiating thread.

The thread which is doing the writing does not seem visible in the .NET IDE,
however it does continue to operate even after I break the c# stuff.

Iain
"Iain" <id********@dir con.co.uk> wrote in message
news:uG******** ******@tk2msftn gp13.phx.gbl...
Thanks Dmitriy.

But what do I DO?

Obviously the com dll is mutlithreaded ('both'). I've been trying to find
something on this attribute in the docs but haven't hit anything I find
particularly useful. Nor with MTAThread which is what I presume I should
use.

The test program is a windows forms program and I've changed the attribute
on Main() to [MTAThread] which has made no difference. I've also changed
the attributes of the methods that seem relevant (and the event handler) in my class that does all the work - also to no avail.

Lastly, I've replaced the wait with a sleep loop. which I would have
thought would stand some chance of working. but no.

Sorry. still stuck.

Iain

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:OQ******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

As your application is most likely marked with [STAThread], the events
raised by the COM object are handled on the single UI thread your
application has. Once you block the UI thread with WaitOne(), it will

never
be able to process further events from the COM object.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE


Nov 15 '05 #4
You can try the following. In the .NET application, launch a separate thread
that would only wait for an event. When the event is set, this thread would
notify the main thread by invoking some method through Form.Invoke() and
immediately exit.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Iain" <id********@dir con.co.uk> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I've put a bit of debug code just before the creation of the Com object (and after the invocation of thee method in the Com object) and the Thread at
those points is MTA.

So it looks like the problem is elsewhere.

The underlying COM object has been coded to work in VB 6 which means that
the events are raised from the main thread (basically the worker thread
sends the event information into a Windows Message loop owned by COM Object. when this is received it raises the event). So the events are effectively
marshalled on to the initiating thread.

The thread which is doing the writing does not seem visible in the .NET IDE, however it does continue to operate even after I break the c# stuff.

Iain
"Iain" <id********@dir con.co.uk> wrote in message
news:uG******** ******@tk2msftn gp13.phx.gbl...
Thanks Dmitriy.

But what do I DO?

Obviously the com dll is mutlithreaded ('both'). I've been trying to find something on this attribute in the docs but haven't hit anything I find
particularly useful. Nor with MTAThread which is what I presume I should use.

The test program is a windows forms program and I've changed the attribute on Main() to [MTAThread] which has made no difference. I've also changed the attributes of the methods that seem relevant (and the event handler)

in
my class that does all the work - also to no avail.

Lastly, I've replaced the wait with a sleep loop. which I would have
thought would stand some chance of working. but no.

Sorry. still stuck.

Iain

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote in message news:OQ******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

As your application is most likely marked with [STAThread], the events
raised by the COM object are handled on the single UI thread your
application has. Once you block the UI thread with WaitOne(), it will

never
be able to process further events from the COM object.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE




Nov 15 '05 #5
Hmm. I'm trying to get my head round this (sorry, I've taken some time out
to do some higher level stuff, now back to the detail).

Basically, this will end up as a server process - possibly a service, so I
can't rely on UI level stuff like timers of Form.Invoke.

I've taken the waitforone out as an experiment and also the stuff that's
meant to follow afterwards (leaving the code to return to the test UI) and
the events come through fine.

I've tried setting the process of on another thread, but that failed.
Reasonably enough as I guess the thread that could receive the messages is
still locked and therefore will not peek the equivalent of a message loop
(which seems to be the essential problem) - I am by the way, reasonbly
convinced that I could have done this in unmanaged C++ 6 without this class
of problem, though in truth I haven't tried it.

I can't actually see any way of setting of a thread which just waits for the
events. If I block it, it wont get the events, if I don't block it it will
just exit. As mentioned I've tried putting a sleep loop in an it STILL
doesn't get teh events.

What I seem to need is some kind of 'yeild' or message loop functionality.
Is there such - not obvious to a quick look.

This seems an unlikley deficiency in dot net. What am I missing?

Iain
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:Op******** ******@TK2MSFTN GP11.phx.gbl...
You can try the following. In the .NET application, launch a separate thread that would only wait for an event. When the event is set, this thread would notify the main thread by invoking some method through Form.Invoke() and
immediately exit.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Nov 15 '05 #6
Iain,

I'd suggest receiving *COM* events on the main thread that should never be
blocked. To make the worker thread wait, introduce an AutoResetEvent that
the worker thread would wait for. Thus, the worker thread will be sleeping
until the AutoResetEvent is signaled. And after the event is signaled, the
worker thread will do some useful work and go to sleep until the
AutoResetEvent is signaled for the next time.

The main thread, in turn, will be signaling the AutoResetEvent every time it
receives a COM event. If you need to pass some params to the worker thread
before it starts its work, introduce a thread-safe parameter object
accessible to both threads.

Or, and it might be even better! - you can employ the ThreadPool. Every time
the main thread receives a COM event, it queues a work item on the thread
pool, thus avoiding any "home-made" thread care.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Iain" <id********@dir con.co.uk> wrote in message
news:eG******** ******@TK2MSFTN GP09.phx.gbl...
Hmm. I'm trying to get my head round this (sorry, I've taken some time out to do some higher level stuff, now back to the detail).

Basically, this will end up as a server process - possibly a service, so I
can't rely on UI level stuff like timers of Form.Invoke.

I've taken the waitforone out as an experiment and also the stuff that's
meant to follow afterwards (leaving the code to return to the test UI) and
the events come through fine.

I've tried setting the process of on another thread, but that failed.
Reasonably enough as I guess the thread that could receive the messages is
still locked and therefore will not peek the equivalent of a message loop
(which seems to be the essential problem) - I am by the way, reasonbly
convinced that I could have done this in unmanaged C++ 6 without this class of problem, though in truth I haven't tried it.

I can't actually see any way of setting of a thread which just waits for the events. If I block it, it wont get the events, if I don't block it it will just exit. As mentioned I've tried putting a sleep loop in an it STILL
doesn't get teh events.

What I seem to need is some kind of 'yeild' or message loop functionality.
Is there such - not obvious to a quick look.

This seems an unlikley deficiency in dot net. What am I missing?

Iain
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:Op******** ******@TK2MSFTN GP11.phx.gbl...
You can try the following. In the .NET application, launch a separate

thread
that would only wait for an event. When the event is set, this thread

would
notify the main thread by invoking some method through Form.Invoke() and
immediately exit.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE



Nov 15 '05 #7

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

Similar topics

1
1888
by: Iain | last post by:
I raised this a few days ago and Dmitriy helped, but there's been a lot of posts under the bridge since then so I thought I'd start afresh. Basically, I have a COM Dll written in C++ (6). It's job is to create DVD Images which it does nicely. It runs Asynchronously. That is (once all the information is provided) it starts its own thread and raises events as things develop. The events are marshalled through a hidden window to so that...
1
4894
by: Chris B. | last post by:
If anyone can figure out how to implement the following C++ function in C# using interop, I'd be very appreciative. I have not been successful in getting it to work correctly using interop (without using unsafe code) because of the pointer issues of the structure. Thanks, Chris //------------------------------------------------------------
3
5951
by: bb | last post by:
I have a windows network device driver written in c++ and a user interface im porting to c#, my problem is i dont seem to be getting notified of the event calls from the driver to the c# app im using the following code in c# in the UI to create an event public static IntPtr OpenGrantedPacketEvent() { IntPtr objDriver = Driver.OpenDriver(); IntPtr objEvent = Win32.CreateEvent(IntPtr.Zero, false, false,
1
3044
by: Charles Law | last post by:
I have a frustrating problem where WaitOne does not return when I expect. I am using an AutoResetEvent with an overlapped structure in a call to WaitCommEvent. The data I am expecting on the serial port is 23 bytes long, and is sent in response to a command. I begin timing before sending the command, and display split times when received data is signalled. The entire exchange of command and response is over within 6 ms (I have checked...
2
12562
by: juli jul | last post by:
Hello, How can I know that the Manual Reset Event object done WaitOne(). I have a condition and I want to do Set() to Manual Reset Event in it but I have to know that there WaitOne() was done on it before. How can I know that? Thank you! *** Sent via Developersdex http://www.developersdex.com ***
0
1662
by: qiangdi_he | last post by:
Hi, Pals, I am using WaitAny( ) to wait for the events triggered by a timer. But it seems like that when the timer's interval elapsed, and the event was set, but WaitAny is still waiting there. Basically, I want the statusBar's text change every 2 seconds. But since the WaitAny( ) doesn't stop, the statusBar's text changes every 10 seconds which is not what I want.
3
10411
by: Ed Sutton | last post by:
Can any one please offer any theories on how ManualResetEvent.WaitOne can hang when used with a timeout? When I notice my USB/Serial devices are no longer receiving communications, I hit break all, and one of the threads is hanging at: bool replyReceived = readerCommand.SignalReply.WaitOne(readerCmd.rxTimeOutMs, false); Apparently the 1000ms rxTimeOutMs is not working. I have two threads
2
3508
by: Christopher Carnahan | last post by:
I need to figure out how to terminate a thread while it is blocked trying to create a COM object via interop. In a worker thread, I do something like this: Type t = null; Object activatedObject = null; Legacy.IScheduled comObject = null; t = Type.GetTypeFromProgID(ProgID);
5
1959
by: Kürþat | last post by:
Hi all, I have a named autoreset event (EventWaitHandle) and two applications one waits on the event and one sets the evet. Application A calls Set method and free a thread in application B. What I want to know is when Set method returns can I assume event is certainly reset? Is this ordering always true : 1- Application A calls Set method (No doubt)
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.