调整窗口到1000*1000可以正常使用
//using System;
//using System.Diagnostics;
//using System.Runtime.InteropServices;
//using System.Threading.Tasks;
//using System.Windows.Forms;
//namespace CSharpWindow
//{
// public class Program : Form
// {
// [DllImport("user32.dll")]
// public static extern IntPtr GetConsoleWindow();
// public Program()
// {
// // 设置窗口大小和位置
// this.Text = "Camera Window"; // 窗口标题
// this.Size = new System.Drawing.Size(1000, 1000); // 窗口大小
// this.StartPosition = FormStartPosition.CenterScreen; // 窗口居中显示
// }
// [STAThread]
// static void Main()
// {
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// Program form = new Program();
// form.Load += (sender, e) =>
// {
// IntPtr hwnd = form.Handle;
// Console.WriteLine($"Window Handle: {hwnd}");
// ProcessStartInfo startInfo = new ProcessStartInfo
// {
// FileName = "D:\\anaconda3\\envs\\yolo\\python.exe",
// Arguments = $"C:\\Users\\011241\\Desktop\\multi_task_python\\python38_SDK\\TestWindowHandle\\TestWindowHandle.py {hwnd}",
// RedirectStandardOutput = true,
// RedirectStandardError = true,
// UseShellExecute = false,
// CreateNoWindow = true
// };
// Task.Run(() =>
// {
// using (Process process = Process.Start(startInfo))
// {
// string output = process.StandardOutput.ReadToEnd();
// string error = process.StandardError.ReadToEnd();
// process.WaitForExit();
// Console.WriteLine(output);
// if (!string.IsNullOrEmpty(error))
// {
// Console.WriteLine($"Error: {error}");
// }
// Console.WriteLine($"Exit Code: {process.ExitCode}");
// }
// });
// };
// Application.Run(form);
// }
// protected override void OnPaint(PaintEventArgs e)
// {
// base.OnPaint(e);
// // 这里可以添加绘制逻辑
// }
// }
//}
//创建窗口,并保持窗口显示(测试可用)
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CSharpWindow
{
public class Program : Form
{
[DllImport("user32.dll")]
public static extern IntPtr GetConsoleWindow();
// 静态变量以保持窗口句柄
private static IntPtr windowHandle;
public Program()
{
// 设置窗口大小和位置
this.Text = "Camera Window"; // 窗口标题
this.Size = new System.Drawing.Size(1000, 1000); // 窗口大小
this.StartPosition = FormStartPosition.CenterScreen; // 窗口居中显示
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Program form = new Program();
// 获取并保存窗口句柄
windowHandle = form.Handle;
Console.WriteLine($"Window Handle: {windowHandle}");
// 运行窗口
Application.Run(form);
// 在这里可以保持窗口句柄的有效性
Console.WriteLine("Press any key to exit...");
Console.ReadKey(); // 等待用户输入以保持程序运行
}
}
}
测试和python的进程间通信,已经成功实现通信
//using System;
//using System.Diagnostics;
//using System.Net.Sockets;
//using System.Text;
//class Program
//{
// static void Main(string[] args)
// {
// // 第一步:使用 System.Diagnostics.Process 启动 Python 服务器
// Process pythonServer = new Process();
// pythonServer.StartInfo.FileName = "python"; // 假设系统 PATH 中有 'python'
// pythonServer.StartInfo.Arguments = "C:\\Users\\011241\\Desktop\\yolov8multi-task\\TestCSharpConnect.py"; // Python 脚本路径
// pythonServer.StartInfo.CreateNoWindow = true; // 不创建新窗口
// pythonServer.StartInfo.UseShellExecute = false; // 不使用Shell执行
// pythonServer.Start();
// try
// {
// // 第二步:通过 TCP 连接 Python 服务器
// TcpClient client = new TcpClient("localhost", 9999); // 连接到本地Python服务器
// NetworkStream stream = client.GetStream();
// while (true)
// {
// Console.WriteLine("请输入命令(例如: do_task_1, do_task_2)或输入 'exit' 退出:");
// string message = Console.ReadLine();
// // 发送命令到 Python 服务器
// byte[] data = Encoding.UTF8.GetBytes(message);
// stream.Write(data, 0, data.Length);
// if (message == "exit")
// {
// break;
// }
// // 接收来自 Python 服务器的响应
// byte[] buffer = new byte[1024];
// int bytes = stream.Read(buffer, 0, buffer.Length);
// string response = Encoding.UTF8.GetString(buffer, 0, bytes);
// Console.WriteLine($"收到来自服务器的响应: {response}");
// }
// // 关闭连接
// stream.Close();
// client.Close();
// }
// catch (Exception e)
// {
// Console.WriteLine("错误: " + e.Message);
// }
// // 第三步:程序结束时,杀掉 Python 服务器进程
// pythonServer.Kill();
// }
//}
python进程间通信
最新推荐文章于 2025-05-27 20:07:06 发布