none
Keep alive Child thread after killing parent thread RRS feed

  • Question

  • Hello,

    I am developing programme in c#

    I have case, where i need to use multithreadding concept for communication using TCPIP.

    Now, i have a seperate thread(T1) for each receving message, in that message i am creating another thread(T2) for performing some task(displaying popup messge)

    My  T1 task continue working after generating T2 task, now after completion of T1 task it automaticcaly killed but I want T2 thread to be running untill theres a task complete, also, i cannot wait for Join method for both threads task complete.

    So, how can i achive Thread(T2) keeps running after T1 termineted.

    Because, as of now, on termination T1 programme giving error  like "waiting for a handle....." not reverted back....


    Thanks for the help!
    Friday, July 1, 2011 9:23 AM

Answers

  • I'd like to see the code where you are receiving data (T1 thread) and how you are setting the callback, creating and staring the T2 Thread.

    Threads do not really have a parent-child relation. Each thread is unique and can run on its own even if the thread that has spawned it completes. Try creating a foreground thread on the UI thread and keep a long sleep. Even if you close your form, the process will still be alive as the long running thread is still alive.

    The problem you are facing is that you seem to be making a call in this fashion:

    UI thread -> starts T1 for receiving socket data -> T1 spawns T2 for displaying alert form -> Call back on T1 -> invoke UI thread

    This is not really necessary. You can directly invoke onto the UI thread from T2. I'd also like to know why T2 is really necessary. Creating and initializing a form object shouldnt really take that long. Once the form is displayed by invoking onto the UI thread, the timer object within the form will manage the closing part on its own.

    Paste the rest of the code if you are still unsure about this.

     


    Please mark this as answer or vote as helpful if it solved your problem
    • Marked as answer by Aspen VJ Monday, July 11, 2011 2:25 AM
    Thursday, July 7, 2011 12:55 PM

All replies

  • I can see that you are spawning a thread too many, but I'd like to see your code to correctly point out the changes required.

    I think the problem must be a callback on the thread that spawns the second thread. When the callback occurs, the spawning thread has already completed.


    Please mark this as answer or vote as helpful if it solved your problem
    Friday, July 1, 2011 11:03 AM
  • Hi Jiten,

    Based on your description, what is the relationship between T1 and T2? If T2 is a child thread from T1, then when the T1 is killed, all of resources which T1 has occupied will be released, also, the T2 will be killed too.

    So if you want to keep alive child thread after kill parent thread, it is impossible.


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 6, 2011 1:42 AM
  • yes buddy, you are right i have a callback in function call, which then calls the main thread to cause an UI operation, i thinks this is wat creating issue here.

    Because, T1 getting closed before callback from T2 reaches to main(UI) thread to perform ui operation.

    Call back is used, to achive UI thread access, and popup the alert box.

    Here, posting some of code to understand better. 

    --------------------
    //declared call back delegate
    
        ///<summary>
        /// Delagte for handling UI updation when call comes from seperate threads
        ///</summary>
        ///<param name="frmAlert"></param>
        public delegate void CallbackAlert( Alert frmAlert );
    
    -------------------
    //Function call from T1
    
        /// <summary>
        /// Show alert window for perticular time period.
        /// Thread safe method
        /// Handling callback required
        /// </summary>
        /// <param name="logText">The text to display.</param>
        /// <param name="interval">Miliseconds to keep window opened on screen.</param>
        /// <param name="callback">Method to call back in owner</param>
        public static void OpenAlert( object logText, int interval, CallbackAlert callback )
        {
          Alert frmAlert = new Alert( logText, interval );
          frmAlert.StartPosition = FormStartPosition.CenterScreen;
          frmAlert.BringToFront();
          //frmAlert.Show();
          //frmAlert.ShowAlert();
          callback( frmAlert );
        }
    
    ----------------------------
    
    //popup which will display on screen with time-interval of auto closing
    
        private System.Threading.Timer trTemp;
        private delegate void DlTimer(object interval);
    
        ///<summary>
        /// Show message for give time
        ///</summary>
        ///<param name="displayText">message text to display</param>
        ///<param name="interval">time in miliseconds for keeping display</param>
        public Alert(object displayText, int interval)
        {
          InitializeComponent();
    
          _textDisplay = displayText.ToString();
    
          //SetTimer( interval );
          trTemp = new System.Threading.Timer(SetTimer, null, interval, 0);
        }
    
        ///<summary>
        /// Enable time for given period
        ///</summary>
        ///<param name="interval">time in miliseconds for keeping display</param>
        public void SetTimer(object interval)
        {
          if (InvokeRequired)
          {
            DlTimer obj = SetTimer;
            Invoke(obj, new[] { interval });
          }
          else
          {
            Close();
          }
        }
    

    Can you please suggest how can i achive this functionality and avoid this issue.

    Let me know if, more detail code required....

    Appreciate your help,,,,,


    Thanks for the help!
    Thursday, July 7, 2011 12:04 PM
  • I'd like to see the code where you are receiving data (T1 thread) and how you are setting the callback, creating and staring the T2 Thread.

    Threads do not really have a parent-child relation. Each thread is unique and can run on its own even if the thread that has spawned it completes. Try creating a foreground thread on the UI thread and keep a long sleep. Even if you close your form, the process will still be alive as the long running thread is still alive.

    The problem you are facing is that you seem to be making a call in this fashion:

    UI thread -> starts T1 for receiving socket data -> T1 spawns T2 for displaying alert form -> Call back on T1 -> invoke UI thread

    This is not really necessary. You can directly invoke onto the UI thread from T2. I'd also like to know why T2 is really necessary. Creating and initializing a form object shouldnt really take that long. Once the form is displayed by invoking onto the UI thread, the timer object within the form will manage the closing part on its own.

    Paste the rest of the code if you are still unsure about this.

     


    Please mark this as answer or vote as helpful if it solved your problem
    • Marked as answer by Aspen VJ Monday, July 11, 2011 2:25 AM
    Thursday, July 7, 2011 12:55 PM