Jump to content

Recommended Posts

Posted

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.

Posted

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

Posted

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.

Posted

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

Posted

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")

Posted

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.Regards

OffScrub10.vbs

Posted

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

Posted
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()

Posted (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 by SovietRussia
Posted

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

Posted

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()

Posted
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

Posted
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...
Posted (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 by engineer_z
Posted

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

Posted

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

Posted
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...