|
Posted on
2008-03-19 18:00
带你去月球
阅读( 299)
评论()
编辑
收藏
举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;
![](/Images/OutliningIndicators/None.gif)
namespace ConsoleApplication1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// Program class
/// </summary>
class Program
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// Set form property value handler delegate
/// </summary>
/// <param name="f"></param>
/// <param name="propertyName"></param>
/// <param name="newValue"></param>
delegate void SetFormPropertyValueHandler(Form f, string propertyName, object newValue,int delay);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// Inform the waitting process, event has occur.
/// </summary>
static AutoResetEvent are = new AutoResetEvent(false);
[STAThread]
static void Main(string[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Console.WriteLine("Launching Form");
Form autForm = null;
string formName = "AUTForm.Form1";
string path = @"E:\___Test\AUT\AUTForm\AUTForm\bin\Debug\AUTForm.exe";
![](/Images/OutliningIndicators/InBlock.gif)
autForm = LaunchApp(path, formName);
![](/Images/OutliningIndicators/InBlock.gif)
System.Drawing.Point pt = new System.Drawing.Point(500, 500);
SetFormPropertyValue(autForm, "Location", pt,1500);
![](/Images/OutliningIndicators/InBlock.gif)
//while( true)
//{
//
//}
}
![](/Images/OutliningIndicators/InBlock.gif)
static void SetFormPropertyValue(Form f, string propertyName, object newValue,int delay)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Thread.Sleep(delay);
![](/Images/OutliningIndicators/InBlock.gif)
if (f.InvokeRequired)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Console.WriteLine("Invoke Required!");
Delegate d = new SetFormPropertyValueHandler(SetFormPropertyValue);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) object[] o = new object[] { f, "Location", newValue ,delay};
f.Invoke(d, o);
return;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Console.WriteLine("Set value");
Type t = f.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(f, newValue, null);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
static Form LaunchApp(string path, string formName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Form result = null;
Assembly a = Assembly.LoadFrom(path);
Type t1 = a.GetType(formName);
result = (Form)a.CreateInstance(t1.FullName);
![](/Images/OutliningIndicators/InBlock.gif)
AppState aps = new AppState(result);
ThreadStart ts = new ThreadStart(aps.RunApp);
Thread thread = new Thread(ts);
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
return result;
}
![](/Images/OutliningIndicators/InBlock.gif)
private class AppState
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
public readonly Form formToRun;
![](/Images/OutliningIndicators/InBlock.gif)
public AppState(Form f)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
this.formToRun = f;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void RunApp()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Application.Run(formToRun);
}
}
}
}
![](/Images/OutliningIndicators/None.gif)
|