Question

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.


  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 { } 
    }
Was it helpful?

Solution

Having a thread execute the Receiver method doesn't mean the thread will repeatedly execute the method.

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

OTHER TIPS

Typically your DaemonReceiver would have code like this

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

This keeps the thread alive until you set the quit global variable in the main thread.

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 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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top