Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

DataSocket depends on application message loop

I'm trying to use DataSocket from console application and later from Windows Service.
I've got connection problem.
I'm using VB.NET.
Here how i call:
_____________________________________________________
Imports System.Windows.Forms

Module Module1

Public WithEvents ds As New CWDSLib.CWDataSocketClass

Sub Main()
ds.ConnectTo("dstp://localhost/test", CWDSLib.CWDSAccessModes.cwdsWriteAutoUpdate)

For i As Integer = 0 To 20
ds.Data.Value = i
Console.WriteLine("{0}", i)
Next

End Sub

Private Sub ds_OnStatusUpdated(ByVal Status As Integer, ByVal [Error] As Integer, ByVal Message As String) Handles ds.OnStatusUpdated
Console.WriteLine(
"{0}", Message)
End Sub

End Module
___________________________________________________
So, i see only:

Connecting: Parsing URL.
Connecting: Parsing URL.

but, not Active:Connected.
If i add form and show it, i see:
Active:Connected
And reader got data, that i pass.
Please tell me is it behavior by design or what?
0 Kudos
Message 1 of 7
(6,691 Views)
Yes, DataSocket uses a single-threaded apartment COM object. This means that it relies on a Windows message loop running on the thread that creates the CWDataSocketClass object in order for DataSocket to function.

To do this without displaying a window, you can call DoEvents in a loop until the DataSocket is connected.

Tony H.
Measurement Studio
0 Kudos
Message 2 of 7
(6,691 Views)
Thanks a lot for the answer.
I think this is done because of the nature of VB 6.0?
Now, when VB.NET support free threading,
does NI plan to improve DataSocket?
We are looking at Measurment Studio 7.0.
So, the question in that, what's new in this vesrion. particularly DataSocket is still ActiveX or native component?
0 Kudos
Message 3 of 7
(6,691 Views)
Hi again!
I've tryed to use DoEvents().
Now i see the following status messages:

16:53:40 DEBUG - 5 0 Connecting: Parsing URL.
16:53:40 DEBUG - 5 0 Connecting: Parsing URL.
16:53:40 DEBUG - 5 0 Connecting: Parsing URL.
16:53:40 DEBUG - 5 0 Connecting: Parsing URL.
16:53:40 DEBUG - 2 0 Active: Connected
16:53:40 DEBUG - 2 0 Active: Connected

But, when I try to send something, again nothing happening, my app just hang.
No exceptions, just hanging...

After i add to my Console app: Application.Run(), everything start working. I've understood, that DataSocket class is STA.
Just tell me why it shows messages, that it is Connected, while it is not!?
0 Kudos
Message 4 of 7
(6,691 Views)
DataSocket is still implemented as an ActiveX control. Measurement Studio 7.0 does provide a native interface to DataSocket that is much nicer to use than the tlbimp-generated interop interface, but the core functionality still lives in the ActiveX implementation.

It works when you call Application.Run because Application.Run starts a message loop on the current thread. To get DataSocket to work in a console application or a Windows service, you will have to create another thread, set the thread's ApartmentState to ApartmentState.STA, start the thread, then call Application.Run on the thread.

- Elton
0 Kudos
Message 5 of 7
(6,691 Views)
Thanks for response.
I've done as you said.
Datasocket now connects to the server.
But, data is not sending.
Again just hung.
Any ideas?

P.S. If I does not set STA model, I also see messages
that DataSocket connected, but data...

I suppose, the problem in that Application.Run should be the same thread with DataSocket?
0 Kudos
Message 6 of 7
(6,690 Views)
Could you post sample, that show that this method is working.
Thanks.
0 Kudos
Message 7 of 7
(6,690 Views)