技术标签: python ProcessStartInfo RedirectStandardOutput Process WaitForExit
上一篇文章中,我们已经将进行压缩等操作的代码写好,并在PyCharm中调试完毕。从代码中可以看出,功能的调用,是需要传入一定的外部参数的。这些参数,我们在C#中进行设置和存取。
为此,我们定义了一个类和一些静态变量,用于保存相关参数。
public class CompressionParameter
{
public static readonly string m_sRegistryNode = @"Software\Microsoft\Windows\CurrentVersion\Run"; //注册表开机启动项位置
public static readonly string m_sRegistryKey = "FissCompressVersions"; //注册表键名
public static readonly string m_sPythonInterpreter = "PythonInterpreter"; //Python解释器路径
public static readonly string m_sConnectionFile = "ConnectionFile"; //SDE连接文件路径
public static readonly string m_sConflictDefinition = "ConflictDefinition"; //冲突定义(0:按行,1:按列)
public static readonly string m_sConflictResolution = "ConflictResolution"; //冲突解决(0:默认版本优先,1:编辑版本优先)
public static readonly string m_sCompressionMode = "CompressionMode"; //定时压缩方式(0:普通,1:自动,1:彻底)
public static readonly string m_sHour = "Hour";//定时压缩时间(小时)
public static readonly string m_sMinute = "Minute";//定时压缩时间(分钟)
public static readonly string m_sRepeat = "Repeat";//定时压缩重复(0:一次,1:每天)
public static readonly string m_sAutoStart = "AutoStart";//是否自动启动(0:否,1:是)
public static readonly string m_sServerIP = "ServerIP"; //服务器IP地址
public static readonly string m_sServerPort = "ServerPort"; //服务器端口号
public static readonly string m_sNull = ""; //为空
public static readonly string m_Checked = "1"; //选中
public static readonly string m_Unchecked = "0"; //未选中
public static ConfigManager m_configManager = new ConfigManager(); //配置管理器
public static Dictionary<string, string> m_dictConfig = new Dictionary<string, string>() //默认配置项
{
{
m_sPythonInterpreter,m_sNull },
{
m_sConnectionFile,m_sNull},
{
m_sConflictDefinition,"0"},
{
m_sConflictResolution, "0"},
{
m_sCompressionMode,"1"},
{
m_sHour,"0"},
{
m_sMinute,"0" },
{
m_sRepeat,"1"},
{
m_sAutoStart,m_Unchecked},
{
m_sServerIP,"127.0.0.1"},
{
m_sServerPort,"9071"}
};
}
接下来,就是使用C#进行调用的时候了。
调用的关键,是使用 Python解释器 去执行相应的代码。
我们实现了一个核心的方法,去启动Python解释器,传入相关执行代码和参数:
private void ExecuteMission(string scriptFile, string param = "")
{
try
{
WriteLine("------------*------------>>>");
WriteLine("Mission Starts...");
var sParam = string.Format("{0} {1}", scriptFile, "\"" + CompressionParameter.m_dictConfig[CompressionParameter.m_sConnectionFile] + "\"");
if (!string.IsNullOrEmpty(param))
sParam += string.Format(" {0}", param);
ProcessStartInfo start = new ProcessStartInfo
{
WorkingDirectory = Application.StartupPath,
FileName = CompressionParameter.m_dictConfig[CompressionParameter.m_sPythonInterpreter],
Arguments = sParam,
UseShellExecute = false,
ErrorDialog = true,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true
};
WriteLine("Processing...");
Process process = Process.Start(start);
var reader = process.StandardOutput;
string sLine = reader.ReadLine();
WriteLine(sLine);
while (!reader.EndOfStream)
{
Application.DoEvents();
sLine = reader.ReadLine();
WriteLine(sLine);
}
process.WaitForExit();
process.Close();
reader.Close();
WriteLine("Mission Ends");
WriteLine("<<<------------*------------");
}
catch (Exception e)
{
MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
WriteLine(e.Message);
Log.Loging.Error(e.Message);
}
}
在上述方法中,我们截获了Python代码执行过程中的输出,将其读取到界面输出中,避免弹出Python解释器窗口,造成观感上的混乱。
WriteLine方法用于输出日志到界面上。为解决在界面控件线程以外修改控件内容的带来的冲突,需要判断是否有必要使用委托来输出日志。WriteLine方法如下:
public delegate void DelegateWriteLine(string sMessage); //输出到窗口日志的委托
private void WriteLine(string sMessage)
{
if (InvokeRequired)
{
var logDelegate = new DelegateWriteLine(WriteLine);
Invoke(logDelegate, sMessage);
}
else
{
Log.Loging.Info(sMessage);
if (textBoxOutput.GetLineFromCharIndex(textBoxOutput.Text.Length) > 100)
textBoxOutput.Clear();
textBoxOutput.AppendText(DateTime.Now.ToString("HH:mm:ss ") + sMessage + "\r\n");
textBoxOutput.ScrollToCaret();
}
}
public void CompressNormally()
{
ExecuteMission(@"PyScript\CompressNormally.py");
}
public void CompressAutomatically()
{
ExecuteMission(@"PyScript\CompressAutomatically.py",
string.Format("\"{0}\" \"{1}\"",
CompressionParameter.m_configManager.GetAppSetting(CompressionParameter.m_sConflictDefinition),
CompressionParameter.m_configManager.GetAppSetting(CompressionParameter.m_sConflictResolution)));
}
public void CompressCompletely()
{
ExecuteMission(@"PyScript\CompressCompletely.py",
string.Format("\"{0}\" \"{1}\"",
CompressionParameter.m_configManager.GetAppSetting(CompressionParameter.m_sConflictDefinition),
CompressionParameter.m_configManager.GetAppSetting(CompressionParameter.m_sConflictResolution)));
}
本人是某大学计算机的菜鸡,在数据结构上机课作业完成过程中,曾上网找了很多类似代码寻找思路,但是网上的代码太繁琐,而且都不是很好理解,因此在自己完成以后就写了这么一个博客,提供一种比较简单的程序代码,希望对那些还在数据结构中头痛的同学一点帮助。 问题描述:设计以校园导游程序,为来访的客人提供各种信息查询服务。设计要求:(1)设计自己所在学校的校园平面图(所含景点不少于十个),..._校园导航系统c语言
本文介绍了Oracle数据库到DM数据库的迁移方式,并进行了实操讲解。_oracle迁移达梦
【已解决】ERR_BLOCKED_BY_XSS_AUDITOR:Chrome 在此网页上检测到了异常代码:解决办法_chrome在此网页上检测到了异常代码
哈喽大家好,一段时间没有更新了非常抱歉。现在努力日更,给大家提供干货学习。今天我们的ps课程是制作特效。大家会觉得很难,但是并不是这样的。大家跟着小编的教程走,反复练习就很快学会啦。接下来我们就开始进入今天的学习吧!1.第一步我们先看下我们的精修对比图。对比2.首先我们拖入我们的原图到ps。原图3.下面是特效素材图片,大家可以直接保存图片练习即可。特效素材4.把特效图片拖入照片里。素材拖入照片5...._ps特效文字制作实例 pdf
java关键字 编辑Java关键字是电脑语言里事先定义的,有特别意义的标识符,有时又叫保留字,还有特别意义的变量。Java的关键字对Java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构等,关键字不能用作变量名、方法名、类名、包名和参数。中文名Java关键字关键字是电脑语言里事先定义的有特别意义的标识符有 时又叫保留字,还有特别意义..._java如何使我受益
随后在写吧
2019独角兽企业重金招聘Python工程师标准>>> ...
CountDownLatch用法---等待多个线程执行完才执行CountDownLatch用法---等待多个线程执行完才执行CountDownLatch用法---等待多个线程执行完才执行CountDownLatch用法---等待多个线程执行完才执行一.CountDownLatch用法CountDownLatch类位于java.util.concurrent包下,利用它可以实现类...
利用ArcGis进行地理处理之三(融合)一、应用目标在实际工作中,融合往往用于数据的整理。如,整合了10个采油厂的输油管线数据和10个采气厂的输气管线数据,总共有1000个图元,数据体量大,想要整合成所有的输油管线为一个图元,所有的输气管线为一个图元,就可以用融合。再如,整理了储量数据,这些数据也分别赋予了矿权、县域的属性,需要分别按照矿权、县域统计储量,当然也可以用excel来完成,但本着电脑工..._arcgis融合后还有没融合的
原标题:Python数据预处理:彻底理解标准化和归一化数据预处理数据中不同特征的量纲可能不一致,数值间的差别可能很大,不进行处理可能会影响到数据分析的结果,因此,需要对数据按照一定比例进行缩放,使之落在一个特定的区域,便于进行综合分析。常用的方法有两种:最大 - 最小规范化:对原始数据进行线性变换,将数据映射到[0,1]区间 Z-Score标准化:将原始数据映射到均值为0、标准差为1的分布上 为什...
实例import time, threading /# 新线程执行的代码: def loop(): print('thread %s is running...' % threading.current_thread().name) n = 0 while n < 5: n = n + 1 print..._python机器学习 多线程
此题和"小明上学"题干类似,不过我们需要模拟时间的流动来预测小明到达红绿灯时红绿灯的状态。这里我们增加了一个eclipse()函数来实现,实际上类似于状态机。思路详解把红绿灯的状态看成是<颜色k,剩余秒数t>,经过的时间是各个状态之间的转化边,那么我们可以描述一下红绿灯的有限状态机:初始状态为<红灯,30秒>,经过30秒后状态将变成<绿灯,g秒&..._小明发明发布2