Hi,
I am using EViews COM Automation to run some programs, but sometimes I experience some problems with floating license and I'd like to interrupt the Eviews Process, so other colleagues can use EViews. Here is the code I wrote in VBScript:
Dim mgr, oEViewsApp
Set mgr = CreateObject("EViews.Manager")
Set oEViewsApp = mgr.GetApplication(1)
oEviewsApp.Hide()
oEviewsApp.Run("run ""C:\run.prg""")
oEviewsApp.Run("exit")
Set oEviewsApp = Nothing
Set mgr = Nothing
When there's no exception, the program runs normally and exits with no problem, but when there's an exception, say EViews is already opened, I won't be able to finish the process. Is there a way so that I can interrupt the process when I get a problem directly without using oEviewsApp.Run("exit") or someone has another solution? Thank you in advance!
EViews COM Automation Interrupt Process VBScript
Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt
-
EViews Steve
- EViews Developer
- Posts: 844
- Joined: Tue Sep 16, 2008 3:00 pm
- Location: Irvine, CA
Re: EViews COM Automation Interrupt Process VBScript
Ok, a few things to discuss here...
First, if you call GetApplication and pass a 1, this means ExistingOrNew. So if EViews happens to be already running, this code will connect to that instance and basically take it over. This might not be a "safe" thing to do as the user might be in the middle of doing something in EViews (like have a dialog open) and it might be in a state where you can't simply run a program right then and there. I would suggest you use NewInstance (0) instead. Then you don't have to call Hide as EViews is hidden by default in this case.
Next, when you run any EViews command, there could be an error generated by that command. When an error is encountered by the app.Run method, the error is returned to you as a thrown Exception (at least for VBA clients). This means you should have an error handler to catch these exceptions and handle them properly.
When running an EViews program, you can tell the program to ignore a certain amount of errors by passing a number option to the Run command itself, like this:
This will tell EViews to ignore upto 10 exceptions and keep running until the program ends or the 11th error is encountered. The 11th will then be thrown as an exception like I discussed above.
Finally, the call to app.Run("exit") is not required if EViews is currently hidden. By default, if EViews is hidden and all COM clients release their connections to EViews, it will automatically quit within a few seconds.
So here's a modified example of how I would call EViews:
Steve
First, if you call GetApplication and pass a 1, this means ExistingOrNew. So if EViews happens to be already running, this code will connect to that instance and basically take it over. This might not be a "safe" thing to do as the user might be in the middle of doing something in EViews (like have a dialog open) and it might be in a state where you can't simply run a program right then and there. I would suggest you use NewInstance (0) instead. Then you don't have to call Hide as EViews is hidden by default in this case.
Next, when you run any EViews command, there could be an error generated by that command. When an error is encountered by the app.Run method, the error is returned to you as a thrown Exception (at least for VBA clients). This means you should have an error handler to catch these exceptions and handle them properly.
When running an EViews program, you can tell the program to ignore a certain amount of errors by passing a number option to the Run command itself, like this:
Code: Select all
app.Run("run(10) ""c:\run.prg""")Finally, the call to app.Run("exit") is not required if EViews is currently hidden. By default, if EViews is hidden and all COM clients release their connections to EViews, it will automatically quit within a few seconds.
So here's a modified example of how I would call EViews:
Code: Select all
Public Sub test()
On Error GoTo ErrorHandler
Dim mgr As New EViews.Manager
Dim app As EViews.Application
Set app = mgr.GetApplication(NewInstance)
app.Run "run(100) c:\files\errortest.prg"
ExitHandler:
Set app = Nothing
Set mgr = Nothing
Exit Sub
ErrorHandler:
Dim msg As String
msg = Err.Description
'do something with the error?
Resume ExitHandler
End SubRe: EViews COM Automation Interrupt Process VBScript
Perfect! This is exactly what I needed. I got all my questions answered in this post. Thak you!
Who is online
Users browsing this forum: No registered users and 2 guests
