|
Posted on
2008-03-19 18:00
带你去月球
阅读( 309)
评论()
收藏
举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;

namespace ConsoleApplication1
  {
 /**//// <summary>
/// Program class
/// </summary>
class Program
 {
 /**//// <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);

 /**//// <summary>
/// Inform the waitting process, event has occur.
/// </summary>
static AutoResetEvent are = new AutoResetEvent(false);
[STAThread]
static void Main(string[] args)
 {
Console.WriteLine("Launching Form");
Form autForm = null;
string formName = "AUTForm.Form1";
string path = @"E:\___Test\AUT\AUTForm\AUTForm\bin\Debug\AUTForm.exe";

autForm = LaunchApp(path, formName);

System.Drawing.Point pt = new System.Drawing.Point(500, 500);
SetFormPropertyValue(autForm, "Location", pt,1500);

//while( true)
//{
//
//}
}

static void SetFormPropertyValue(Form f, string propertyName, object newValue,int delay)
 {
Thread.Sleep(delay);

if (f.InvokeRequired)
 {
Console.WriteLine("Invoke Required!");
Delegate d = new SetFormPropertyValueHandler(SetFormPropertyValue);
 object[] o = new object[] { f, "Location", newValue ,delay};
f.Invoke(d, o);
return;
}
else
 {
Console.WriteLine("Set value");
Type t = f.GetType();
PropertyInfo pi = t.GetProperty(propertyName);
pi.SetValue(f, newValue, null);
}
}

static Form LaunchApp(string path, string formName)
 {
Form result = null;
Assembly a = Assembly.LoadFrom(path);
Type t1 = a.GetType(formName);
result = (Form)a.CreateInstance(t1.FullName);

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;
}

private class AppState
 {
public readonly Form formToRun;

public AppState(Form f)
 {
this.formToRun = f;
}

public void RunApp()
 {
Application.Run(formToRun);
}
}
}
}

|