C#通过进程调用外部应用的实现示例

2025-05-19 15:50

本文主要是介绍C#通过进程调用外部应用的实现示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《C#通过进程调用外部应用的实现示例》本文主要介绍了C#通过进程调用外部应用的实现示例,以WINFORM应用程序为例,在C#应用程序中调用PYTHON程序,具有一定的参考价值,感兴趣的可以了解一下...

以WINFORM应用程序为例,在C#应用程序中调用python程序(Matplotlib绘制图形程序),将调用PYTHON程序生成的窗口程序嵌入在WINFORM窗口中

窗口程序类

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Systemhttp://www.chinasem.cn.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace MyForm
{
    public partial class Form2 : DevComponents.DotNetBar.Office2007Form
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(Form2));
        [DllImport("shell32.dll")]
        private static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, Int32 nShowCmd);
        private string pyFileName = @"code.py";
        //需要在生成完文件后修改该参数值为生成的文件绝对路径
        private string pyParamsfilePath = @"pyParams.dat";
        private string args = "-u";
        private string dataType = "";
        public delegChina编程ate void UpdateUI();//委托用于更新UI
        Process p = null;//接收python程序进程,用于控制进程
        Thread startload;//线程用于matplotlib窗体处理
        IntPtr figure1;//图像句柄
        #region 绘图事件Windows API调用
        [DllImport("user32.dll")]
        public static extern IntPtr Findwindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewparent);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);

        const int GWL_STYLE = -16;
        //const int WS_CAPTION = 0x00C00000;
        const int WS_CAPTION = 0x00CC0000;
        const int WS_THICKFRAME = 0x00040000;
        const int WS_SYSMENU = 0X00080000;
        [DllImport("user32")]
        private static extern int GetWindowLong(System.IntPtr hwnd, int nIndex);

        [DllImport("user32")]
        private static extern int SetW编程indowLong(System.IntPtr hwnd, int index, int newLong);
        [DllImport("user32")]
        private static extern int InvalidateRect(System.IntPtr hwnd, object rect, bool bErase);
        /// <summary>最大化窗口,最小化窗口,正常大小窗口
        /// nCmdShow:0隐藏,3最大化,6最小化,5正常显示
        /// </summary>
        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        #endregion
        /// <summary>
        /// 绘制图形按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Draw3D(object sender, EventArgs e)
        {
            Create3DChart("Z");
        }
        /// <summary>
        /// 创建3D图形
        /// </summary>
        private void Create3DChart(string drawDataType)
        {
            if (processInfo.process != null)
            {//如果python程序进程不为null
                if (!processInfo.process.HasExited)
                {//如果当前进程没有被终止,未终止时值为false,终止时值为true
                    processInfo.process.Kill();
                }

            }
            dataType = drawDataType;
            //实例化线程,用来初次调用matlab,并把图像窗体放到winform
            startload = new Thread(new ThreadStart(RunPythonScript));
            //开始线程
            startload.Start();
        }
        /// <summary>
        /// 关闭窗口时结束进程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Close3DChart(object sender, FormClosedEventArgs e)
        {
            if (processInfo.process != null)
            {//如果python程序进程不为null
                if (!processInfo.process.HasExited)
                {//如果当前进程没有被终止,未终止时值为false,终止时值为true
                    processInfo.process.Kill();
                }

            }
        }
        /// <summary>
        /// 调用python脚本绘制3D图形
        /// </summary>
        public void RunPythonScript()
php        {

            log.Info($"开始绘图线程---{DateTime.Now.ToString()}");
            int count50ms = 0;
            //获取python的绝对路径(将文件放在c#程序的debug文件夹中可以这样操作)
            string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + pyFileName;
            //创建进程
            Process process = new Process();
            //没有配置环境变量的话,可以写python.exe的绝对路径,如果配置了,直接写"python.exe"即可
            process.StartInfo.FileName = SystemConfigClass.PythonPath;
            //设置python文件路径参数
            string sArguments = path;
            //设置执行方式参数
            sArguments += " " + dataType;
            sArguments += " " + Application.StartupPath + "\\" + pyParamsfilePath;
            sArguments += " " + args;
            log.Info($"绘图参数---{DateTime.Now.ToString()}:{sArguments}");
            process.StartInfo.Arguments = sArguments;//设置命令行自变量
            process.StartInfo.UseShellExecute = true;//设置直接从可执行文件创建进程
            //process.StartInfo.RedirectStandardOutput = true;//设置应用程序输出写入进程(程序输出可在c#控制台显示) 调试时使用
            //process.StartInfo.RedirectStandardInput = true;//设置应用程序输入读取进程(程序输入可读取进程输入)    调试时使用
            //process.StartInfo.RedirectStandardError = true;//设置应用程序错误信息写入进程(程序错误输出可在c#控制台显示)  调试时使用
            process.StartInfo.CreateNoWindow = true;//设置新窗口启动进程
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//设置窗口隐藏启动

            processInfo.process = process;


            process.Start();//进程开始

            //process.BeginOutputReadLine();//在Process.StandardOutput异步读取数据 调试时使用
            //process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);   //调试时使用

            figure1 = IntPtr.Zero;
            //循环查找figure1窗体
            while (figure1 == IntPtr.Zero)
            {
                //查找matplotlib的Figure 1窗体,安装PYQT组件后,查找类名Qt652QWindowIcon,未安装查找类名TkTopLevel
                figure1 = FindWindow("Qt652QWindowIcon", "Figure 1");

                //延时50ms
                Thread.Sleep(50);
                count50ms++;
                //20s超时设置
                if (count50ms >= 400)
                {
                    //label1.Text = "matplotlib资源加载时间过长!";
                    return;
                }
            }

            //跨线程,用委托方式执行
            UpdateUI update = delegate
            {

                ShowWindow(figure1, 0);
                //隐藏标签
                //label1.Visible = false;
                //设置matlab图像窗体的父窗体为panel
                SetParent(figure1, PlotPanel.Handle);
                //获取窗体原来的风格
                var style = GetWindowLong(figure1, GWL_STYLE);
                //设置新风格,去掉标题,不能通过边框改变尺寸
                SetWindowLong(figure1, GWL_STYLE, style & ~WS_CAPTION & ~WS_THICKFRAME);

                //移动到panel里合适的位置并重绘
                MoveWindow(figure1, 0, 0, PlotPanel.Width, PlotPanel.Height, true);
                //调用显示窗体函数,隐藏再显示相当于刷新一下窗体

                ShowWindow(figure1, 5);android
            };
            PlotPanel.Invoke(update);
            //再移动一次,防止显示错误
            Thread.Sleep(100);
            MoveWindow(figure1, 0, 0, PlotPanel.Width, PlotPanel.Height, true);


            //process.WaitForExit();//等待退出
        }
    }
}

进程信息类 

    /// <summary>
    /// 进程信息类
    /// </summary>
    public static class processInfo {
        public static Process process { get; set; } = null;
    }

系统设置类 

    /// <summary>
    /// 系统设置类
    /// </summary>
    public static class SystemConfigClass { 
        public static string PythonPath { get; set; } = @"D:\Python\python.exe";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataFilePath>Application.StartupPath + "\\SystemConfig.DAT"</param>
        public static void SetSystemConfig(string dataFilePath) {
            if (File.Exists(dataFilePath))
            { //如果文件存在 删除文件后保存文件
                string content = "";
                using (StreamReader dataFile = new StreamReader(dataFilePath))
                {
                    while ((content = dataFile.ReadLine()) != null)
                    {
                        content = content.ToString();
                        var tempArr = content.Split("^".ToCharArray()).ToList();
                        if (tempArr != null && tempArr.Count == 2) {
                            SystemConfigClass.PythonPath = tempArr[1];
                        }
                        else {
                            SystemConfigClass.PythonPath = @"D:\Python\python.exe";
                        }
                    }
                    dataFile.Close();
                    dataFile.Dispose();
                };
            }
            
        }
    }

到此这篇关于C#通过进程调用外部应用的实现示例的文章就介绍到这了,更多相关C# 进程调用外部应用内容请搜索编程China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持China编程(www.chinasem.cn)! 

这篇关于C#通过进程调用外部应用的实现示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1154668

相关文章

HTML5的input标签的`type`属性值详解和代码示例

《HTML5的input标签的`type`属性值详解和代码示例》HTML5的`input`标签提供了多种`type`属性值,用于创建不同类型的输入控件,满足用户输入的多样化需求,从文本输入、密码输入、... 目录一、引言二、文本类输入类型2.1 text2.2 password2.3 textarea(严格

MyBatis-Plus逻辑删除实现过程

《MyBatis-Plus逻辑删除实现过程》本文介绍了MyBatis-Plus如何实现逻辑删除功能,包括自动填充字段、配置与实现步骤、常见应用场景,并展示了如何使用remove方法进行逻辑删除,逻辑删... 目录1. 逻辑删除的必要性编程1.1 逻辑删除的定义1.2 逻辑删php除的优点1.3 适用场景2.

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配

JAVA项目swing转javafx语法规则以及示例代码

《JAVA项目swing转javafx语法规则以及示例代码》:本文主要介绍JAVA项目swing转javafx语法规则以及示例代码的相关资料,文中详细讲解了主类继承、窗口创建、布局管理、控件替换、... 目录最常用的“一行换一行”速查表(直接全局替换)实际转换示例(JFramejs → JavaFX)迁移建

Python实现快速扫描目标主机的开放端口和服务

《Python实现快速扫描目标主机的开放端口和服务》这篇文章主要为大家详细介绍了如何使用Python编写一个功能强大的端口扫描器脚本,实现快速扫描目标主机的开放端口和服务,感兴趣的小伙伴可以了解下... 目录功能介绍场景应用1. 网络安全审计2. 系统管理维护3. 网络故障排查4. 合规性检查报错处理1.

Python轻松实现Word到Markdown的转换

《Python轻松实现Word到Markdown的转换》在文档管理、内容发布等场景中,将Word转换为Markdown格式是常见需求,本文将介绍如何使用FreeSpire.DocforPython实现... 目录一、工具简介二、核心转换实现1. 基础单文件转换2. 批量转换Word文件三、工具特性分析优点局