自己做的网站如何链接到百度浅谈电子商务网站的建设与管理
news/
2025/9/23 20:11:52/
文章来源:
自己做的网站如何链接到百度,浅谈电子商务网站的建设与管理,佛山自动机设备骏域网站建设专家,wordpress产品优惠C#制作定时任务工具执行CMD命令 概要准备知识点实现原理thinkphp配置winform执行CMD命令读取ini配置文件定时任务Quartz.Net 完整代码Job.csIniFunc.csForm1.csconfig.ini简易定时任务工具雏形 概要
很多时候写接口上线后还会遇到很多修改#xff0c;类似JAVA,C#,delphi制作的… C#制作定时任务工具执行CMD命令 概要准备知识点实现原理thinkphp配置winform执行CMD命令读取ini配置文件定时任务Quartz.Net 完整代码Job.csIniFunc.csForm1.csconfig.ini简易定时任务工具雏形 概要
很多时候写接口上线后还会遇到很多修改类似JAVA,C#,delphi制作的接口上线后难以修改测试也有困难。为了接口便于制作和修改采用动态语言编写接口定时任务基座的处理方法例如PHP写的接口内容使用定时任务工具定时执行这样即使接口上线后也可以随意修改PHP这种解释性脚本方便修改和定位错误。定时任务工具python也是很好的解决方案定时任务可以采用JAVA或者C#来构建目前采用C#构建定时任务桌面工具
准备
vs2019C# winformphpstudy2016thinkphp3.2.3quartz.net 3.7.2
知识点
实现原理
thinkphp启用cmd执行程序C#利用定时任务框架quartz.net去执行CMD命令
thinkphp配置
开发阶段可以使用phpstudy环境部署阶段采用cmd命令可以不使用网络容器thinkphp开启cli模式入库文件index.php添加一句就可以了
if(version_compare(PHP_VERSION,5.3.0,)) die(require PHP 5.3.0 !);
//添加这一句就可以了
define(MODE_NAME, cli);
...其他不变配置后网络容器和CMD都可以执行thinkphpCMD执行语句
D:\phpStudy\php\php-7.0.12-nts\php.exe D:\phpStudy\WWW\tp3\index.php Home/Index/queryAndWrite说明绝对路径找到php.exe去执行thinkphp中的方法 另外如果不采用自定义基座定时任务工具可以使用win自带的计划任务也行但是计划任务会弹出cmd框所以在cmd命令旁添加一个vb命令执行这个vb命令就不会有弹窗了
Set ws CreateObject(Wscript.Shell)
ws.run cmd /c times.bat,vbhidewinform 项目结构 说明 引用类似java的maven包 Form1.cs窗口1其中Form1.Designer.cs是编译器自动生成的布局代码和Form是分步类等同一个类分成2个文件 IniFunc.cs:读取ini配置文件 Job.cs:具体任务这里只有一个任务但是通过不同的触发器传值形成不同任务分身 Promgram.cs:程序入口 任务类中调用Form1的控件
From1定义为静态类 public static Form1 form1;public Form1(){InitializeComponent();form1 this;}控件设置成public Job通过静态类访问From1控件
var c Form1.form1.textBox1.Text;
MessageBox.Show(c);执行CMD命令
//需要引入using System.Diagnostics;
private void cmd(String t){var p new Process();p.StartInfo.FileName cmd.exe;p.StartInfo.RedirectStandardInput true;p.StartInfo.UseShellExecute false;p.StartInfo.CreateNoWindow true;p.Start();p.StandardInput.WriteLine(t);//p.StandardInput.WriteLine(exit);p.StandardInput.Flush();}读取ini配置文件
Debug目录下新建config.ini
[Information]
job1D:\phpStudy\php\php-7.0.12-nts\php.exe D:\phpStudy\WWW\tp3\index.php Home/Index/queryAndWrite
job2D:\phpStudy\php\php-7.0.12-nts\php.exe D:\phpStudy\WWW\tp3\index.php Home/Index/queryAndWrite2定义文件路径在Form1事件load中选择Form1_Load然后按钮1点击后获取配置文件内容job1和job2是两个定时任务去执行thinkphp中的数据库操作 private string filename null;private void Form1_Load(object sender, EventArgs e){filename Application.StartupPath \\config.ini;//MessageBox.Show(filename);}private void button1_Click(object sender, EventArgs e){//this.textBox1.Text 777;string job1 IniFunc.getString(Information, job1, null, filename);string job2 IniFunc.getString(Information, job2, null, filename);textBox1.Text job1;textBox2.Text job2;//Task(job1,job2);}定时任务Quartz.Net
安装
右键项目点击管理NuGet浏览中搜索quartz点击安装安装成功后在项目引用中会有quartz.dll
构建定时任务大概分为4步
构建scheduler任务管理器并开启创建job添加job构建触发器scheduler中添加job
完整代码
Job.cs
using Quartz;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp1
{public class Job : IJob{//public static readonly JobKey Key new JobKey(customer-process, group);//这里是定义job唯一keypublic async Task Execute(IJobExecutionContext context){var customerId context.MergedJobDataMap.GetString(CustomerId);//获取trggier传来的值同一个job通过trggier值不同而执行不同任务await Task.Run(() {//Random rd new Random();try{//MessageBox.Show($CustomerId{customerId});cmd(customerId);}catch (System.Exception e){MessageBox.Show(e.Message);}//try//{// var c Form1.form1.textBox1.Text;//获取界面文本值// cmd(c);//}//catch (System.Exception e)//{// MessageBox.Show(e.Message);//}});}//执行一个cmd命令private void cmd(String t){var p new Process();p.StartInfo.FileName cmd.exe;p.StartInfo.RedirectStandardInput true;p.StartInfo.UseShellExecute false;p.StartInfo.CreateNoWindow true;//不显示窗口p.Start();p.StandardInput.WriteLine(t);//p.StandardInput.WriteLine(exit);//执行退出可以不要p.StandardInput.Flush();}}
}IniFunc.cs
using System.Runtime.InteropServices;
using System.Text;
//https://blog.csdn.net/qq_38693757/article/details/121675847
namespace WindowsFormsApp1
{public static class IniFunc{/// summary/// 获取值/// /summary/// param namesection段落名/param/// param namekey键名/param/// param namedefval读取异常是的缺省值/param/// param nameretval键名所对应的的值没有找到返回空值/param/// param namesize返回值允许的大小/param/// param namefilepathini文件的完整路径/param/// returns/returns[DllImport(kernel32.dll)]private static extern int GetPrivateProfileString(string section,string key,string defval,StringBuilder retval,int size,string filepath);/// summary/// 写入/// /summary/// param namesection需要写入的段落名/param/// param namekey需要写入的键名/param/// param nameval写入值/param/// param namefilepathini文件的完整路径/param/// returns/returns[DllImport(kernel32.dll)]private static extern int WritePrivateProfileString(string section,string key,string val,string filepath);/// summary/// 获取数据/// /summary/// param namesection段落名/param/// param namekey键名/param/// param namedef没有找到时返回的默认值/param/// param namefilenameini文件完整路径/param/// returns/returnspublic static string getString(string section, string key, string def, string filename){StringBuilder sb new StringBuilder(1024);GetPrivateProfileString(section, key, def, sb, 1024, filename);return sb.ToString();}/// summary/// 写入数据/// /summary/// param namesection段落名/param/// param namekey键名/param/// param nameval写入值/param/// param namefilenameini文件完整路径/parampublic static void writeString(string section, string key, string val, string filename){WritePrivateProfileString(section, key, val, filename);}}
}Form1.cs
using Quartz;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading.Tasks;
using Quartz.Impl;namespace WindowsFormsApp1
{public partial class Form1 : Form{//定义静态类便于外部访问类似单例public static Form1 form1;public Form1(){InitializeComponent();form1 this;}private string filename null;//获取ini文件路径private void Form1_Load(object sender, EventArgs e){filename Application.StartupPath \\config.ini;//MessageBox.Show(filename);}//按钮点击后显示ini同时执行定时任务private void button1_Click(object sender, EventArgs e){//获取ini值string job1 IniFunc.getString(Information, job1, null, filename);string job2 IniFunc.getString(Information, job2, null, filename);//显示在界面上textBox1.Text job1;textBox2.Text job2;//执行定时任务Task(job1,job2);}//执行定时任务public async void Task(string cmd1,string cmd2) {//构建scheduler管理器StdSchedulerFactory factory new StdSchedulerFactory();IScheduler scheduler await factory.GetScheduler();await scheduler.Start();//3.7.2版本官网是先执行再加入任务意思是可以动态添加老博客都是后执行//定义任务WithIdentity(a)是任务的识别码Key,这个主要和trigger关联用可以是KV也可以是KIJobDetail job JobBuilder.CreateJob().WithIdentity(a).Build();//这里的模式是一个job对于若干个trigger所以需要先添加job然后trigger去关联这个jobawait scheduler.AddJob(job, replace: true, storeNonDurableWhileAwaitingScheduling: true);//定义trigger并关联job,并使用JobDataJobDataMap传值ITrigger trigger TriggerBuilder.Create().WithIdentity(trigger1)//.StartNow().ForJob(a).UsingJobData(CustomerId, cmd1).WithSimpleSchedule(x x.WithIntervalInSeconds(5)//5秒一次.RepeatForever()).Build();ITrigger trigger2 TriggerBuilder.Create().WithIdentity(trigger2)//.StartNow().ForJob(a).UsingJobData(CustomerId, cmd2).WithSimpleSchedule(x x.WithIntervalInSeconds(7)//7秒一次.RepeatForever()).Build();//添加触发器普通多任务是这样的await scheduler.ScheduleJob(job,trigger),但是这里是单任务多触发await scheduler.ScheduleJob(trigger);await scheduler.ScheduleJob(trigger2);MessageBox.Show(任务开始);}}
}config.ini
[Information]
job1D:\phpStudy\php\php-7.0.12-nts\php.exe D:\phpStudy\WWW\tp3\index.php Home/Index/queryAndWrite
job2D:\phpStudy\php\php-7.0.12-nts\php.exe D:\phpStudy\WWW\tp3\index.php Home/Index/queryAndWrite2简易定时任务工具雏形 官网的例子才是经典的去看看
quartz.nethttps://www.quartz-scheduler.net/documentation/best-practices.html#static-job-keyAPIhttps://quartznet.sourceforge.io/apidoc/3.0/html/参考https://blog.csdn.net/qq_46104221/article/details/130578236
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/913754.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!