Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the correct/best method for threading in a COM exposed assembly?
    text
    copied!<p>First let me say: I am still very inexperienced with VB.NET so there is probably something simple I am missing, or at least that's what I'm hoping. </p> <p>I've been writing a COM exposed wrapper class to provide access to a web service in a legacy VB6 application. I got everything pretty much working as I wanted – all the COM properties, methods and events are showing up in the VB6 app – but for one minor detail: when I used a method which made a call to the service, it was obviously only working asynchronously, so I couldn't update a progress bar or do anything else while waiting for a corresponding assembly event to fire. </p> <p>So I concluded that I needed to use a separate thread to access the web service. I decided the cleanest method was to do this via a separate class, which raised events in the main wrapper class, which in turn raised the public COM events. Here is how I implemented it: </p> <p>At the top of the wrapper class I put: </p> <pre><code>Private WithEvents oService As Service_Class Private ServiceThread As Thread </code></pre> <p>Then, in one of the main wrapper methods (say a simple Status Request), I put the following: </p> <pre><code>oService = New Service_Class ServiceThread = New Thread(AddressOf oService.RequestStatus) ServiceThread.Start() </code></pre> <p>Once the service call has acquired the 'Status' value it raises the StatusReport event in the main wrapper, which in turn raises the COM exposed StatusReport event, thus: </p> <pre><code>Private Sub oService_StatusReport(ByVal Status As String) Handles oService.StatusReport RaiseEvent StatusReport(Status) </code></pre> <p>This works, up to a point – it certainly doesn't tie up processing in the calling app any more – but there does still seem to be a problem: the calling app crashes if I try to access any controls within the wrapper object's events. I've tested this with a .NET form and it doesn't crash but it raises a "Cross-thread operation" error. Of course I could use a timer in the calling app, monitoring a variable or something, to get around this problem, but that seems awfully messy and made me think that I'm probably doing something fundamentally wrong in the way I'm implementing the threading. Can someone tell me what I am doing wrong and how to fix it, please?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload