|
呵呵,这段时间忙得脑壳都大了.很久没有上博客来看看了.做了不少东西还是有些收获.呵呵.. 前不久做了个课程设计,做得有点失败,主要是因为做了一部分之后才发现有个问题以前一直都想错了,后来因为时间很赶所以就只有转个思路做成了别的东西.就是把子进程获得处理机的时间把它作在一张图上 .然后再通过主进程对子进程的调度让子进程按规律获得处理机,呵呵 这个思路确实烂,没办法 还有就是写这个程序确实很赶,只花了大半个晚上,所以很多地方都写得很乱. 为了省点时间,我就用C#写的 下面大率的说一下程序的主要部分 主要是用了window 窗口消息来控制子程序以及需要进程间互斥访问资源的控制
详细的有兴趣请参看附件 (需要一张空白图片) 主进程程序:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using C_List; using System.Threading; namespace WinFormSendMsg { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; private Button button2; private ListBox listBox1; private Button button3; const int WM_COPYDATA = 0x004A; public List list; private Label label1; public System.Diagnostics.Process my_process; private PictureBox pictureBox1; private Panel panel1; private Button button4; private TextBox textBox2; private ListBox listBox2; public System.Collections.ArrayList arraylist; public Form1() { InitializeComponent(); long xx =System.DateTime.Now.Second*60000 + System.DateTime.Now.Millisecond; this.textBox1.Text=xx.ToString(); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } private void InitializeComponent() { //窗体代码 略 } 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); [DllImport("user32.dll")] public static extern void SetForegroundWindow(IntPtr hwnd); private void button1_Click(object sender, System.EventArgs e) { int WINDOW_HANDLER; int id; for (id = 1; id <= arraylist.Count; id++) { WINDOW_HANDLER = FindWindow(null, @"child" + arraylist[id-1].ToString()); if (WINDOW_HANDLER == 0) { } else { byte[] sarr = System.Text.Encoding.Default.GetBytes(this.textBox1.Text+"#"+id.ToString()); int len = sarr.Length; COPYDATASTRUCT cds; cds.dwData = (IntPtr)100; cds.lpData = this.textBox1.Text+"#"+id.ToString(); //MessageBox.Show(cds.lpData.ToString()+"#"+id.ToString()); cds.cbData = len + 1; SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds); } } } public void bindpic() { // this.textBox2.Text += this.textBox2.Text + "1"; Image image = Image.FromFile("C:pic.bmp"); System.Drawing.Bitmap bmp = new Bitmap(image); //bmp.SetPixel(x,y,System.Drawing.Color.Red); image.Dispose(); //System.Drawing.Point point=new Point(0,0); //System.Drawing.Point point1=new Point(100,100); //Manage_pic.Draw.DrawLineInPicture(bmp, point, point1, System.Drawing.Color.Red, 2, System.Drawing.Drawing2D.DashStyle.Dash); this.pictureBox1.Image = bmp; } private void Form1_Load(object sender, EventArgs e) { // bindpic(); System.Timers.Timer tt = new System.Timers.Timer(10000); tt.Enabled = true; tt.Elapsed += new System.Timers.ElapsedEventHandler(tt_Elapsed); } private void button2_Click(object sender, EventArgs e) { list = new List(); System.Diagnostics.Process my_process; my_process = new System.Diagnostics.Process(); arraylist = new ArrayList(); for (int xx = 1; xx < 5; xx++) { my_process.StartInfo.FileName = "C:\\win_recevied.exe"; my_process.StartInfo.CreateNoWindow = false; my_process.Start(); list.InsertAtBack(my_process.Id, my_process.PriorityClass.ToString()); list.Print(); this.listBox1.Items.Add(my_process.Id); arraylist.Add(my_process.Id); } //使用移出方法 } private void button3_Click(object sender, EventArgs e) { object removedObject; try { while (true) { removedObject = list.RemoveFromFront(); this.listBox1.Items.Remove(removedObject); my_process = System.Diagnostics.Process.GetProcessById(System.Convert.ToInt32(removedObject)); my_process.Kill(); } } catch (EmptyListException emptyListException) { Console.Error.WriteLine("\n" + emptyListException); } bindpic(); } void tt_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { // bindpic(); int xx,r; int WINDOW_HANDLER; System.Random rnd = new Random(); r=rnd.Next(4)+1; WINDOW_HANDLER = FindWindow(null, @"child" + arraylist[r - 1].ToString()); 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); //MessageBox.Show("hello--"+arraylist[r-1].ToString()); } private void button4_Click(object sender, EventArgs e) { int p_id; p_id = System.Convert.ToInt32(this.textBox2.Text); my_process = System.Diagnostics.Process.GetProcessById(p_id); SetForegroundWindow(my_process.Handle); //my_process.PriorityClass = System.Diagnostics.ProcessPriorityClass.Idle; MessageBox.Show("OK"); } } public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } }
子进程
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; namespace WindowsFormGetMsg { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.ListBox listBox2; private System.Windows.Forms.Button stop_button; private System.ComponentModel.Container components = null; const int WM_COPYDATA = 0x004A; private int y; private int x; private Int64 x1; private int exit_c; public static System.Diagnostics.Process cruuent_p; public System.Timers.Timer p_time; 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.listBox1 = new System.Windows.Forms.ListBox(); this.listBox2 = new System.Windows.Forms.ListBox(); this.stop_button = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(12, 12); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(179, 21); this.textBox1.TabIndex = 0; // // listBox1 // this.listBox1.FormattingEnabled = true; this.listBox1.ItemHeight = 12; this.listBox1.Location = new System.Drawing.Point(106, 41); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(85, 220); this.listBox1.TabIndex = 1; // // listBox2 // this.listBox2.FormattingEnabled = true; this.listBox2.ItemHeight = 12; this.listBox2.Location = new System.Drawing.Point(12, 41); this.listBox2.Name = "listBox2"; this.listBox2.Size = new System.Drawing.Size(96, 220); this.listBox2.TabIndex = 2; // this.stop_button.Location = new System.Drawing.Point(205, 24); this.stop_button.Name = "stop_button"; this.stop_button.Size = new System.Drawing.Size(75, 23); this.stop_button.TabIndex = 3; this.stop_button.Text = "停止timer"; this.stop_button.UseVisualStyleBackColor = true; this.stop_button.Click += new System.EventHandler(this.stop_button_Click); // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.listBox2); this.Controls.Add(this.listBox1); this.Controls.Add(this.textBox1); this.Controls.Add(this.stop_button); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox1}); this.Name = "Form1"; this.Text = "child"+cruuent_p.Id; this.ResumeLayout(false); } static void Main() { cruuent_p = System.Diagnostics.Process.GetCurrentProcess(); Application.Run(new Form1()); } protected override void DefWndProc(ref System.Windows.Forms.Message m) { switch (m.Msg) { case WM_COPYDATA: base.DefWndProc(ref m); COPYDATASTRUCT mystr = new COPYDATASTRUCT(); Type mytype = mystr.GetType(); mystr = (COPYDATASTRUCT)m.GetLParam(mytype); // this.textBox1.Text = mystr.lpData; //MessageBox.Show("aa"+mystr.lpData.ToString()); try { string [] xx = mystr.lpData.Split('#'); //MessageBox.Show(xx[0].ToString()); if (xx.Length== 2) { this.textBox1.Text = xx[0].ToString(); this.x = System.Convert.ToInt32(xx[0].ToString()); this.y = (System.Convert.ToInt32(xx[1].ToString()) + 1) * 10; } //MessageBox.Show(this.x+"--"+this.y); } catch (System.Exception e) { // MessageBox.Show(mystr.lpData.ToString()); } for(int xixi=1;xixi<=10;xixi++) { draw_p(); } //MessageBox.Show("收到"); break; default: base.DefWndProc(ref m); break; } } public void sety() { string xx; xx=cruuent_p.Id.ToString(); this.y = System.Convert.ToInt32(xx.Substring(xx.Length - 3, 3)); } private void stop_button_Click(object sender, EventArgs e) { this.p_time.Stop(); } public void draw_pic() { System.Diagnostics.Process[] localByName = System.Diagnostics.Process.GetProcessesByName("win_recevied"); exit_c = localByName.Length; //sety(); //this.textBox1.Text = System.Convert.ToString(exit_c * 100 + 100); //if (exit_c == 1) //{ // cruuent_p.PriorityClass = System.Diagnostics.ProcessPriorityClass.Idle; //} //p_time = new System.Timers.Timer(5); ///p_time.AutoReset = true; //p_time.Enabled = true; //p_time.Elapsed += new System.Timers.ElapsedEventHandler(p_time_Elapsed); //while (true) //{ //draw_p(); // Thread.Sleep(2000); //MessageBox.Show("dd"); // } } //public void p_time_Elapsed(object sender, System.Timers.ElapsedEventArgs e) public void draw_p() { //MessageBox.Show(this.x.ToString()); bool requestInitialOwnership = true; bool mutexWasCreated; System.Threading.Mutex m = new Mutex(requestInitialOwnership, "MyMutex", out mutexWasCreated); if (!(mutexWasCreated)) { // MessageBox.Show("被占用"+this.Text); m.WaitOne(); } print(); m.ReleaseMutex(); } public void print() { Image image = Image.FromFile("C:pic.bmp"); System.Drawing.Bitmap bmp = new Bitmap(image); Int64 n =System.DateTime.Now.Second * 60000 + System.DateTime.Now.Millisecond; this.textBox1.Text=n.ToString("000000000000.0000"); x1 =n-x; Int64 k; k = x1*1000000 / x; // string tt; // tt = k.ToString("00000.00000"); //MessageBox.Show("x1"+x1.ToString()+"x"+x.ToString()+"k="+tt); this.listBox2.Items.Add(System.Convert.ToInt32(k / 3000)); this.listBox1.Items.Add(y + 100); //MessageBox.Show("x="+x.ToString()+"now="+n.ToString()+"x1="+x1.ToString()+"y="+y.ToString()); bmp.SetPixel(System.Convert.ToInt32(k/30000), y + 100, System.Drawing.Color.Red); image.Dispose(); bmp.Save("C:pic.bmp"); } //public static void DrawLineInPicture(Bitmap bmp, Point p0, Point p1, Color LineColor, int LineWidth, DashStyle ds) //{ // if (bmp == null) return; // if (p0.X == p1.X || p0.Y == p1.Y) return; // Graphics g = Graphics.FromImage(bmp); // Brush brush = new SolidBrush(LineColor); // Pen pen = new Pen(brush, LineWidth); // pen.DashStyle = ds; // g.DrawLine(pen, p0, p1); // g.Dispose(); // } } public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } }
附件:
Projects1.rar (91 K)
|
一共有 0 条评论