Console App / Task Scheduler - vb.net

I have a console app written in VB Net that works perfectly. Now I want to run it using task scheduler. The problem is that the app has a console.readline command at the very end that keeps the console window open until the user hits enter. Is there a way to test whether the app is running in a session or not?
If I knew that the app was not tied to a desktop console, I would not write the comments to the console and I'd bypass the final console.readline command.

You should add an argument to your task to indicate it is unattended. For example, pass /u in your scheduled task. Check for /u in your program to determine if it should skip the console.readline.
excerpt from msdn forum
Dim args() As String = Environment.GetCommandLineArgs()
' args(0) = Full path of executing program with program name
' args(1) = First switch in command - /u
if args(1) = "/u" then ....
Or you can change the signature of your Main() to Public Sub Main(ByVal args() As String) and you won't need to use Dim args() As String = Environment.GetCommandLineArgs()

Related

Error thrown trying launch a webpage from a button

I created a button and a menu item in vb.net (.NET 6). I found several answers here on SO that say the process to launching a webpage from such an event can be launched with this code:
Dim webAddress As String = "http://www.example.com/"
Process.Start(webAddress)
However, trying launch the code, I'm given the error of "system cannot find the file specified".
Looking more into it, I know that .NET 6 is running a bit differently and changed the code to the following:
Using link As New Process()
link.StartInfo.UseShellExecute = True
link.Start(New ProcessStartInfo("https://example.com"))
End Using
But still to no avail, and I am given the same error. "System cannot find the file specified." I can run addresses via the regular Windows Run prompt... but the program still cannot launch.
Following Jimi's comment to my original question, I changed the Sub to the following:
Sub LaunchWebsite(strWebpageURL As String)
Using Process.Start(New ProcessStartInfo(strWebpageURL) With {.UseShellExecute = True})
End Using
End Sub
Using this, the webpage launched in my desktop's default browser with no problem.
You can use
Respone.Redirect("http://www.example.com/")
or use javascript in server side code
Dim url As String = "http://www.example.com"
Dim s As String = "window.open('" & url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');"
ClientScript.RegisterStartupScript(Me.GetType(), "script", s, True)
Above code open webpage in new popup window.
Regards
Aravind

run external program and exit from calling program

n the closing procedure of a program (just before running Application.exit) I need to run an external program to pass a parameter and exit the calling program.
The program called (FilmDB_Update.exe) has the task of overwriting the main program or a dll library.
I tried to use the "process.start" technique, but apparently, the calling program remains in use and does not allow me to overwrite it.
This is the code that I write:
Private Sub AggiornaPgm()
Dim ws_file As String = "FilmDB_Update.exe"
Dim ws_proc_param As String = """" + ws_working_path + """ " + ws_temp_path
Dim ws_fullPath As String = Path.Combine(ws_temp_path, ws_file)
If File.Exists(ws_fullPath) Then
File.Copy(ws_fullPath, ws_file, True)
End If
Dim proc As New System.Diagnostics.Process()
proc = Process.Start(ws_file, ws_proc_param)
End Sub
I wanted to try using the shell command, but I can not pass the parameters to the called program.
Does any of you have any other ideas about it?
Thank you
Marcello
As Ahmed suggested, I added the test for the calling process to the program called.
p = Process.GetProcessesByName(ws_calling_pgm)
While p.Count > 0
Threading.Thread.Sleep(3000)
p = Process.GetProcessesByName(ws_calling_pgm)
End While
p = Nothing
When I exit the While loop, the calling process is terminated. I do not understand why, despite the process no longer exists, the main program is still in use.

Writing to Command Line Not Working

An application I need to use (USB capture utility) has a .cmd version that I can call from my Visual Basic code. I am able to launch the application and put it in "command line mode" like this:
Public Class MyClass
Dim StreamWriteUtility As System.IO.StreamWriter
Dim StreamReadUtility As System.IO.StringReader
Dim ProcessInfo As ProcessStartInfo
Dim Process As Process
Public Sub StartUSBCapture(ByVal DataStorageLocation As String)
Dim ProcessInfo As ProcessStartInfo
Dim Process As New Process
ProcessInfo = New ProcessStartInfo("C:\FW_Qualification_Suite\data-center-windows\data-center\bin\datacenter.cmd", "-c ")
ProcessInfo.CreateNoWindow = True
ProcessInfo.UseShellExecute = False 'Must be changed if redirect set to True
ProcessInfo.RedirectStandardInput = True
Process = Process.Start(ProcessInfo)
SWUtility = Process.StandardInput
While True
SWUtility.WriteLine("run") 'Looping for test to ensure this isn't a timing issue
End While
End Sub
End Class
This launches the application and opens a separate command line window that should accept further commands (i.e., capture, run, stop, etc). However, I am having trouble getting those subsequent commands to show up in the command line window. I've tried redirecting the standard input of the process, but still nothing shows up in the console window.
Can anyone tell how I'm supposed to actually get these commands from my Visual Basic program into this application?

Running a advanced java call from VB.net

I need to run a small piece of Java code (Java is the only option in this case)
I have the jar file in the VB.net resources as JSSMCL(the extension is not required to run it, of this I am sure :P) I know I use Path.GetFullPath(My.Resources.ResourceManager.BaseName)
but no mater how I do it it fails, I have tried so many ways i have lost count!
this is the command that I need to run:
java -cp "JSSMCL.jar" net.minecraft.MinecraftLauncher username false
You can use System.Diagnostics.Process class and its method to start/run the external process.
Refer to the following code part to run the Command using Process
Sub Main()
' One file parameter to the executable
Dim sourceName As String = "ExampleText.txt"
' The second file parameter to the executable
Dim targetName As String = "Example.gz"
' New ProcessStartInfo created
Dim p As New ProcessStartInfo
' Specify the location of the binary
p.FileName = "C:\7za.exe"
' Use these arguments for the process
p.Arguments = "a -tgzip """ & targetName & """ """ & sourceName & """ -mx=9"
' Use a hidden window
p.WindowStyle = ProcessWindowStyle.Hidden
' Start the process
Process.Start(p)
End Sub
EDIT:
Use the Coding part like below, may be it works
-jar "compiler.jar" --js_output_file="myOutput.min.js" --js="input1.js" --js="input2.js"
Have a look at this link for your problem

Installing Windows services gives an error

I created a simple windows service on my local PC and added the following code to it
Protected Overrides Sub OnStart(ByVal args() As String)
Const iTIME_INTERVAL As Integer = 60000 ' 60 seconds.
Dim oTimer As System.Threading.Timer
System.IO.File.AppendAllText("C:\AuthorLog.txt", _
"AuthorLogService has been started at " & Now.ToString())
Dim tDelegate As Threading.TimerCallback = AddressOf EventAction
oTimer = New System.Threading.Timer(tDelegate, Me, 0, iTIME_INTERVAL)
End Sub
Protected Overrides Sub OnStop()
End Sub
Public Sub EventAction(ByVal sender As Object)
System.IO.File.AppendAllText("C:\AuthorLog.txt", _
"AuthorLogService fires EventAction at " & Now.ToString())
End Sub
Next I added a Setup project to this solution and added a custom action (By double clicking application folder then clicking add output folder then selecting primary output from the dialog). The solution builds fine but I have 2 problems.
1) Everytime I install the service, it asks me for the username, password and confirm password; I was wondering if there was anyway to get rid of it atleast while running locally. I tried setting the account type to user, local service, local system etc but it keeps popping up.
2) Once I enter the credentials (random ones), I get an error "No mapping between account names and security ids was done".
Kindly help me out
1: You could make your service be selfinstalling as in this codeproject article and then just send in the username/password you want to use to the ServiceProcessInstaller.
2: Try entering the credentials in a different format. If you're currently using ".\user" try writing "computer\user" or vice versa.