简体   繁体   English

如何在Windows应用程序中使线程保持活动状态C#

[英]How to keep thread alive throughout the windows application c#

I am using thread which will receive messages from the external application.So my thread shud be alive always. 我正在使用将从外部应用程序接收消息的线程,因此我的线程应该始终处于活动状态。

I want my thread to be running through out the application, untill application exits. 我希望我的线程在整个应用程序中运行,直到应用程序退出。 Currently i am calling my thread in program.cs, which is the startup for windows application c#. 目前,我正在program.cs中调用线程,这是Windows应用程序c#的启动。 Please see the code below to know how i am doing it. 请查看下面的代码,以了解我的工作方式。

When i use the below code, the thread starts up when application starts...But it aborts some how, after the thread recieves one message from the external application. 当我使用以下代码时,线程在应用程序启动时启动...但是在线程从外部应用程序收到一条消息后,它中止了某些操作。

I hope i am clear with my questio. 我希望我对我的问题很清楚。 Please help. 请帮忙。 Thanks. 谢谢。


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

From a comment: 来自评论:

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

Your startup code looks fine: my prognosis is there's something wrong with DaemonEngine.Receiver() , perhaps an exception is being thrown, or perhaps the function itself is structured to only handle a single message... Without seeing the exact function it's hard to say. 您的启动代码看起来不错:我的预后是DaemonEngine.Receiver()出了点问题,可能抛出了异常,或者函数本身仅处理了一条消息,而……却没有看到确切的函数,很难说。

EDIT: 编辑:

To get my stuff out of my comments: 为了使我的意见不对:

SOMETHING is going wrong to kill your thread. 某些错误会导致线程中断。 Exception, logical error, I can't tell you what, because there's no code of what's happening inside your thread, but something is happening. 异常,逻辑错误,我无法告诉您什么,因为线程中没有发生任何事情的代码,但是正在发生什么。 It's nothing to do with the code you've posted already which simply gets the thread running, not keeps it running 与您已经发布的代码无关,后者只是使线程运行,而不能使其保持运行

Also, from the code you have posted, you're simply throwing away the fact there was an exception... I don't have a link on hand, but swallowing exceptions like that is horrible . 另外,从您发布的代码中,您只是丢弃了一个异常的事实……我手边没有链接,但是吞噬像这样的异常是可怕的 Especially inside a thread where they don't show up like normal anyway. 尤其是在线程内,它们无论如何都不会像正常情况一样显示。

There's also no indication of a loop of any kind, so it could be one or both of my suggestions above that is causing problems. 也没有任何形式的循环的迹象,因此可能是我上面的一个或两个建议引起了问题。

Having a thread execute the Receiver method doesn't mean the thread will repeatedly execute the method. 让线程执行Receiver方法并不意味着线程将重复执行该方法。

Given the processing code in the question, Daemon.Receiver needs to execute in a loop so that it can go back and retrieve the next message to process. 给定问题中的处理代码, Daemon.Receiver需要循环执行,以便它可以返回并检索下一条要处理的消息。 It should look something like this: 它看起来应该像这样:

void Receiver() { 
    while(!done) // without this loop, only one execution will occur
    {
        try { 
            // do stuff
        } 
        catch { 
            // log/handle error
        } 

        // wait for next message
    }
}

Typically your DaemonReceiver would have code like this 通常,您的DaemonReceiver将具有这样的代码

while (!quit) 
{
  --- do work ---
  Thread.Sleep(1000);
}

This keeps the thread alive until you set the quit global variable in the main thread. 这将使线程保持活动状态,直到在主线程中设置quit全局变量为止。

Also, it is very important to not let exceptions leak out of your thread. 同样,非常重要的一点是不要让异常泄漏到线程之外。 These exceptions will not be reported anywhere and will cause silent errors, probably what you're experiencing now. 这些异常不会在任何地方报告,并且会导致无提示错误,可能是您现在遇到的错误。 Do a catch all and at least report on it. 全力以赴,至少报告一下。

Your thread might be experiencing a Exception, which is not being caught. 您的线程可能遇到异常,但未被捕获。 Try putting a try-catch in the method that is being executed and see if you're getting an exception. 尝试将try-catch放入正在执行的方法中,看看是否遇到异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM