engineer_z Posted November 25, 2013 Posted November 25, 2013 Hi there My code is written in vb.net, What am doing is I want to tell the compiler to wait until vbs file finished exeutition then continuoe the rest of the code as the following: myProcess_uninstall = Process.Start("filepath.vbs") myProcess_uninstall.WaitForExit() The problem here is the error message (Object reference not set to an instance of an object) when using myProcess_uninstall.WaitForExit() So, any idea how to solve it.
Steve21 Posted November 25, 2013 Posted November 25, 2013 What have you declared myProcess_uninstall as? Unless it's a process that won't work. Example: Dim MyProcess As Process = Process.Start("C:\test\echo.vbs") MyProcess.WaitForExit() MsgBox("ended") Won't show msgbox until the process ends. If it's complaining about your object I'd take a guess that "myprocess_uninstall" isn't declared as a process, and rather a return code variable? Steve
localzuk Posted November 25, 2013 Posted November 25, 2013 Looks like you're not using the Process class correctly. This is Microsoft's C# example [color=#000000][font=Consolas]//[/font][/color][color=#000000][font=Consolas] Start the child process[/font][/color][color=#000000][font=Consolas].[/font][/color] Process p = [color=#00008B]new[/color] Process(); // Redirect the output stream of the child process. p.StartInfo.UseShellExecute = [color=#800000]false[/color]; p.StartInfo.RedirectStandardOutput = [color=#800000]true[/color]; p.StartInfo.FileName = [color=#800000]"Write500Lines.exe"[/color]; p.Start(); // [color=#00008B]Do[/color] [color=#00008B]not[/color] wait [color=#00008B]for[/color] the child process [color=#00008B]to[/color] [color=#00008B]exit[/color] before // reading [color=#00008B]to[/color] the [color=#00008B]end[/color] of its redirected stream. // p.WaitForExit(); // Read the output stream first [color=#00008B]and[/color] [color=#00008B]then[/color] wait. [color=#00008B]string[/color] output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); So, you need to instantiate a Process, tell it what FileName you're calling and any extra options, and then run the Start method.After that, you run your WaitForExit method.
engineer_z Posted November 25, 2013 Author Posted November 25, 2013 Hi Steve I used your declaration , it's still the same error message. When I try the same code with file.exe it's worked fine. So I think (WaitForExit() function) just worked with exe files and does not accept vbs file and see it as null. regards
Steve21 Posted November 25, 2013 Posted November 25, 2013 I just tested that and that works fine. Can you post exactly what you're using? Thanks, Steve
engineer_z Posted November 25, 2013 Author Posted November 25, 2013 Hi localzuk I used your code and still I got the same error, and as I told steve that waitforexit() function just worked fine with exe file. Regards
LosOjos Posted November 25, 2013 Posted November 25, 2013 Have you tried wrapping the VBS in to a CScript call? Dim MyProcess As Process = Process.Start("CScript.exe C:\test\echo.vbs") MyProcess.WaitForExit() MsgBox("ended")
engineer_z Posted November 25, 2013 Author Posted November 25, 2013 Hi steve This is my code Dim myProcess_uninstall As New Process myProcess_install = Process.Start("C:\Users\admin\Desktop\OffScrub10.vbs") MyProcess.WaitForExit() MsgBox("ended") I have attached the vbs file.RegardsOffScrub10.vbs
localzuk Posted November 25, 2013 Posted November 25, 2013 You've declared "myProcess_uninstall" then assign to "myProcess_install".
engineer_z Posted November 25, 2013 Author Posted November 25, 2013 Sorry this is my code Dim myProcess_uninstall As New Process myProcess_uninstall = Process.Start("C:\Users\admin\Desktop\OffScrub10.vbs") myProcess_uninstall.WaitForExit() Where Still not working Regards
LosOjos Posted November 25, 2013 Posted November 25, 2013 Sorry this is my code Dim myProcess_uninstall As New Process myProcess_uninstall = Process.Start("C:\Users\admin\Desktop\OffScrub10.vbs") myProcess_uninstall.WaitForExit() Where Still not working Regards Try it wrapped in a Cscript call; if it is the fact it's not an exe, this should fix it: Dim myProcess_uninstall As New Process myProcess_uninstall = Process.Start("CScript.exe C:\Users\admin\Desktop\OffScrub10.vbs") myProcess_uninstall.WaitForExit()
engineer_z Posted November 25, 2013 Author Posted November 25, 2013 Hi LosOjos unfortunately still the same error Regards
SovietRussia Posted November 25, 2013 Posted November 25, 2013 (edited) Try this: Dim startInfo As New ProcessStartInfo startInfo.FileName = "cscript" startInfo.Arguments = "[color=#333333]C:\Users\admin\Desktop\OffScrub10.vbs[/color]" Process.Start(startInfo) startInfo.WaitForExit() Edited November 25, 2013 by SovietRussia
engineer_z Posted November 25, 2013 Author Posted November 25, 2013 Hi SovietRussia I have tried it and it give me error with line (startInfo.WaitForExit()) , The error is "WaitForExit() is not a member of system.Diagnostics.processStartInfo" Regards
LosOjos Posted November 25, 2013 Posted November 25, 2013 Missing a step: Dim myProcess_uninstall As New Process Dim startInfo As New ProcessStartInfo startInfo.FileName = "cscript" startInfo.Arguments = "C:\Users\admin\Desktop\OffScrub10.vbs" myProcess_uninstall = Process.Start(startInfo) myProcess_uninstall.WaitForExit()
LosOjos Posted November 25, 2013 Posted November 25, 2013 Just to check, are you importing the necessary dependencies? Imports System Imports System.Diagnostics Imports System.ComponentModel
SovietRussia Posted November 25, 2013 Posted November 25, 2013 Missing a step: Dim myProcess_uninstall As New Process Dim startInfo As New ProcessStartInfo startInfo.FileName = "cscript" startInfo.Arguments = "C:\Users\admin\Desktop\OffScrub10.vbs" myProcess_uninstall = Process.Start(startInfo) myProcess_uninstall.WaitForExit() Sorry! My VB is rusty, I am a C# developer and tried to port it over -epicfail
engineer_z Posted November 25, 2013 Author Posted November 25, 2013 Hi LosOjos I have imported the dependencies and still not working
LosOjos Posted November 25, 2013 Posted November 25, 2013 I think we're going to need to see more of your code, there's no reason the code you've been given shouldn't work unless there's a problem elsewhere in the code...
engineer_z Posted November 25, 2013 Author Posted November 25, 2013 (edited) Option Explicit Off Imports System.IO Imports System.Windows.Forms Imports System.Xml Imports System.Deployment.Application Imports System.Collections.Specialized Imports Microsoft.VisualBasic Imports System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock Imports System Imports System.Diagnostics Imports System.ComponentModel Public Class Form1 Private Property WScript As Object Public objNetwork As New Object Dim ServerName Dim ShareName Dim ShareLetter Dim myProcess_uninstall As Process = Nothing Dim myProcess_install As Process = Nothing Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click End End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Me.Close() '======================== MAP NETWORK DRIVE =================== 'Name of the server where the share exists ServerName = "KBC-PC" 'The name of the shared folder ShareName = "office_2013" 'The letter that will appear on the users machine ShareLetter = "u:" objNetwork = CreateObject("WScript.Network") objNetwork.MapNetworkDrive(ShareLetter, "\\" & ServerName & "\" & ShareName) '================= UNINSTALL OFFICE 2010 ======================= myProcess_uninstall = Process.Start("C:\Users\admin\Desktop\OffScrub10.vbs") myProcess_uninstall.WaitForExit() '================== INSTALL OFFICE 2013 ===================== myProcess_install = Process.Start("C:\office_2013\setup.EXE") myProcess_install.WaitForExit() '================ DISCONNECT NETWORK DRIVE ================== objNetwork.RemoveNetworkDrive(ShareLetter) End Sub End Class Please I need your help ASAP Regards Edited November 25, 2013 by engineer_z
LosOjos Posted November 25, 2013 Posted November 25, 2013 OK, you don't appear to have implemented the changes we've been suggesting, plus there's a rogue space in the file name of the script you're calling.... should be .vbs not .v_bs
engineer_z Posted November 25, 2013 Author Posted November 25, 2013 hi. Even with after updating the changes that you told me , still the same error. And the a bout the file name it complies the start function correctly and opens the file but the waitfor exit function does not work. Regards
LosOjos Posted November 26, 2013 Posted November 26, 2013 hi. Even with after updating the changes that you told me , still the same error. And the a bout the file name it complies the start function correctly and opens the file but the waitfor exit function does not work. Regards Try moving the Me.Close() line to the end of the sub routine
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now