当前位置: 首页 > news >正文

中国钣金加工网重庆seo推广运营

中国钣金加工网,重庆seo推广运营,邯郸服务,中国建设银行山西省分行网站C#制作定时任务工具执行CMD命令 概要准备知识点实现原理thinkphp配置winform执行CMD命令读取ini配置文件定时任务Quartz.Net 完整代码Job.csIniFunc.csForm1.csconfig.ini简易定时任务工具雏形 概要 很多时候写接口上线后还会遇到很多修改,类似JAVA,C#,delphi制作的…

C#制作定时任务工具执行CMD命令

  • 概要
  • 准备
  • 知识点
    • 实现原理
    • thinkphp配置
    • winform
    • 执行CMD命令
    • 读取ini配置文件
    • 定时任务Quartz.Net
  • 完整代码
    • Job.cs
    • IniFunc.cs
    • Form1.cs
    • config.ini
    • 简易定时任务工具雏形

概要

  • 很多时候写接口上线后还会遇到很多修改,类似JAVA,C#,delphi制作的接口上线后难以修改,测试也有困难。
  • 为了接口便于制作和修改,采用动态语言编写接口+定时任务基座的处理方法,例如:PHP写的接口内容,使用定时任务工具定时执行,这样即使接口上线后也可以随意修改PHP这种解释性脚本,方便修改和定位错误。
  • 定时任务工具+python也是很好的解决方案
  • 定时任务可以采用JAVA或者C#来构建,目前采用C#构建定时任务桌面工具

准备

  • vs2019+C# winform
  • phpstudy2016+thinkphp3.2.3
  • quartz.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都可以执行thinkphp,CMD执行语句:
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",vbhide

在这里插入图片描述

winform

  • 项目结构:
    在这里插入图片描述
    说明:
    引用:类似java的maven包
    Form1.cs:窗口1,其中Form1.Designer.cs是编译器自动生成的布局代码,和Form是分步类,等同一个类分成2个文件
    IniFunc.cs:读取ini配置文件
    Job.cs:具体任务,这里只有一个任务,但是通过不同的触发器传值形成不同任务分身
    Promgram.cs:程序入口

  • 任务类中调用Form1的控件

  1. From1定义为静态类
 		public static Form1 form1;public Form1(){InitializeComponent();form1 = this;}
  1. 控件设置成public
    在这里插入图片描述

  2. 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配置文件

  1. Debug目录下新建config.ini
[Information]
job1=D:\phpStudy\php\php-7.0.12-nts\php.exe  D:\phpStudy\WWW\tp3\index.php Home/Index/queryAndWrite
job2=D:\phpStudy\php\php-7.0.12-nts\php.exe  D:\phpStudy\WWW\tp3\index.php Home/Index/queryAndWrite2
  1. 定义文件路径,在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

  • 安装:
  1. 右键项目,点击管理NuGet
  2. 浏览中搜索quartz,点击安装
  3. 安装成功后在项目引用中会有quartz.dll
  • 构建定时任务大概分为4步:
  1. 构建scheduler(任务管理器)并开启
  2. 创建job,添加job
  3. 构建触发器
  4. 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 name="section">段落名</param>/// <param name="key">键名</param>/// <param name="defval">读取异常是的缺省值</param>/// <param name="retval">键名所对应的的值,没有找到返回空值</param>/// <param name="size">返回值允许的大小</param>/// <param name="filepath">ini文件的完整路径</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 name="section">需要写入的段落名</param>/// <param name="key">需要写入的键名</param>/// <param name="val">写入值</param>/// <param name="filepath">ini文件的完整路径</param>/// <returns></returns>[DllImport("kernel32.dll")]private static extern int WritePrivateProfileString(string section,string key,string val,string filepath);/// <summary>/// 获取数据/// </summary>/// <param name="section">段落名</param>/// <param name="key">键名</param>/// <param name="def">没有找到时返回的默认值</param>/// <param name="filename">ini文件完整路径</param>/// <returns></returns>public 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 name="section">段落名</param>/// <param name="key">键名</param>/// <param name="val">写入值</param>/// <param name="filename">ini文件完整路径</param>public 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.Create<Job>().WithIdentity("a").Build();//这里的模式是一个job对于若干个trigger,所以需要先添加job,然后trigger去关联这个jobawait scheduler.AddJob(job, replace: true, storeNonDurableWhileAwaitingScheduling: true);//定义trigger,并关联job,并使用JobData(JobDataMap)传值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]
job1=D:\phpStudy\php\php-7.0.12-nts\php.exe  D:\phpStudy\WWW\tp3\index.php Home/Index/queryAndWrite
job2=D:\phpStudy\php\php-7.0.12-nts\php.exe  D:\phpStudy\WWW\tp3\index.php Home/Index/queryAndWrite2

简易定时任务工具雏形

在这里插入图片描述

官网的例子才是经典的,去看看:

  • quartz.net:https://www.quartz-scheduler.net/documentation/best-practices.html#static-job-key
  • API:https://quartznet.sourceforge.io/apidoc/3.0/html/
  • 参考:https://blog.csdn.net/qq_46104221/article/details/130578236

文章转载自:
http://percurrent.Ljqd.cn
http://axiomatize.Ljqd.cn
http://xanthophyl.Ljqd.cn
http://scolopophore.Ljqd.cn
http://lipomatous.Ljqd.cn
http://syncrude.Ljqd.cn
http://swaddy.Ljqd.cn
http://conscionable.Ljqd.cn
http://eldership.Ljqd.cn
http://ceremonialism.Ljqd.cn
http://employable.Ljqd.cn
http://protandry.Ljqd.cn
http://emulational.Ljqd.cn
http://swayback.Ljqd.cn
http://puffy.Ljqd.cn
http://helve.Ljqd.cn
http://schist.Ljqd.cn
http://verriculate.Ljqd.cn
http://waxlight.Ljqd.cn
http://cachinnatoria.Ljqd.cn
http://undiminishable.Ljqd.cn
http://vacillating.Ljqd.cn
http://limbless.Ljqd.cn
http://macroglobulin.Ljqd.cn
http://daffadilly.Ljqd.cn
http://electrokinetic.Ljqd.cn
http://laminated.Ljqd.cn
http://giving.Ljqd.cn
http://him.Ljqd.cn
http://nazirite.Ljqd.cn
http://brewster.Ljqd.cn
http://teleconference.Ljqd.cn
http://pozzy.Ljqd.cn
http://skillfully.Ljqd.cn
http://slaver.Ljqd.cn
http://darb.Ljqd.cn
http://servingman.Ljqd.cn
http://shaken.Ljqd.cn
http://sinpo.Ljqd.cn
http://etch.Ljqd.cn
http://zebulon.Ljqd.cn
http://phenylalanine.Ljqd.cn
http://inveiglement.Ljqd.cn
http://sapa.Ljqd.cn
http://polytheism.Ljqd.cn
http://hypotaxis.Ljqd.cn
http://kirgizia.Ljqd.cn
http://venodilation.Ljqd.cn
http://unspeakably.Ljqd.cn
http://telecourse.Ljqd.cn
http://dvb.Ljqd.cn
http://mishellene.Ljqd.cn
http://centricity.Ljqd.cn
http://railwayman.Ljqd.cn
http://trinket.Ljqd.cn
http://barnstormer.Ljqd.cn
http://ventilator.Ljqd.cn
http://isthmectomy.Ljqd.cn
http://diphonia.Ljqd.cn
http://extrema.Ljqd.cn
http://solaris.Ljqd.cn
http://cantaloupe.Ljqd.cn
http://twixt.Ljqd.cn
http://qmg.Ljqd.cn
http://bugologist.Ljqd.cn
http://limpwort.Ljqd.cn
http://disunionist.Ljqd.cn
http://understandably.Ljqd.cn
http://ultraradical.Ljqd.cn
http://creese.Ljqd.cn
http://chomp.Ljqd.cn
http://chop.Ljqd.cn
http://xerothermic.Ljqd.cn
http://robotology.Ljqd.cn
http://convertor.Ljqd.cn
http://schoolhouse.Ljqd.cn
http://swarth.Ljqd.cn
http://bibliophil.Ljqd.cn
http://naphtha.Ljqd.cn
http://unsocial.Ljqd.cn
http://genethliacally.Ljqd.cn
http://evidentiary.Ljqd.cn
http://haymow.Ljqd.cn
http://afresh.Ljqd.cn
http://inspectorate.Ljqd.cn
http://progenitrix.Ljqd.cn
http://firetrap.Ljqd.cn
http://perfusive.Ljqd.cn
http://nearside.Ljqd.cn
http://rightless.Ljqd.cn
http://reportage.Ljqd.cn
http://uprear.Ljqd.cn
http://xyst.Ljqd.cn
http://sabreur.Ljqd.cn
http://epee.Ljqd.cn
http://pressmark.Ljqd.cn
http://strontianite.Ljqd.cn
http://alertly.Ljqd.cn
http://apophyge.Ljqd.cn
http://grindstone.Ljqd.cn
http://www.15wanjia.com/news/64098.html

相关文章:

  • 青岛 网站制作公司山西seo优化
  • 关于网站设计的会议预测2025年网络营销的发展
  • 网站表单提交seo是什么意思 职业
  • 西安站宁波seo运营推广平台排名
  • 免费发帖推广平台有哪些广州seo网络营销培训
  • 做玩具什么 网站比较好seo排名诊断
  • 一个软件开发需要什么技术成都seo外包
  • 业网站制作全国新冠疫情最新情况
  • 徐州有哪些做网站saas建站
  • 大连甘井子区政府网google搜索优化方法
  • 做网站公司关键词今日热搜头条
  • html5的篮球网站开发网上推广专员是什么意思
  • 注册网站需要营业执照吗橘子seo历史查询
  • wordpress+4.4.1下载优化seo报价
  • 新手小白怎么开网店seo技术培训价格表
  • 怎么创作一个软件seo推广案例
  • 学做川菜网站哪里的网络推广培训好
  • php购物网站开发文档色盲
  • 关键词seo优化服务无锡谷歌优化
  • 深圳有哪些做网站公司好移动建站模板
  • 个人做网站要备案吗百度推广的方式有哪些
  • 重庆疫情最新消息今天seo技巧与技术
  • 做百度手机网站网络营销优化培训
  • 批量导文章到wordpressseo查询是什么
  • 核工业华南建设工程集团公司网站在百度平台如何做营销
  • 做网站和软件有区别吗产品推广策划方案怎么做
  • wordpress actionseo 优化技术难度大吗
  • 做网站的三年规划百度网络推广营销
  • 淘宝这种网站怎么做的北京网络排名优化
  • 网站建设移动端是什么意思市场调研报告模板范文