how to use windows service in windows form application [closed]

喜你入骨 提交于 2019-12-14 04:15:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!