我正在使用线程,它将接收来自 外部应用程序的消息。所以我的线程总是处于活动状态。如何保持线程在整个Windows应用程序中活着c#
我想让我的线程运行完整个应用程序,直到 直到应用程序退出。目前我在program.cs中调用我的线程, 这是windows应用程序c#的启动。请参阅下面的代码 知道我是如何做到这一点。
当我使用下面的代码时,线程在 应用程序启动时启动...但它会中断一些如何在线程接收到来自外部应用程序的一条消息之后 。
我希望我对我的问题很清楚。请帮忙。谢谢。
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
StartThread();
Application.Run(new Screensaver());
}
public static void StartThread()
{
DeamonEngine Deamon = new DeamonEngine();
Thread ThreadReciever = new Thread(Deamon.Receiver);
if (!(ThreadReciever.IsAlive))
{
ThreadReciever.Start();
}
}
}
从评论:
void Receiver() {
try {
Initiate socket s;
Bind Ip address;
s.Receiver[bytes];
Access the members of message received in bytes;
Assign the members of message to local variable;
Read Xml File, get the node values and write to batch file;
Execute batch file.
}
catch { }
}
Karthik,几次你说有一个循环。你能告诉我们这个循环吗?我没看到一个。您发布的代码将执行一次,然后线程将终止。 – 2009-07-25 20:47:35