How to make my System close and re-open automatically? - vb.net

Im making a system, and as part of the system i need to have a reset function. I have tried Application.reset, environment and shell, but nothing seems to be working with the 2017 VB.net console app?

Just run the program and let the current instance exit. Warning: don't run this example. It'll create and endless loop of spawned processes.
Module Module1
Sub Main()
Process.Start("ConsoleApp2")
End Sub
End Module

Related

VB.Net Application starts as background process in Windows 10

We have an internally developed VB.Net Windows Forms Application that handles all our Auto-Updating for our other software applications. Whenever I run the application in Windows 10, the application starts under the "Background Processes" section of the Task Manager. The only way I can get it to run in the foreground is to run it as an Administrator even though I'm an Admin on the VM and my UAC settings are turned all the way down. This doesn't happen in Windows 7 or 8 so I'm wondering if there's something I'm doing wrong or something about Windows 10 that's changed where it has to be run as an Admin. Our other applications don't seem to have this issue, it's just specifically this one application for some reason but I can't seem to figure out what's different.
So I put some more error handling in and it sounds like #xfx was onto the right idea. The application is erring when it starts up because it can't register the URL of the WCF service so it never got to the point where it was displaying the form. Once I manually registered the URL of the WCF service and ran the application as an Admin, it displayed and worked like normal.
This is because of the way the project has been configured in Visual Studio.
Here's how to create an application that behaves like a background process:
In the Project settings dialog, disable the Enable application framework option
Next, change the Startup object to Sub Main
Finally, add a Module to the project and add the following code:
Module Module1
Sub Main()
Application.Run()
End Sub
End Module
If you run the application (not from within the IDE, but directly) it will behave just like the one you describe.
The application will remain as a background process as long as it doesn't display a Form. As soon as one is displayed, you will see that Task Manager moves the process from the Background processes list to the Apps list.
To test this, just change the code in the Module for this version:
Imports System.Threading
Module Module1
Sub Main()
Dim tmp As New Thread(Sub()
Thread.Sleep(3000)
Using f As New Form1()
f.ShowDialog()
End Using
End Sub)
tmp.Start()
Application.Run()
End Sub
End Module
The application will start as a background process and 3 seconds later will display form, becoming a foreground process.

VB.net program with no UI

I'm making a VB.net program via a text file and I'm compiling it using vbc.exe via command line. I'm trying to get just a simple program to run in the background of my computer. Problem is, it displays the annoying console window. How do I get nothing to show? No form, no console?
Just use windows forms application don't load the form at all! Just go in project properties and uncheck enable application framework. Now, in the startup object dropdown, select "sub main". Add a module to the project and put a Public Sub Main() in it. You do all the stuff in main() and don't load form at all.
I think you need a form of some kind to keep the message loop going.
Maybe a NotifyIcon type program. It would keep it away from the task bar and desktop areas.
And then customize the NotifyIcon to "Only Show Notifications" from the "Customize" menu for your icon using Windows.
1) Add a module in your project, and create Sub Main
2) Write whatever you want in Sub Main,and MAKE SURE you end it with this statement:
Application.Run()
3) Open properties of your project and choose "Sub Main" as startup object
So , your application will have NO INTERFACE (NO FORM / NOT CONSOLE APPLICATION) and will run from Sub Main(), in addition it will NOT TERMINATE once all the code in Sub Main has executed.Your program will run like a NORMAL windows form application, and will only exit when you want.

Getting Startup Location In Windows

I've written a small program to open at startup, but I want to give the user the ability to delete it from showing on startup by clicking a button.
But it needs to be compatible on XP, Vista and Windows 7.
Is there a line of code which will get the default startup folder path automatically so I can then delete it using my button?
Thank you in advance
What you are wanting is the SpecialFolder.Enumeration. and use the Environment methods
Something like this Environment.GetFolderPath(Environment.SpecialFolder.Startup)
To do your deletion you need to use System.IO.File.Delete(path)
To check to see what files are out there try making a console application and use this code.
Module Module1
Sub Main()
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup)
For Each file In System.IO.Directory.GetFiles(path)
Console.WriteLine(file)
Next
Console.ReadLine()
End Sub
End Module

A strange behaviour shared variable issue in VB.net

When I was using VB.net , I came across a very strange behaviour, I created a simple test WPF project to reproduce it. here is the details. I have a very simple class, which when an instance is created, the class will create a test.txt file
Public Class Test
Public Sub New()
Using writer As New System.IO.StreamWriter("test.txt")
writer.Write("test")
End Using
End Sub
End Class
Then in the Application.xaml.vb
Class Application
' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
' can be handled in this file.
Shared tt As New Test()
End Class
I simply define a shared variable. my expectation of this are, when I start the application, the variable will be initiated, and a "test.txt" file will be created.
If the Configuration is "Debug" , everything is fine.
If the Configuration is "release", When I start press F5 in Visual Studio 2010, everything is fine as well, it worked as expected, file had been create
But When I start it without debug, press (Ctrl+F5), the variable had not been initiated, file had not been created as I expected.
I am not fully understand why this happen, Can anyone help me out?
Thanks and regards
Is this shared variable accessed somewhere? It could been removed due to compiler optimization. Try to add some code that uses the variable.

Start VB.NET GUI app using Sub Main or form startup object?

Is there any reason to start a GUI program (application for Windows) written in VB.NET in the Sub Main of a module rather than directly in a form?
EDIT: The program won't take any command line parameters and it will be executed as a GUI program always.
The primary reason for using Main() in VB .NET 1.x was for adding code that needed to run before any forms were loaded. For example, you might want to detect whether an instance of your Windows Forms app was already loaded. Or you might want to intercept any unhandled exception for the AppDomain:
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf MyExceptionFilter
But the next version of VB and Visual Studio 2005 introduced a new Application model that made Main() unnecessary in most scenarios. You can now intercept the My.Application.Startup event to add code that needs to run before any forms are loaded.
Note that the code for the Startup event handler is stored in the ApplicationEvents.vb file, which is hidden by default.
You can do it either way, but you should really only keep code in the form that is directly related to the operations and user interface elements on that form. Application startup code isn't related to UI, normally concerned with splash screens, checking network connectivity, verifying a single instance only, setting up user configuration settings, and so on.
After the above items (or the appropriate initialization code for your app) are complete, Sub Main can create an instance of the main form, then show it so the user can begin interacting with your application.
This separates startup code from your form code. Later, when you're maintaining the application, you'll be glad you separated the two.
Yes, and I have done it a few times.
One reason is, that if your app is COM EXE (speaking now from a VB6 point of view) then you want to be able to detect in what context the EXE is being called (being launched or being spoken to by some other app).
For example:
Sub Main()
If App.StartMode = vbSModeAutomation Then
...
Else
...
End If
End Sub
Another is if you want your app to be able to handle any command line parameters.
For example:
Sub Main()
If App.PrevInstance Then End
If InStr(Command, "/s") > 0 Then
Form1.Show
ElseIf InStr(Command, "/p") > 0 Then
LoadPicture ("c:\windows\Zapotec.bmp")
End If
End Sub
(from one of my attempts to make a screen saver)
No, if you always want to show that form.
Yes, if you sometimes want to use your app without GUI, just using command line.
Yes, if I want to display different forms depending on some parameter (in a file, on a remote server, etc.).