views:

857

answers:

5

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.


  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 { } 
    }
+2  A: 

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.

Matthew Scharley
My Deamon.receiver method picks up the message without any exception. I am shure of that, becuase the processing of first message is working fine and i also debugged line by line.
Anuya
A: 

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.

Andy White
My Deamon.receiver method picks up the message without any exception. I am shure of that, becuase the processing of first message is working fine and i also debugged line by line.
Anuya
+2  A: 

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
    }
}
Nader Shirazie
yes. My Deamon.receiver method picks up the message. Its running a loop for processing the message i get. But once it process the first message, it aborts.
Anuya
can you put a quick outline of the Deamon.Receiver method up? without the main logic of course... just leave in the looping statements, and the try catches, etc...
Nader Shirazie
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 { }}
Anuya
it'd be useful to add that to the question so everyone can see it.where's the loop part?
Nader Shirazie
+1  A: 

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.

Hans Malherbe
A: 

Hey karthik, Do you got the solution of these? I am also facing same problem and not able to figure out what's the problem. If you have any solution, can you please tell me..I will appreciate it. Thanks in Advance. - Chetan

John Saunders