I have a winform application (.exe file) in my windows server 2008 VPS.
This app should grab some data from internet every 5 minutes and save them to a text file.
My VPS restarts after 2 or 3 days for no reason.
I want to force my server run that application on startup & execute it in every reboot.
For this purpose i followed this instruction :
It is simple. The process is.
- Run gpedit.msc
- Go to computer Configuration -> Windows Setting -> Scripts(Startup/shutdown)
- Go to Startup properties then you will get the new windows.
- Now add the program that you want to run before login.
The problem is that file is executed on startup, but after login to windows i can not see it's form is running.
I can see that app is running in task manager.
How can i see that winform after login to windows?(I need GUI)
I mean my application must be running even before log in and it should sit on System Tray from which I can "show" Interface for interact.
If there is a way in c# programming for show that app after login please share.
You need to separate the services of your application from UI.
Here I'll share a step by step by step example including:
As a result, after installation the service will be started in windows startup. The GUI application also will start when user logs into the application. Then if you click on a button it will show a message from service.
Clone or Download:
Create a class library project (ServiceLayer)
Add a new WCF service to the class library. (MyWCFService)
Modify the contract:
[ServiceContract]
public interface IMyWCFService
{
[OperationContract]
string Echo(string message);
}
Modify the implementation of MyWCFService:
public class MyWCFService : IMyWCFService
{
public string Echo(string message)
{
return message;
}
}
Add a Windows Service. (MyWindowsService)
Modify the implementation of the service:
partial class MyWindowsService : ServiceBase
{
ServiceHost host;
public MyWindowsService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
host = new ServiceHost(typeof(MyWCFService));
host.Open();
}
protected override void OnStop()
{
host.Close();
}
}
Double click on MyWindowsService, then right-click on the surface and choose Add Installer
Double click on ProjectInstaller.cs and from list of components, choose serviceInstaller1, Then in properties window, set StartType to automatic.
Choose serviceProcessInstaller1 and then in properties window, set Account to LocalSystem.
Add a C# file. (Program.cs)
Modify the content of Program.cs class to:
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyWindowsService()
};
ServiceBase.Run(ServicesToRun);
}
}
Create a Windows Forms Application (UILayer)
Add reference to MyServiceLayer project
Open Form1 in design mode, drop a button on it and double click to handle its click event and use the following code:
private void button1_Click(object sender, EventArgs e)
{
ChannelFactory<IMyWCFService> factory =
new ChannelFactory<IMyWCFService>("BasicHttpBinding_IMyWCFService");
IMyWCFService proxy = factory.CreateChannel();
MessageBox.Show(proxy.Echo("Hello!"));
}
Modify the app.config file to include WCF configurations:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMyWCFService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8733/Design_Time_Addresses/ServiceLayer/MyWCFService/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyWCFService"
contract="ServiceLayer.IMyWCFService" name="BasicHttpBinding_IMyWCFService" />
</client>
</system.serviceModel>
</configuration>
Make sure the endpoint address
in app.config of UILayer
is same as baseAddress
in app.config of ServiceLayer
.
After the installation is done, you may want to restart the system to see the effect. The service will be started automatically, and the UILayer will be shown after user logon.
Click on the button and it will show a MessageBox saying Hello! This content is coming from service.