问题
how to use windows service in windows form application? I am having the database table , consists of Gold, silver,etc.. Prices. These can be displayed in Windows Form.
I want to Update those things periodically in Windows Form(example : for each 10 mins, need to update). Is there possible way available?
回答1:
You can use Timer to periodically update the database
Timer timer = new Timer();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (10) * (1); // Timer will tick evert 10 seconds
timer.Enabled = true; // Enable the timer
timer.Start();
void timer_Tick(object sender, EventArgs e)
{
//Put your technique for updating database here
}
You can invoke service like this
using System.ServiceProcess;
ServiceController sc = new ServiceController("My service name");
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}
回答2:
Use the Forms timer.
If the update takes more than a few seconds, do the work in a background worker. If you use the background worker, I'd wrap it in code to prevent multiple simultaneous executions.
来源:https://stackoverflow.com/questions/17021210/how-to-use-windows-service-in-windows-form-application