Hi,
I am having an small application which is running in a thread and it is waiting for an event initially. As soon as if i clicked the START button, event will be enabled. From then onwards my entire application is running in WHILE (TRUE) loop. That means that i want to run my application continuously. As soon as if i clicked the STOP button, i am reseting the event and exiting the application. But here i am facing one problem. Once if i clicked the STOP button, my application is hanging. Can any one give me some ideas, to overcome this problem.

Additional Information: -

1) I have created the class library for my application. ( This creates DLL for my application )

2) In order to test my application, i have created the small form based application which will call my actual application

3) I have included that DLL as a reference in the reference list, in the Form based application

4) I have used the "ManualResetEvent" Class to handle the events.

Please give me the solution. It's urgent. Reply back to me if anyone needs more inputs.

Regards,
Vijay Bhaskar

Dani AI

Generated

As described, the worker thread runs continuously and the STOP action leaves the app hanging. and pointed toward a shutdown flag, which is the right idea, but hangs usually come from three roots: the worker never observes the stop (it’s blocked on the wrong wait), the UI thread is synchronously waiting for the worker while the worker needs the UI, or the worker swallows an exception and never exits. Make the worker block (not spin), give it a clear cancellation path, and avoid blocking the UI thread while you wait.

A robust pattern is cooperative cancellation combined with a wait that listens for either "work available" or "cancel". That prevents busy loops and makes shutdown deterministic. Example pattern:

var cts = new CancellationTokenSource();
var workSignal = new AutoResetEvent(false);

var worker = new Thread(() =>
{
    try
    {
        var handles = new WaitHandle[] { workSignal, cts.Token.WaitHandle };
        while (true)
        {
            int idx = WaitHandle.WaitAny(handles); // 0: work, 1: cancel
            if (idx == 1) break;
            // handle one unit of work here
        }
    }
    finally
    {
        // cleanup
    }
});
worker.Start();

// stop sequence
cts.Cancel();
if (!worker.Join(2000)) worker.IsBackground = true; // graceful timeout, then fallback

If it still hangs: break into the debugger and inspect all thread stacks; add short-lived logging at loop entry/exit; ensure the stop signal (cts.Cancel or workSignal.Set) is invoked before calling Join; avoid calling Thread.Join on the UI thread unless you use a timeout; and do not call Thread.Abort. Also ensure the worker never calls UI methods directly (use Invoke/BeginInvoke) and that exceptions in the worker are logged.

In short: adopt cooperative cancellation, block with WaitHandle/Token instead of spinning, use Join with a timeout (don’t block the UI indefinitely), and add logging/stack inspections to find where the thread is stuck. This builds on and ’s suggestions but adds a safe wait-and-cancel workflow and concrete debugging steps.

Recommended Answers

All 3 Replies

Instead of using a while(TRUE) loop can't you use a variable while(keepLooping) and when the stop button is pressed you change keepLooping to false?

Yeah, your problem is the while (true), you need to have some sort of shutdown flag, something like:

internal static bool Shutdown = false;

private static void Main()
{
     while (!Shutdown)
     {
             // Do stuff here
     }
}

And when you click your stop button, do "Shutdown = true;".

lanza kodaka threads nerchoko ra muttak na madda

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.