c# 定期重启程序操作的实现
作者:kingwebo'sZone
本文主要介绍了c# 定期重启程序操作的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
一、Restart方法
System.Windows.Forms.Application.Restart();
经测试发现有时候只会关闭程序,并不会重新启动
二、Process.Start()和Exit()
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); Application.Exit();
经测试发现有时候也只会关闭程序,并不会重新启动
三、进程的Start和Kill方法
System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); System.Diagnostics.Process.GetCurrentProcess().Kill();
经测试使用进程进行重启比较稳定。
//开启新的实例 System.Diagnostics.Process.Start(Application.ExecutablePath); //关闭当前实例 System.Diagnostics.Process.GetCurrentProcess().Kill(); Application.Exit();//退出当前项目,如果是子项目,则不会停止主项目 System.Environment.Exit(0);//停止所有项目
四:使用Process方式
Process p = new Process(); p.StartInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory + “xxx.exe”; p.StartInfo.UseShellExecute = false; p.Start(); Application.Current.Shutdown();
未测试。
带参数重启
Process proc = new Process(); proc.StartInfo.FileName = @"MyExecutable.exe"; proc.StartInfo.Arguments = "\"C:\\My Docs\\SomeDirectory\\MyXMLPath.xml\""; proc.Start();
补
我的数据库结构:
GO /****** Object: Table [dbo].[RestartLog] Script Date: 09/04/2023 20:45:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[RestartLog]( [Id] [int] IDENTITY(1,1) NOT NULL, [RestartDate] [datetime] NULL, [ThisPCName] [nvarchar](80) NULL, [IPAdd] [nvarchar](80) NULL, [CreateDate] [datetime] NULL ) ON [PRIMARY] GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'重启时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'RestartLog', @level2type=N'COLUMN',@level2name=N'RestartDate' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'计算机名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'RestartLog', @level2type=N'COLUMN',@level2name=N'ThisPCName' GO ALTER TABLE [dbo].[RestartLog] ADD CONSTRAINT [DF_RestartLog_CreateDate] DEFAULT (getdate()) FOR [CreateDate] GO
我的DAL处理操作方法:
/// <summary>
/// 主要的数据操作
/// </summary>
/// <returns></returns>
public ResultMsg InsertAndUpdate(int HourRestart)
{
ResultMsg msg = new ResultMsg();
try
{
string SqlStr = $@" DECLARE @IPadd nvarchar(80); ---Ip地址
DECLARE @PCName nvarchar(100); --计算机名
DECLARE @TempId int ;--0无数据,1时间未到,2需要重启
DECLARE @OutInt int;---返回数据
DECLARE @RestartHour int;---重启时间
Select @IPadd= CONVERT( nvarchar(80) ,CONNECTIONPROPERTY('CLIENT_NET_ADDRESS') ) ;
Select @PCName= HOST_NAME();
set @RestartHour={HourRestart};
if exists( SELECT * FROM [RestartLog] WHERE [IPAdd] = @IPadd )
begin
if exists( SELECT * FROM [RestartLog] WHERE [IPAdd] = @IPadd and datediff(hour,RestartDate,getdate())>@RestartHour)
begin
update [RestartLog] set RestartDate = GETDATE() WHERE [IPAdd] = @IPadd and datediff(hour,RestartDate,getdate())>@RestartHour;
set @OutInt = 2;--需要重启
end
else
begin
set @OutInt = 1;--时间未到不需要需要重启
end
end
else ----无数据,需要插入数据
begin
insert [RestartLog] ( [RestartDate] ,[ThisPCName] ,[IPAdd],[CreateDate]) values (GETDATE(),@PCName,@IPadd,GETDATE());
set @OutInt = 0;
end
select @OutInt; ";
msg.ReturnInt = DapperDbHelper.ExecuteScalar<int>(SqlStr);
msg.Success = true;
}
catch (Exception ex)
{
msg.Success = false;
msg.ErrMsg = ex.Message;
}
return msg;
}到此这篇关于c# 定期重启程序操作的实现的文章就介绍到这了,更多相关c# 定期重启内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
