Question : How to keep thread alive throughout the windows application c#

Hi Experts,

             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#. 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.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
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();
                }
            }

Answer : How to keep thread alive throughout the windows application c#

- Are you closing the log file each time you write to it? If not, you might not see the most recent log event.
- You should be able to use the log file to follow what lines are executed in Receive, if not the debugger.
- If you want ot keep the loop running the longest, you should have a try, catch block just inside the while loop.
- This is a silly question, but are you sending the stop_app command and that is why the thread is only running once?
- You could try simplifying the code to identify the problem.
Random Solutions  
 
programming4us programming4us