I am working on a project, i am using threads over here.. I run a thread and waiting for its isAlive status to become false but my thread is not terminating... Here is the Code Sample.

Thread t = new Thread(new ThreadStart(print));
t.start();

while(t.IsAlive)
{
}

console.writeline("Thread is Dead Now");

private void print()
{
console.writeline("Hello,World");
}

//------------------------------- Syntax is buggy but logic is same as of my app -----------

I have reach to main problem... its the while loop which checks for condition , it stops all process. I used Sleep function but its not working.. AnY hELP...

The provided code runs fine on my computer, can we see the actual?

Its runs fine... But thread is Alive status remain true which causes infinite loop.
Its the same as actuall, I am using VS 2005 Professional

Untitled84

Sorry didnt make myself clear, the code runs all the way to the end.

HelperCheckDLLS is a simple function for checking file.

//------------------ Caller -----------------------------
Thread t = new Thread(new ThreadStart(checkDLLS));
            t.Start();

            while (t.IsAlive)
            {
                Console.Write("Hang");
                Thread.Sleep(300);
            }


//-------- Checking DLL'S -------------------------------------         
 private void checkDLLS()
        {
            if (HelperCheckDLLS("GSMCommShared.dll"))
            {

//------------------------ Delegate ---------------------------         
        private void statusUpdate(String msg)
        {
            if (statusText.InvokeRequired)
            {
                updateStatusTextDelegate deleg = new updateStatusTextDelegate(statusUpdate);
                statusText.Invoke(deleg, new object[] { msg });
            }
            else
                statusText.Text = msg;
        }

I think delegate is causing the problem. I checked my code again and need solution how to end this delegate.

Edit: missed the above post when writing this.

Right so now we get into where my knowledge is hazy, delegates :)

My first step would be to use Console.WriteLine() on nearly every line of your threaded code, because this will allow us to see how far it runs before it hangs. Like the "Hello World" in your example code.

Just write say a description of what line is doing into command prompt and see where it gets stuck :)?

//statusUpdate("Msg") it hangs here... I dont know how to close this delegate. I have pasted the code for delegate.
After further analysis i came to know that it happens when it tries to write in status Label. Is there any thing UI thread to do with this.

It Solved Finally , Just BeginInvoke Was required. Some time i think i am a genious :) Thanx buddy to be with me.

Hehe its always the simple things eh! Know more about me with delegates anyway!

Your welcome, but I wasnt here much, I go for lunch and its solved on my return :D

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.