大家好:
现在我想实现在打开应用程序主窗体时用线程来运行一个方法。。该方法就是不停的检索远程服务器,如果满足条件的则触发另外一个方法。。该功能类似于移动公司不停的在检索数据库,然后发送短信一样。
该检索从运行应用程序到应用程序关闭。。大家有什么好的方法吗
你可以写成线程类,然后把窗体的方法委托给线程即可
Sample code as follows:
public delegate void InvokeFun( string sData );
public class clsThreadFun
{
private InvokeFun myFunHandler;
private yourForm pParent;
public clsThreadFun( yourForm pMain, InvokeFun FunHandler )
{
pParent = pMain;
myFunHandler = FunHandler;
}
public void ThreadFun()
{
//Do what you want here
pParent.InvokeMethod( this.myFunHandler, new object[]{"Test"} );
}
}
----Start sub-thread in your form
private void HandlerData( string sData )
{
//Process data here
}
clsThreadFun myThread = new clsThreadFun( this, new InvokeFun( HandlerData ) );
Thread thdSub = new Thread( new ThreadStart( myThread.ThreadFun ) );
thdSub.Start();
评论