vb msgbox after completion of a process - vb.net-2010

I am running a batch file with shell function in VB2010 with the following command
Shell("C:\test.bat", AppWinStyle.NormalFocus)
This process takes a lot of time to complete, might even take a day to complete depending on the input file.
I want a MsgBox to display a "Job Finished" msg when the process is finished.
something like
MsgBox("Job Finished")
How do I do that. I am very new to VB, so please help me with full code.
Thank you

This will basically wait till the process finishes (It finishes by exiting. as most batch files do. I'm only making an assumption though).
Sub Main()
Dim P As New Process
P.StartInfo.FileName = "C:\test.bat"
Try
P.Start()
P.WaitForExit()
MsgBox("Process completed successfully")
Catch ex As Exception
MsgBox("Error:" & ex.Message)
End Try
End Sub

Related

SaveFileDialog makes application freeze on close

I experience a wierd problem with SafeFileDialog: When used, application can't be closed properly and hangs in Background Processes in Task Manager, where I have to kill it (and it takes like 10 minutes before Windows kills it).
Try
SaveFileDialog1.Filter = "Text Files (*.txt*)|*.txt|Report file (*.rpt)|*.rpt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Console.WriteLine("Hello World")
End If
Catch ex As Exception
Console.WriteLine("ERROR during saving report file. " & ex.Message)
End Try
No error or output message is produced and it seem to work as intended otherwise ("Hello World" is written into output).
Any ideas?

Looped Tasks Submitted Inside Try/Catch Blocks

I have a background task "submitter" that handles one or more scheduled tasks:
...
While sdr.read()
...
Dim oBackground As New Background
Task.Run(Sub() CallByName(oBackground, sProcessKey, CallType.Method, iPQID))
End While
Before you ask, yes, the tasks are thread-safe. :)
I want the submitter to end (go back to sleep) while the tasks operate; awaiting the next heartbeat, but I need to catch and handle exceptions in each thread.
The only way I've come up with to do this is to create a new task for each background job to be run and handle the exception there, but this seems inefficient:
Create new Task for each background job that then, submits the actual job and waits to see if an error happened and handles it:
Private Sub NewJob(sProcessKey, iPQID)
Dim t As Task
....
While sdr.read()
...
Dim oBackground As New Background
t = Task.Run(Sub() CallByName(oBackground, sProcessKey, CallType.Method, iPQID))
Try
t.Wait()
Catch ex As AggregateException
For Each IEx As Exception In ex.InnerExceptions
HandleBackgroundException(IEx, sProcessKey)
Next
End Try
End While
End Sub
Isn't there a better way to do this??
Thanks!
I believe I've found the answer to my own question.
Since each task is it's own thread, no additional thread submittal is needed.
The task is run directly and error handling happens within the task as shown above (but without Await'ing it). Thus:
Public Shared Sub MyTask()
Try
... do some work here ...
Catch ex As AggregateException
For Each IEx As Exception In ex.InnerExceptions
HandleBackgroundException(IEx, sProcessKey)
Next
End Try
End Sub
That's it!
I hope this helps someone else.

Get output of external console app to RichTextBox

I used the VB.Net Shell() command to start a console app that I haven't created. I want to get the lines from that console to a RichTextBox in my form. It should be possible because I have seen many apps that do this. Please provide some information or any program that might help me. I tried to see if the external app creates log files, but it does not.
Here's how I started. What should I add, and where, to return the output?
Try
writer.WriteLine(RichTextBox1.Text)
writer.Close()
Shell(CurDir() & "\yelloeye.exe .\new.demo")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Here's an example using the RedirectStandardOutput property as mentioned by #Plutonix which will ping SO and display the results in a RichTextBox. Just replace the .exe name and arguments (if any) to suit your needs.
Private Sub ShellToRTB()
Dim p As New Process()
p.StartInfo.FileName = "ping.exe"
p.StartInfo.Arguments = "www.stackoverflow.com"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.Start()
RichTextBox1.Text = p.StandardOutput.ReadToEnd()
p.WaitForExit()
End Sub
Result:

Process Start and Errors from Slow Applications

I made an application that our company uses to launch databases and updates them on the users machine, when needed.
I am having a slight problem when it comes to launching databases and the database starts up slow. When this occurs my application throws an exception, as I assuming its awaiting some kind of response back.
As of now the error thrown is: The system cannot find the file specified
I am trying to prevent this exception logging for cases like this(Slow Application), but still allow the logging if a real error occurs while opening a database.
Current Code I am using:
Private Sub OpenApplication()
If File.Exists(LocalPathString) Then ' File Found. Open the File.
Try
Dim ps As New Process
ps = Process.Start(LocalPathString)
Catch ex As Exception
ex.Source += " | " & LocalPathString
RaiseEvent ShowError(ex)
Finally
RaiseEvent CancelIt() ' Thread Complete. Close the ActionForm
End Try
Else
If LocalPathString = vbNullString Then
RaiseEvent CancelIt() ' No file exits. Cancel thread.
Else
RaiseEvent ShowError(New Exception("Database Not Located: " & LocalPathString))
End If
End If
End Sub
StackTrace:
System.Diagnostics.Process.StartWithShellExecuteEx(startInfo As ProcessStartInfo)
App.exe: N 00912
System.Diagnostics.Process.Start()
App.exe: N 00136
System.Diagnostics.Process.Start(startInfo As ProcessStartInfo)
App.exe: N 00049
SAMi.ActionClass.OpenApplication()
App.exe: N 00117
Maybe I'm missing something, but why don't you simply omit the logging if you found that specific exception?
Catch ex As Exception
ex.Source += " | " & LocalPathString
if not ex.Message.Contains("The system cannot find the file specified") Then
RaiseEvent ShowError(ex)
end if

Can't delete file. File in use by another process

I have a program (simple file updater) which downloads a file. Before that, an old version of this file is queued for deletion. But if I edit this file e.g. in text editor (save and close it), then my program refuses to delete it.
I have sub like this
Private Sub delete_file(ByVal dir As String)
Try
If My.Computer.FileSystem.FileExists(dir) Then My.Computer.FileSystem.DeleteFile(dir)
Catch ex As Exception
Debug.WriteLine(ex.ToString())
Sleep(1000)
delete_file(dir)
End Try
End Sub
It never goes out of the recursion. Exception says that file is being used by other process, and waiting doesn't change anything.
Any clues?
[EDIT]
Changed Sub a bit, so it doesn't contain recursion in Exception handler
Private Sub delete_file(ByVal dir As String)
Dim ok As Boolean = True
Try
If My.Computer.FileSystem.FileExists(dir) Then My.Computer.FileSystem.DeleteFile(dir)
Catch ex As Exception
Debug.WriteLine(ex.ToString())
ok = False
End Try
If ok = False Then
Sleep(1000)
delete_file(dir)
End If
End Sub
Words "another process" are VERY ambiguous.
It turned out that other function from my program was opening the same file twice, and closing it only once. Fixing that removed problem with deleting it.
So when u're having the same error, try searching your program for other places when this file may be modified.
Thanks for comments, they surely gave me some hints about good programming.