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

--------------Solutions-------------

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

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.

Category:c# Time:2009-06-25 Views:2

Related post

  • Keeping a thread alive in a C# application 2009-11-20

    Possible Duplicate: Locking main() thread Hello, Below you'll find my code, Main calls two threads, one initiates an event handler that returns the values of registry keys after they have been changed. The other sets up a timer which writes the chang

  • Two UI Thread in C# windows Application 2011-01-25

    How can I implement two ui threads in a C# windows application? --------------Solutions------------- You need to call Thread.SetApartmentState() to switch the thread to STA before starting it. And pump a message loop to keep any windows created on th

  • Is there a specification of Java's threading model running under Windows XP available anywhere? 2008-09-15

    There are various documents describing threading on Solaris/Linux, but nowwhere describing the Windows implementation. I have a passing interest in this, it seems strange that something so critical is (seemingly) not documented. Threading is not the

  • C# Windows Application - Many threads using the same connection? 2008-12-23

    I've got a c# WINDOWS Application that is multi-threaded. It is my understanding that in a web environment, connections are pooled automatically. It is also my understanding that in a Windows app, this is not the case. Therefore, for a Windows app, t

  • How do i know if a thread is suspended under Windows CE 2009-05-22

    Can I get a threads suspend count under Windows CE, using C or Visual C++, without calling resume or suspend functions? The only way I can see of doing it is something like int Count = SuspendThread(ThreadHandle); ResumeThread(ThreadHandle); This has

  • Execution order with threads and PyGTK on Windows 2009-06-17

    I'm having issues with threads and PyGTK on Windows. According the the PyGTK FAQ (and my own experimentation), the only way to reliably update the GUI from a child thread is to use the gobject.idle_add function. However, it can't be guaranteed when t

  • Multiple UI threads on the same window 2009-10-30

    I don't want multiple windows, each with its own UI thread, nor events raised on a single UI thread, not background workers and notifications, none of that Invoke, BeginInvoke stuff either. I'm interested in a platform that allows multiple threads to

  • How to use threads in C on Windows? 2009-12-30

    What do I need and how can I use threads in C on Windows Vista? Could you please give me a simple code example? --------------Solutions------------- Here is the MSDN sample on how to use CreateThread() on Windows. The basic idea is you call CreateThr

  • Making thread-safe calls to windows forms controls 2010-02-23

    The MSDN article: How to: Make Thread-Safe Calls to Windows Forms Controls says we should use async delegate to make the call. But why does the async delegate make the call safe? --------------Solutions------------- Windows controls use the Component

  • C/C++/Assembly Programatically detect if hyper-threading is active on Windows, Mac and Linux 2010-05-25

    This question already has an answer here: Programatically detect number of physical processors/cores or if hyper-threading is active on Windows, Mac and Linux 8 answers I can already correctly detect the number of logical processors correctly on all

  • Threads in c# windows application 2010-07-29

    Windows Task Manager shows four threads for my simple C# Windows application. What are these for? --------------Solutions------------- Main thread Debugger thread Finalizer thread GDI+ rendering thread Source There's good discussion on the topic on t

  • What thread handles modal dialog windows in .Net? 2010-08-16

    I have a pretty basic understanding of the GUI thread and the message loop, but I'm curious as to how that applies to one window starting a modal window. If I had to guess, I'd say that both windows are being run under the same GUI thread and that so

  • Synchronising threads using Events in windows 2011-02-14

    I need to synchronize threads using events. First I am not getting concept of Events in windows. I will take an example, I have main thread and secondary thread. I created a two events called "write" and "test". In main thread I signaled for 'write"

  • Boost thread local storage in Windows threads 2011-10-30

    I tried to use Boost thread local storage with Windows threads. I built the project without any problems. However my question here is, is it okay to use Boost TLS with Windows threads? --------------Solutions------------- Edit Yes Boost is integrally

  • Stopping an thread application in C# Windows Application? 2011-11-07

    I am making a windows application in c# in which on button click i have written some code as public void btnStart_Click(object sender, EventArgs e) { while(true) { //some processing } } when i starts application it running continuously. But on other

  • System.Threading.ThreadAbortException in System.Windows.Markup.WpfXamlLoader.Load when application is restarted 2011-12-07

    I have created an WPF application that starts on windows start. Application checks for list of files on the system and if any file is missing it popups the file path. The issue I am facing is when I restart the system while application is running, on

  • How to keep core threads alive and die out excess threads in ThreadPoolExecutor? keepAliveTime doesn't work as expected 2012-01-05

    I use ThreadPoolExecutor to manage a thread pool. What we want are: if the pool has less than corePoolSize threads, kick off a new thread for a new task; if the pool has more than corePoolSize threads and all are busy, kick off a new thread for a new

  • Cancel Main Form closing while some thread runs in back, C# .windows application? 2012-03-09

    I am running a background thread to move some profile data in an windows application. This thread will run priodically on certain interval. If the user tries to close the form while the thread runs, i should prompt the user about the thread running a

  • Checking "liveness" of a Windows application? 2012-03-13

    I've got a Windows application running some expensive equipment; this application dies in a variety of creative ways. Usually when it goes, the process dies completely. I wrote a little monitoring program which looks for the process' name in the list

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.604 (s). 13 q(s)