我想在后台运行程序。 pref C#

时间:2022-05-13 04:48:33

I want to run program on background . pref C# I want put icon in the tray. On specified time it’s synchronizes folders (I know how to sync folders) . How to run it on background and start sync ( for example at 2am)?

我想在后台运行程序。 pref C#我想把图标放在托盘中。在指定的时间它同步文件夹(我知道如何同步文件夹)。如何在后台运行它并开始同步(例如凌晨2点)?

5 个解决方案

#1


3  

You need to consider using the windows scheduler service.

您需要考虑使用Windows调度程序服务。

#2


1  

To run "in background"

在“后台”运行

Form.Visible = False
Form.ShowInTaskbar = False

To start the synchronization, use a Timer and...

要开始同步,请使用Timer和...

  • Check the time every minute, start synch if the time matches
  • 检查每分钟的时间,如果时间匹配则开始同步

  • OR calculate the interval Now->TimeToStart, set it, start the Timer, and reset it to 24 hours on the first tick
  • 或者计算间隔Now-> TimeToStart,设置它,启动Timer,并在第一个滴答时将其重置为24小时

You might also think about creating a service if you want it to be a background process only.

如果您希望它只是一个后台进程,您也可以考虑创建一个服务。

#3


1  

I very often use Notify Icon on my applications which comes with Visual Studio, http://www.developer.com/net/csharp/article.php/3336751 as for start syncing, you can call a function on another thread which will measure time every second and if it is 2am you can call another function in another thread to sync folders (you can use also a timer component).

我经常在Visual Studio附带的应用程序上使用Notify Icon,http://www.developer.com/net/csharp/article.php/3336751作为开始同步,你可以调用另一个将测量的线程上的函数每秒一次,如果是凌晨2点,你可以在另一个线程中调用另一个函数来同步文件夹(你也可以使用一个计时器组件)。

Resources:

Notify Icon: http://www.developer.com/net/csharp/article.php/3336751

通知图标:http://www.developer.com/net/csharp/article.php/3336751

C# Threading: http://www.albahari.com/threading/

C#线程:http://www.albahari.com/threading/

Timer Component: http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx and http://www.codeproject.com/KB/miscctrl/TimeSheet.aspx

定时器组件:http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx和http://www.codeproject.com/KB/miscctrl/TimeSheet.aspx

#4


1  

If I were you, I'd use the scheduler services in Windows.

如果我是你,我会在Windows中使用调度程序服务。

Having a service, keeping an icon in the tray, etc. means you'll be filling up valuable screen space and consuming resources. What's more, the user will need some way to configure the application so they can choose whether it starts with Windows or not; if you provide this configuration, it'll be necessarily different to how every other application does it.

提供服务,在托盘中保留图标等意味着您将填满宝贵的屏幕空间并消耗资源。更重要的是,用户需要一些方法来配置应用程序,以便他们可以选择是否从Windows启动;如果您提供此配置,则必然会与其他每个应用程序的配置不同。

A scheduler task, on the other hand, only needs one process - the task scheduler itself - and the user can disable or delete the task themselves. Both the Apple and Google automatic updater processes now run in this way, for example.

另一方面,调度程序任务只需要一个进程 - 任务调度程序本身 - 用户可以自己禁用或删除任务。例如,Apple和Google自动更新程序流程现在都以这种方式运行。

#5


1  

Well, if you start with a Windows Forms Application, and want it to run in background, hanging from the system tray, you have to create the system tray, hide the main form before Application.Run(MainForm), and you're on.

好吧,如果你从一个Windows窗体应用程序开始,并希望它在后台运行,挂在系统托盘上,你必须创建系统托盘,在Application.Run(MainForm)之前隐藏主窗体,然后你就开始了。

To hide the form, I use:

要隐藏表单,我使用:

MainForm.WindowState = System.Windows.Forms.FormWindowState.Minimized;
MainForm.ShowInTaskBar = false;

(since setting visible= false will be turned back on by the Run() method.)

(因为设置visible = false将由Run()方法重新打开。)

To create a System Tray, I used instructions from here:

要创建系统托盘,我使用了以下说明:

#1


3  

You need to consider using the windows scheduler service.

您需要考虑使用Windows调度程序服务。

#2


1  

To run "in background"

在“后台”运行

Form.Visible = False
Form.ShowInTaskbar = False

To start the synchronization, use a Timer and...

要开始同步,请使用Timer和...

  • Check the time every minute, start synch if the time matches
  • 检查每分钟的时间,如果时间匹配则开始同步

  • OR calculate the interval Now->TimeToStart, set it, start the Timer, and reset it to 24 hours on the first tick
  • 或者计算间隔Now-> TimeToStart,设置它,启动Timer,并在第一个滴答时将其重置为24小时

You might also think about creating a service if you want it to be a background process only.

如果您希望它只是一个后台进程,您也可以考虑创建一个服务。

#3


1  

I very often use Notify Icon on my applications which comes with Visual Studio, http://www.developer.com/net/csharp/article.php/3336751 as for start syncing, you can call a function on another thread which will measure time every second and if it is 2am you can call another function in another thread to sync folders (you can use also a timer component).

我经常在Visual Studio附带的应用程序上使用Notify Icon,http://www.developer.com/net/csharp/article.php/3336751作为开始同步,你可以调用另一个将测量的线程上的函数每秒一次,如果是凌晨2点,你可以在另一个线程中调用另一个函数来同步文件夹(你也可以使用一个计时器组件)。

Resources:

Notify Icon: http://www.developer.com/net/csharp/article.php/3336751

通知图标:http://www.developer.com/net/csharp/article.php/3336751

C# Threading: http://www.albahari.com/threading/

C#线程:http://www.albahari.com/threading/

Timer Component: http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx and http://www.codeproject.com/KB/miscctrl/TimeSheet.aspx

定时器组件:http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx和http://www.codeproject.com/KB/miscctrl/TimeSheet.aspx

#4


1  

If I were you, I'd use the scheduler services in Windows.

如果我是你,我会在Windows中使用调度程序服务。

Having a service, keeping an icon in the tray, etc. means you'll be filling up valuable screen space and consuming resources. What's more, the user will need some way to configure the application so they can choose whether it starts with Windows or not; if you provide this configuration, it'll be necessarily different to how every other application does it.

提供服务,在托盘中保留图标等意味着您将填满宝贵的屏幕空间并消耗资源。更重要的是,用户需要一些方法来配置应用程序,以便他们可以选择是否从Windows启动;如果您提供此配置,则必然会与其他每个应用程序的配置不同。

A scheduler task, on the other hand, only needs one process - the task scheduler itself - and the user can disable or delete the task themselves. Both the Apple and Google automatic updater processes now run in this way, for example.

另一方面,调度程序任务只需要一个进程 - 任务调度程序本身 - 用户可以自己禁用或删除任务。例如,Apple和Google自动更新程序流程现在都以这种方式运行。

#5


1  

Well, if you start with a Windows Forms Application, and want it to run in background, hanging from the system tray, you have to create the system tray, hide the main form before Application.Run(MainForm), and you're on.

好吧,如果你从一个Windows窗体应用程序开始,并希望它在后台运行,挂在系统托盘上,你必须创建系统托盘,在Application.Run(MainForm)之前隐藏主窗体,然后你就开始了。

To hide the form, I use:

要隐藏表单,我使用:

MainForm.WindowState = System.Windows.Forms.FormWindowState.Minimized;
MainForm.ShowInTaskBar = false;

(since setting visible= false will be turned back on by the Run() method.)

(因为设置visible = false将由Run()方法重新打开。)

To create a System Tray, I used instructions from here:

要创建系统托盘,我使用了以下说明: