C#如何实现一个进程?当整点的时候,进行弹窗报时?

feyyee 2010-11-22 10:35:12
C#如何实现一个进程?当整点的时候,进行弹窗报时?

有一个Form程序,我想让程序启动时,任务管理器里有个A.exe进行,表示该Form程序正在运行。

我想实现:当A.exe运行时,同时开启B.exe执行(即在任务管理器中可以看到B.exe在运行)
,B.exe的作用是当整点时,弹出窗口进行报时。

我想到了用进程(不想用线程),不知道还有什么其他更好的办法?
...全文
954 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cobibiyang 2010-11-24
  • 打赏
  • 举报
回复
要用到windows message 分成两个窗体
1发送窗体


//发送方:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WinFormSendMsg
{
public partial class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
const int WM_COPYDATA = 0x004A;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(184, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(344, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 32);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 142);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.textBox1});
this.Name = "Form1";
this.Text = "发送方窗体";
this.ResumeLayout(false);
}
static void Main()
{
Application.Run(new Form1());
}
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
ref COPYDATASTRUCT lParam // second message parameter
);

[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string
lpWindowName);

private void button1_Click(object sender, System.EventArgs e)
{
int WINDOW_HANDLER = FindWindow(null, @"接收方窗体");
if (WINDOW_HANDLER == 0)
{
}
else
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(this.textBox1.Text);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = this.textBox1.Text;
cds.cbData = len + 1;
SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);

}
}
}
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}

}


2接收窗体 :

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsFormGetMsg
{
public partial class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.Container components = null;
const int WM_COPYDATA = 0x004A;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(176, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(160, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1});
this.Name = "Form1";
this.Text = "接收方窗体";
this.ResumeLayout(false);
}
static void Main()
{
Application.Run(new Form1());
}
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT mystr = new COPYDATASTRUCT();
Type mytype = mystr.GetType();
mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
this.textBox1.Text = mystr.lpData;
break;
default:
base.DefWndProc(ref m);
break;
}
}
}
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
}
feyyee 2010-11-24
  • 打赏
  • 举报
回复
现在知道该怎么实现了,用Windows SERVICE
xiehuanxie 2010-11-23
  • 打赏
  • 举报
回复
想法很好啊, 你只需要解决两个问题:
- A中判断什么时候是正点。
- 正点以后执行B。

这些都不难实现
feyyee 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 pcqpzq 的回复:]

引用 3 楼 brookmill 的回复:
没必要用进程或线程,用一个timer控件即可。

C# code
DateTime now = DateTime.Now; // 当前时间
DateTime zhengdian = new DateTime(now.Year, now.Month, now.Day, now.Hour + 1, 0, 0); // 下一个整点时间
……

……
[/Quote]

我举这个整点报时的例子是可以用timer,但是现实情况,比整点报时要复杂,所以得用进程。 整点报时只是举例而已。 最要A和B是不相干的。
feyyee 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 brookmill 的回复:]

没必要用进程或线程,用一个timer控件即可。
C# code
DateTime now = DateTime.Now; // 当前时间
DateTime zhengdian = new DateTime(now.Year, now.Month, now.Day, now.Hour + 1, 0, 0); // 下一个整点时间
……
[/Quote]

我举这个整点报时的例子是可以用timer,但是现实情况,比整点报时要复杂,所以得用进程。 整点报时只是举例而已。 最要A和B是不相干的。
jianshao810 2010-11-23
  • 打赏
  • 举报
回复
的确没必要用线程,你可以用另一个窗体+timer
guoyanhong1111 2010-11-23
  • 打赏
  • 举报
回复
时间控件Timer可以实现,它有一个Interval属性就是隔多久调用一次
pcqpzq 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 brookmill 的回复:]
没必要用进程或线程,用一个timer控件即可。

C# code
DateTime now = DateTime.Now; // 当前时间
DateTime zhengdian = new DateTime(now.Year, now.Month, now.Day, now.Hour + 1, 0, 0); // 下一个整点时间
……
[/Quote]
顶一个
直接在B里实现,完全和A不相干最好,当然A调用B是可以的。
DateTime t = DateTime.Now.AddHours(1);
DateTime zhengdian = new DateTime(t.Year, t.Month, t.Day, t.Hour+1, 0, 0);
gmkiy121 2010-11-23
  • 打赏
  • 举报
回复
private void Open(string pat)
{

System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
Info.FileName = pat;
Info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
System.Diagnostics.Process Proc;
try
{
Proc = System.Diagnostics.Process.Start(Info);
}
catch
{
}

}
gmkiy121 2010-11-23
  • 打赏
  • 举报
回复
A包含下面程序
string Pct = System.Environment.CurrentDirectory.ToString();
Open("" + Pct + "//B.exe");
//----------------
private void OpenRespond(string pat)
{

System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
Info.FileName = pat;
Info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
System.Diagnostics.Process Proc;
try
{
Proc = System.Diagnostics.Process.Start(Info);
}
catch
{
}

}

B就和他们说的那样.用timer
artwl_cn 2010-11-23
  • 打赏
  • 举报
回复
timer控件可以在间隔一定时间触发某个事件
NewMan98 2010-11-23
  • 打赏
  • 举报
回复
我知道了,搞两个程序,A计时,到时执行B,完事后B自动退出
cy_paul 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jianshao810 的回复:]
的确没必要用线程,你可以用另一个窗体+timer
[/Quote]

用timer 正确
brookmill 2010-11-23
  • 打赏
  • 举报
回复
3楼计算整点的方法显然不对,这样应该可以:
DateTime t = DateTime.Now.AddHours(1);
DateTime zhengdian = new DateTime(t.Year, t.Month, t.Day, t.Hour, 0, 0);
brookmill 2010-11-23
  • 打赏
  • 举报
回复
没必要用进程或线程,用一个timer控件即可。
            DateTime now = DateTime.Now; // 当前时间
DateTime zhengdian = new DateTime(now.Year, now.Month, now.Day, now.Hour + 1, 0, 0); // 下一个整点时间
TimeSpan ts = zhengdian - DateTime.Now; // 间隔
timer1.Interval = (int)ts.TotalMilliseconds; // 设置timer时间,单位是毫秒
timer1.Enabled = true; // 启动 timer

// timer到时
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Interval = 3600 * 1000; // 一小时,单位是毫秒
// ...... 弹窗报时

Dogfish 2010-11-23
  • 打赏
  • 举报
回复
有timer控件的。
内容概要:本文围绕“新型电力系统下多分布式电源接入配电网承载力评估方法”的研究,系统性地介绍了基于Matlab的仿真建模与代码实现方案,旨在评估高比例分布式电源(如光伏、风电等)接入背景下配电网的接纳能力。研究融合了智能优化算法(如蜣螂优化、灰狼优化、遗传算法)、多目标优化、鲁棒优化及双层优化模型,结合潮流计算、稳定性分析与故障仿真,构建了完整的承载力评估体系。文档不仅提供核心算法实现,还拓展至微电网调度、储能配置、电氢耦合系统、电动汽车协同等前沿方向,强调“复现+创新”相结合的科研路径,助力研究者快速掌握高水平论文复现技巧并激发原创思路。; 适合人群:具备电力系统、自动化或相关专业背景,熟悉Matlab/Simulink仿真环境,正在从事科研或工程应用的研究生及初级科研人员(工作1-3年);; 使用场景及目标:①复现高水平期刊中关于配电网承载力的优化模型;②开展高比例可再生能源接入下的配电网规划与运行研究;③学习并应用智能优化算法解决复杂电力系统问题;④获取完整科研资源包以加速课题进展与论文撰写; 阅读建议:建议读者关注公众号“荔枝科研社”获取网盘资源,下载全套代码与模型文件,按照文档结构循序渐进学习,重点理解算法设计逻辑与仿真建模细节,结合所提供的复现案例深化对优化模型与工程应用场景的理解,提升科研效率与创新能力。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧