Control Study -> 用Process调用其它应用程序

这是一个C# Windows应用程序,展示了如何使用System.Diagnostics.Process类来启动另一个应用程序,例如在这里是'skate.exe'。当用户点击按钮时,程序会创建并启动指定的进程。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

namespace WindowsApplication3
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.ComponentModel.IContainer components;

  private System.Diagnostics.Process process;
  private System.Windows.Forms.Button button1;
  //Form2 a;
  //Graphics g=pictureBox1.CreateGraphics();
  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(136, 144);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler(this.button1_Click_1);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(344, 302);
   this.Controls.Add(this.button1);
   this.HelpButton = true;
   this.Name = "Form1";
   this.Text = "Form1";
   this.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.Form1_HelpRequested);
   this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  } 

  private void Form1_HelpRequested(object sender, System.Windows.Forms.HelpEventArgs hlpevent)
  {
   
  }

  private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {   

  }

  private void button1_Click_1(object sender, System.EventArgs e)
  {
   process=new Process();   
   process.StartInfo.FileName="skate.exe";
   //process.StartInfo.FileName=@"F:/c#/拼图游戏2/bin/Release/拼图游戏2.exe";
   this.process.Start();
  }

  private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  { 
   
  }
 }
}

#include "bsp_flash_log.h" #include "Reg_RW.h" #include "LDChip.h" #include "bsp_beep.h" #include <stdint.h> #include <stdbool.h> #include <stdio.h> #include <string.h> #include "bsp_ili9341_lcd.h" #include "bsp_color_led.h" #include "usart.h" #include "delay.h" #include "bsp_spi_flash.h" #include "bsp_usart.h" // 包含 USART_Config 声明 #include "remind.h" #include "bsp_SysTick.h" #include "stm32f10x_it.h" // 包含 SysTick_Handler 声明 #define MAX_TASKS 10 #define CMD_BUFFER_SIZE 64 // 全局变量声明 u8 nAsrStatus = LD_ASR_NONE; // 语音识别状态 u8 nAsrRes = 0; // 语音识别结果 u8 flag = 0; // 通用标志位 volatile uint32_t ms_count = 0; // 毫秒计数器 - 添加volatile关键字 uint8_t needStartASR = 1; bool lcdState_changed = true ; // 外部变量 //extern uint32_t log_count; // 日志条目计数 //extern uint32_t log_bytes_written; // 日志已写字节数 // 天气信息结构体 typedef struct { char condition[16]; // 天气状况 int8_t temperature; // 温度 } WeatherInfo; // LCD显示状态枚举 typedef enum { LCD_SHOW_MENU, // 显示主菜单 LCD_SHOW_TASK, // 显示任务列表 LCD_SHOW_WEATHER // 显示天气信息 } LCD_State; // 全局数据结构 Task taskList[MAX_TASKS]; // 任务列表 uint8_t taskCount = 0; // 任务计数 WeatherInfo todayWeather; // 当天天气信息 LCD_State lcdState = LCD_SHOW_MENU; // 当前LCD状态 Time global_time = {17, 27}; // 全局时间(初始值) char cmd_buffer[CMD_BUFFER_SIZE]; // 串口命令缓冲区 uint16_t cmd_index = 0; // 命令缓冲区索引 /*-------------------------- 函数声明 --------------------------*/ // 串口通信 void UART_SendData(uint8_t* data, uint16_t len); void LOG_ProcessCommand(char* cmd); void LOG_EnhancedRxHandler(uint8_t* data, uint16_t len); // 任务管理 bool is_overlap(Time a_start, Time a_end, Time b_start, Time b_end); bool add_task(Task *newTask); void check_and_remind(Time now); // LCD显示 void LCD_ShowTask(Task* pTask, uint8_t y); void LCD_ShowWeather(WeatherInfo* pWeather, uint8_t y); // 语音识别 void User_Modification(u8 dat); /*-------------------------- 函数实现 --------------------------*/ // 串口发送数据 void UART_SendData(uint8_t* data, uint16_t len) { for(uint16_t i = 0; i < len; i++) { USART_SendData(USART1, data[i]); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } } // 处理串口命令 void LOG_ProcessCommand(char* cmd) { if(strcmp(cmd, "READLOG") == 0) { printf("Sending log via UART...\r\n"); LOG_SendToSerial(UART_SendData); printf("Log transfer complete.\r\n"); } else if(strcmp(cmd, "CLEARLOG") == 0) { LOG_Clear(); printf("Log cleared.\r\n"); } else if(strcmp(cmd, "STATUS") == 0) { //printf("Log entries: %lu, Size: %lu bytes\r\n", log_count, log_bytes_written); } else if(strcmp(cmd, "HELP") == 0) { printf("Available commands:\r\n"); printf("READLOG - Send log via UART\r\n"); printf("CLEARLOG - Clear log file\r\n"); printf("STATUS - Show log status\r\n"); printf("TASKS - List current tasks\r\n"); printf("WEATHER - Show weather info\r\n"); printf("ADDTASK - Add new task\r\n"); printf("DELTASK - Delete task\r\n"); } else if(strcmp(cmd, "TASKS") == 0) { printf("Current tasks:\r\n"); for(uint8_t i = 0; i < taskCount; i++) { printf("%d. %02d:%02d-%02d:%02d %s (P%d)\r\n", i+1, taskList[i].start.hour, taskList[i].start.min, taskList[i].end.hour, taskList[i].end.min, taskList[i].name, taskList[i].priority); } } else if (strncmp(cmd, "ADDTASK ", 8) == 0) { int h1, m1, h2, m2, pri; char name[16]; if (sscanf(cmd + 8, "%d %d %d %d %d %15s", &h1, &m1, &h2, &m2, &pri, name) == 6) { Task newTask = { .start = {.hour = (uint8_t)h1, .min = (uint8_t)m1}, .end = {.hour = (uint8_t)h2, .min = (uint8_t)m2}, .priority = (uint8_t)pri, .reminderSet = false }; strncpy(newTask.name, name, sizeof(newTask.name)); if (add_task(&newTask)) { printf("Task added: %s\r\n", name); Remind_SetTaskList(taskList, taskCount); // 更新提醒模块 } else { printf("Add task failed: overlap or full\r\n"); } } else { printf("Invalid ADDTASK format\r\n"); } } else if (strncmp(cmd, "DELTASK ", 8) == 0) { int index; if (sscanf(cmd + 8, "%d", &index) == 1) { if (index >= 1 && index <= taskCount) { printf("Deleting task %d: %s\r\n", index, taskList[index-1].name); // 移动后续任务 for (uint8_t i = index-1; i < taskCount-1; i++) { taskList[i] = taskList[i+1]; } taskCount--; Remind_SetTaskList(taskList, taskCount); // 更新提醒模块 } else { printf("Invalid task index\r\n"); } } else { printf("Invalid DELTASK format\r\n"); } } else if(strcmp(cmd, "WEATHER") == 0) { printf("Weather: %s, %dC\r\n", todayWeather.condition, todayWeather.temperature); } else { printf("Unknown command: %s\r\n", cmd); printf("Type 'HELP' for available commands.\r\n"); } } // 串口接收处理 void LOG_EnhancedRxHandler(uint8_t* data, uint16_t len) { for(uint16_t i = 0; i < len; i++) { // 处理回车/换行(命令结束) if(data[i] == '\r' || data[i] == '\n') { if(cmd_index > 0) { cmd_buffer[cmd_index] = '\0'; LOG_ProcessCommand(cmd_buffer); cmd_index = 0; } continue; } // 处理退格/删除 if(data[i] == 0x08 || data[i] == 0x7F) { if(cmd_index > 0) { cmd_index--; } continue; } // 存储有效字符 if(cmd_index < (CMD_BUFFER_SIZE - 1) && data[i] >= 32 && data[i] <= 126) { cmd_buffer[cmd_index++] = data[i]; } } } // 检查时间重叠 bool is_overlap(Time a_start, Time a_end, Time b_start, Time b_end) { if ((a_end.hour < b_start.hour) || (a_end.hour == b_start.hour && a_end.min <= b_start.min) || (a_start.hour > b_end.hour) || (a_start.hour == b_end.hour && a_start.min >= b_end.min)) return false; return true; } // 添加任务 bool add_task(Task *newTask) { // 检查时间重叠 for (uint8_t i = 0; i < taskCount; i++) { if (is_overlap(newTask->start, newTask->end, taskList[i].start, taskList[i].end)) { return false; } } // 添加到任务列表 if (taskCount < MAX_TASKS) { taskList[taskCount++] = *newTask; return true; } return false; } // 显示任务信息 void LCD_ShowTask(Task* pTask, uint8_t y) { char buf[32]; sprintf(buf, "%02d:%02d~%02d:%02d %-10s P%d", pTask->start.hour, pTask->start.min, pTask->end.hour, pTask->end.min, pTask->name, pTask->priority); ILI9341_DispString_EN(0, y, buf); } // 显示天气信息 void LCD_ShowWeather(WeatherInfo* pWeather, uint8_t y) { char buf[32]; sprintf(buf, "Weather:%-7s %2dC", pWeather->condition, pWeather->temperature); ILI9341_DispString_EN(0, y, buf); } // 语音识别处理 void User_Modification(u8 dat) { if(dat == CODE_1KL1) { // "任务"命令 lcdState = LCD_SHOW_TASK; lcdState_changed = true; // 标记状态改变 printf("CMD: Show tasks\r\n"); } else if(dat == CODE_1KL2) { // "天气"命令 lcdState = LCD_SHOW_WEATHER; lcdState_changed = true; // 标记状态改变 printf("CMD: Show weather\r\n"); } else { // 未知命令 lcdState = LCD_SHOW_MENU; lcdState_changed = true; // 标记状态改变 printf("CMD: Unknown voice command\r\n"); } } /*-------------------------- 主函数 --------------------------*/ int main(void) { // 系统初始化(最先执行) SystemInit(); // 延时初始化(在其他模块之前) delay_init(); // 中断优先级分组 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // SysTick初始化(1ms中断)- 重要! if (SysTick_Config(SystemCoreClock / 1000)) { // 配置为1ms中断 while (1); // 配置失败时死循环 } // 硬件初始化 COLOR_TIMx_LED_Init(); // RGB灯初始化 BEEP_GPIO_Config(); // 蜂鸣器初始化 // 串口和日志初始化 USART_Config(); // 串口初始化 LOG_Init(); // 日志系统初始化 // LCD初始化 ILI9341_Init(); // LCD初始化 ILI9341_Clear(0, 0, LCD_X_LENGTH, LCD_Y_LENGTH); // 清屏 // 语音识别初始化 LD3320_Init(); EXTIX_Init(); LD_Reset(); nAsrStatus = LD_ASR_NONE; // 初始状态:没有在作ASR SCS = 0; // 启用串口中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NVIC_EnableIRQ(USART1_IRQn); // 初始化天气信息 strcpy(todayWeather.condition, "Sunny"); todayWeather.temperature = 25; // 创建示例任务 Task task1 = {{17, 38}, {17, 39}, 1, "Meeting", false}; Task task2 = {{17, 39}, {17, 40}, 2, "Study", false}; Task task3 = {{17, 40}, {17, 41}, 3, "Exercise", false}; add_task(&task1); add_task(&task2); add_task(&task3); Remind_SetTaskList(taskList, taskCount); // 系统启动信息 printf("System started!\r\n"); printf("Type 'HELP' for command list\r\n"); // 非阻塞计时变量 uint32_t last_time_update = ms_count; uint32_t last_remind_check = ms_count; uint32_t last_lcd_refresh = ms_count; uint32_t last_debug = ms_count; // 调试用 // 主循环 while (1) { // ===== 1. 唯一的ASR流程控制逻辑,放在主循环顶部 ====== switch(nAsrStatus) { case LD_ASR_NONE: if (needStartASR) { nAsrStatus = LD_ASR_RUNING; if (RunASR() == 0) { nAsrStatus = LD_ASR_ERROR; } needStartASR = 0; } break; case LD_ASR_FOUNDOK: nAsrRes = LD_GetResult(); printf("Voice recognition result: %d\r\n", nAsrRes); // 调试输出 User_Modification(nAsrRes); nAsrStatus = LD_ASR_NONE; needStartASR = 1; break; case LD_ASR_ERROR: LD_Reset(); nAsrStatus = LD_ASR_NONE; printf("Voice module reset due to error\r\n"); needStartASR = 1; break; default: // 其他状态保持 break; } // 获取当前时间 uint32_t current_ms = ms_count; // 调试输出 - 每秒输出一次ms_count值 if(current_ms - last_debug >= 1000) { last_debug = current_ms; printf("Debug - ms_count: %lu, ASR status: %d\r\n", current_ms, nAsrStatus); } // 2. 非阻塞时间更新(每分钟更新) if(current_ms - last_time_update >= 60000) { // 1分钟 last_time_update = current_ms; global_time.min++; if(global_time.min >= 60) { global_time.min = 0; global_time.hour++; if(global_time.hour >= 24) global_time.hour = 0; } // 每分钟记录一次时间 printf("Current time: %02d:%02d\r\n", global_time.hour, global_time.min); } // 3. 任务提醒检查(每10秒检查) if(current_ms - last_remind_check >= 10000) { // 10秒 last_remind_check = current_ms; Remind_CheckAndDo(global_time); } // 4. LCD显示更新(每秒刷新或状态变化时刷新) if(current_ms - last_lcd_refresh >= 1000 || lcdState_changed) { last_lcd_refresh = current_ms; if(lcdState_changed) { lcdState_changed = false; ILI9341_Clear(0, 0, LCD_X_LENGTH, LCD_Y_LENGTH); // 清屏 } // 显示当前时间 char time_buf[16]; sprintf(time_buf, "Time: %02d:%02d", global_time.hour, global_time.min); ILI9341_DispString_EN(0, 0, time_buf); switch(lcdState) { case LCD_SHOW_MENU: ILI9341_DispString_EN(0, 20, "Voice Control Ready"); ILI9341_DispString_EN(0, 40, "Say 'Task' or 'Weather'"); break; case LCD_SHOW_TASK: for(uint8_t i = 0; i < taskCount; i++) { LCD_ShowTask(&taskList[i], 20 + i*20); } break; case LCD_SHOW_WEATHER: LCD_ShowWeather(&todayWeather, 20); break; } } // 短延时 - 注意函数名 Delay_ms(10); // 改为10ms,减少CPU占用 } } // 添加文件结束符,确保最后一行有换行 为什么输出后是白屏
最新发布
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值