-
ActiveX Exe
I have created an ActiveX exe that encapsulate a global object (that resides
in Standard Module). Now I have marked this component for unattended
execution. I have read somewhere that by doing so, I would make this
component a multi-threaded component (despite the fact that VB is not known
for being mult-threaded).
As I understand it, for client apps to use the component contained in
ActiveX Exe, I have to run the ActiveX exe before any client app can create
an instance of it. I am wondering if there is anyway for client to detect
that the ActiveX exe is not running (thus call the executable from command
line first) before creating a new instance.
Now with respect to the global object, it is my intention that its methods
can only be accessed serially. This is to say, assume that there are methods
A and B of this global object. After Client App C accessed Method A, Client
App D cannot access either Method A or Method B until Client App C completes
executing Method A (i.e., Client App D is blocked from accessing any of the
global object's method until Client App C's request in Method A is
completed.). If marking the component as unattended execution makes the
component (thus the global object encapsulated) multi-threaded, would it
defeat my purpose?
TIA
-
Re: ActiveX Exe
First of all, you (read as clients) can use the CreateObject function to get a new instance of the global object you are talking about. If the ActiveX isn't running, it will be started. if it is running, that one will be used.
Second of all. The exe will only have 1 thread, just like a regular VB program. What you will have is that several methods can be called at the same time by different client applications, but only if the activex exe finds the time to do so (like when using a lot of DoEvents.
I've included some samples to show that.
' this is the activex exe
' the project is called TEST, the class class1, theres also a form called Form1
' this form has a textbox (multiline property true) named text1
' sub a and sub b keep looping for 10 seconds using DoEvents
' sub c sleeps for 10 seconds, not using doevents
private Declare Sub Sleep Lib "kernel32" (byval dwMilliseconds as Long)
public Sub Main()
Form1.Show
End Sub
public Sub SubA()
Form1.Text1.Text = Form1.Text1.Text & "Started SubA" & vbCrLf
Form1.Text1.SelStart = len(Form1.Text1.Text)
Dim RunUntil as Long
RunUntil = DatePart("s", Now) + 10
If RunUntil > 60 then RunUntil = RunUntil - 60
Form1.Text1.Text = Form1.Text1.Text & "RunUntil = " & RunUntil & vbCrLf
Do Until DatePart("s", Now) = RunUntil
Sleep 50
DoEvents
Loop
Form1.Text1.Text = Form1.Text1.Text & "Left SubA" & vbCrLf
Form1.Text1.SelStart = len(Form1.Text1.Text)
End Sub
public Sub SubB()
Form1.Text1.Text = Form1.Text1.Text & "Started SubB" & vbCrLf
Form1.Text1.SelStart = len(Form1.Text1.Text)
Do Until DatePart("s", Now) = RunUntil
Sleep 50
DoEvents
Loop
Form1.Text1.Text = Form1.Text1.Text & "Left SubB" & vbCrLf
Form1.Text1.SelStart = len(Form1.Text1.Text)
End Sub
public Sub SubC()
Form1.Text1.Text = Form1.Text1.Text & "Started SubC" & vbCrLf
Form1.Text1.SelStart = len(Form1.Text1.Text)
Sleep 10000
Form1.Text1.Text = Form1.Text1.Text & "Left SubC" & vbCrLf
Form1.Text1.SelStart = len(Form1.Text1.Text)
End Sub
So far the activex exe, now the client
Create a form with 3 buttons, the buttons have the caption A,B and C.
private Sub Command1_Click()
Dim o as Object
set o = CreateObject("TEST.class1")
o.main
o.suba
End Sub
private Sub Command2_Click()
Dim o as Object
set o = CreateObject("TEST.class1")
o.main
o.subb
End Sub
private Sub Command3_Click()
Dim o as Object
set o = CreateObject("TEST.class1")
o.main
o.subc
End Sub
To test it, make sure to compile both projects to an exe. Start 3 instances of the client. Press a different button on each client and look what happens. After the first button has pressed, the form should show.
When pressing the second button, execution of A will halt until B is completed. When pressing the C button, you will see that B's executions stops until C has returned.
So if you click them all, you should see
Started A
Started B
Started C
Ended C
Ended B
Ended A
Tom Cannaerts
[email protected]
Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
-
Re: ActiveX Exe
Again thanks for the help. I am very grateful for the clarification, esp the sample code.
I actually found out that it was from Microsoft Enterprise/Pro Guide on VB, that I read marking the ActiveX exe would make it a multi-threaded app. It is rather strange, I suppose.
Now just one more clarification: am I correct that I need to use CreateObject, rather than new, to allow client app to create an instance of ActiveX Exe, if it has not already been created.
TIA