Exiting a Thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maloney

    #1

    Exiting a Thread

    Exit thread in C#CF. I'm createing a thread and setting a class object for
    when to exit. Before creating the thread i check if the thread is running,
    if so set the flag to exit the thread.

    What is the proper way of waiting for the thread to exit. In C++ i would
    set an event and WaitForMultiple Object, peek and pump messages until the
    handle was set.

    Class test
    {
    private bool ExitThread = false
    private bool ThreadRunning = false
    private Thread myThread

    private StartThread()
    {
    if(ThreadRunnin g)
    {
    ExitThread = true;
    }
    myThread = new Thread(new ThreadStart(Fun cThread));
    }

    private FuncThread()
    {
    ThreadRunning = true;
    while(more files)
    {
    if(ExitThread)
    break;

    read files in directory
    Invoke back to main thread and fill item to list box
    }
    ThreadRunning = false;
    }
    }


  • William Stacey

    #2
    Re: Exiting a Thread

    Does Join not work for your needs?

    --
    William Stacey, MVP

    "Maloney" <sabrogden@soft home.net> wrote in message
    news:#xiXVEvoDH A.2964@tk2msftn gp13.phx.gbl...[color=blue]
    > Exit thread in C#CF. I'm createing a thread and setting a class object[/color]
    for[color=blue]
    > when to exit. Before creating the thread i check if the thread is[/color]
    running,[color=blue]
    > if so set the flag to exit the thread.
    >
    > What is the proper way of waiting for the thread to exit. In C++ i would
    > set an event and WaitForMultiple Object, peek and pump messages until the
    > handle was set.
    >
    > Class test
    > {
    > private bool ExitThread = false
    > private bool ThreadRunning = false
    > private Thread myThread
    >
    > private StartThread()
    > {
    > if(ThreadRunnin g)
    > {
    > ExitThread = true;
    > }
    > myThread = new Thread(new ThreadStart(Fun cThread));
    > }
    >
    > private FuncThread()
    > {
    > ThreadRunning = true;
    > while(more files)
    > {
    > if(ExitThread)
    > break;
    >
    > read files in directory
    > Invoke back to main thread and fill item to list box
    > }
    > ThreadRunning = false;
    > }
    > }
    >
    >[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Exiting a Thread

      Maloney,

      Unfortunately, there isn't any mechanism in .NET to be notified when a
      thread is complete. In order to do this, you will have to have the code in
      the thread send some sort of notification when it is complete. One
      recommendation would be to use a try/catch/finally block to call Set on a
      ManualResetEven t which would be waited on by another thread.

      Also, it should be noted from the code that you provided that you aren't
      providing the proper synchronization to the ExitThread field on your class.
      You should be wrapping access to this in a lock statement.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Maloney" <sabrogden@soft home.net> wrote in message
      news:%23xiXVEvo DHA.2964@tk2msf tngp13.phx.gbl. ..[color=blue]
      > Exit thread in C#CF. I'm createing a thread and setting a class object[/color]
      for[color=blue]
      > when to exit. Before creating the thread i check if the thread is[/color]
      running,[color=blue]
      > if so set the flag to exit the thread.
      >
      > What is the proper way of waiting for the thread to exit. In C++ i would
      > set an event and WaitForMultiple Object, peek and pump messages until the
      > handle was set.
      >
      > Class test
      > {
      > private bool ExitThread = false
      > private bool ThreadRunning = false
      > private Thread myThread
      >
      > private StartThread()
      > {
      > if(ThreadRunnin g)
      > {
      > ExitThread = true;
      > }
      > myThread = new Thread(new ThreadStart(Fun cThread));
      > }
      >
      > private FuncThread()
      > {
      > ThreadRunning = true;
      > while(more files)
      > {
      > if(ExitThread)
      > break;
      >
      > read files in directory
      > Invoke back to main thread and fill item to list box
      > }
      > ThreadRunning = false;
      > }
      > }
      >
      >[/color]


      Comment

      • Maloney

        #4
        Re: Exiting a Thread

        Join is not supported by the CompactFramewor k. Thanks though

        "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
        news:ewgwTJvoDH A.2964@tk2msftn gp13.phx.gbl...[color=blue]
        > Does Join not work for your needs?
        >
        > --
        > William Stacey, MVP
        >
        > "Maloney" <sabrogden@soft home.net> wrote in message
        > news:#xiXVEvoDH A.2964@tk2msftn gp13.phx.gbl...[color=green]
        > > Exit thread in C#CF. I'm createing a thread and setting a class object[/color]
        > for[color=green]
        > > when to exit. Before creating the thread i check if the thread is[/color]
        > running,[color=green]
        > > if so set the flag to exit the thread.
        > >
        > > What is the proper way of waiting for the thread to exit. In C++ i[/color][/color]
        would[color=blue][color=green]
        > > set an event and WaitForMultiple Object, peek and pump messages until the
        > > handle was set.
        > >
        > > Class test
        > > {
        > > private bool ExitThread = false
        > > private bool ThreadRunning = false
        > > private Thread myThread
        > >
        > > private StartThread()
        > > {
        > > if(ThreadRunnin g)
        > > {
        > > ExitThread = true;
        > > }
        > > myThread = new Thread(new ThreadStart(Fun cThread));
        > > }
        > >
        > > private FuncThread()
        > > {
        > > ThreadRunning = true;
        > > while(more files)
        > > {
        > > if(ExitThread)
        > > break;
        > >
        > > read files in directory
        > > Invoke back to main thread and fill item to list box
        > > }
        > > ThreadRunning = false;
        > > }
        > > }
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Maloney

          #5
          Re: Exiting a Thread

          Thanks for the info Nicholas. When you say lock are you talking about
          Monitor.Enter, Monitor.Exit or just lock. What am i locking, is this
          correct.

          lock(myThread)
          {
          ExitThread = true;
          }

          lock(myThread)

          {

          if(ExitThread)
          break;

          }


          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
          message news:%23slQQJvo DHA.1928@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > Maloney,
          >
          > Unfortunately, there isn't any mechanism in .NET to be notified when a
          > thread is complete. In order to do this, you will have to have the code[/color]
          in[color=blue]
          > the thread send some sort of notification when it is complete. One
          > recommendation would be to use a try/catch/finally block to call Set on a
          > ManualResetEven t which would be waited on by another thread.
          >
          > Also, it should be noted from the code that you provided that you[/color]
          aren't[color=blue]
          > providing the proper synchronization to the ExitThread field on your[/color]
          class.[color=blue]
          > You should be wrapping access to this in a lock statement.
          >
          > Hope this helps.
          >
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >
          > "Maloney" <sabrogden@soft home.net> wrote in message
          > news:%23xiXVEvo DHA.2964@tk2msf tngp13.phx.gbl. ..[color=green]
          > > Exit thread in C#CF. I'm createing a thread and setting a class object[/color]
          > for[color=green]
          > > when to exit. Before creating the thread i check if the thread is[/color]
          > running,[color=green]
          > > if so set the flag to exit the thread.
          > >
          > > What is the proper way of waiting for the thread to exit. In C++ i[/color][/color]
          would[color=blue][color=green]
          > > set an event and WaitForMultiple Object, peek and pump messages until the
          > > handle was set.
          > >
          > > Class test
          > > {
          > > private bool ExitThread = false
          > > private bool ThreadRunning = false
          > > private Thread myThread
          > >
          > > private StartThread()
          > > {
          > > if(ThreadRunnin g)
          > > {
          > > ExitThread = true;
          > > }
          > > myThread = new Thread(new ThreadStart(Fun cThread));
          > > }
          > >
          > > private FuncThread()
          > > {
          > > ThreadRunning = true;
          > > while(more files)
          > > {
          > > if(ExitThread)
          > > break;
          > >
          > > read files in directory
          > > Invoke back to main thread and fill item to list box
          > > }
          > > ThreadRunning = false;
          > > }
          > > }
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Exiting a Thread

            Maloney,

            In C#, when you use the lock statement like this:
            lock (myThread)
            {
            // Code statements here.
            }

            It actually expands to this:

            Monitor.Enter(m yThread)

            try
            {

            }
            finally
            {
            Monitor.Exit(my Thread);
            }

            So in essence, the lock statement handles the calls to the static Enter
            and Exit methods on the Monitor class.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "Maloney" <sabrogden@soft home.net> wrote in message
            news:u7jOWTvoDH A.1928@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Thanks for the info Nicholas. When you say lock are you talking about
            > Monitor.Enter, Monitor.Exit or just lock. What am i locking, is this
            > correct.
            >
            > lock(myThread)
            > {
            > ExitThread = true;
            > }
            >
            > lock(myThread)
            >
            > {
            >
            > if(ExitThread)
            > break;
            >
            > }
            >
            >
            > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote[/color]
            in[color=blue]
            > message news:%23slQQJvo DHA.1928@TK2MSF TNGP12.phx.gbl. ..[color=green]
            > > Maloney,
            > >
            > > Unfortunately, there isn't any mechanism in .NET to be notified when[/color][/color]
            a[color=blue][color=green]
            > > thread is complete. In order to do this, you will have to have the code[/color]
            > in[color=green]
            > > the thread send some sort of notification when it is complete. One
            > > recommendation would be to use a try/catch/finally block to call Set on[/color][/color]
            a[color=blue][color=green]
            > > ManualResetEven t which would be waited on by another thread.
            > >
            > > Also, it should be noted from the code that you provided that you[/color]
            > aren't[color=green]
            > > providing the proper synchronization to the ExitThread field on your[/color]
            > class.[color=green]
            > > You should be wrapping access to this in a lock statement.
            > >
            > > Hope this helps.
            > >
            > >
            > > --
            > > - Nicholas Paldino [.NET/C# MVP]
            > > - mvp@spam.guard. caspershouse.co m
            > >
            > > "Maloney" <sabrogden@soft home.net> wrote in message
            > > news:%23xiXVEvo DHA.2964@tk2msf tngp13.phx.gbl. ..[color=darkred]
            > > > Exit thread in C#CF. I'm createing a thread and setting a class[/color][/color][/color]
            object[color=blue][color=green]
            > > for[color=darkred]
            > > > when to exit. Before creating the thread i check if the thread is[/color]
            > > running,[color=darkred]
            > > > if so set the flag to exit the thread.
            > > >
            > > > What is the proper way of waiting for the thread to exit. In C++ i[/color][/color]
            > would[color=green][color=darkred]
            > > > set an event and WaitForMultiple Object, peek and pump messages until[/color][/color][/color]
            the[color=blue][color=green][color=darkred]
            > > > handle was set.
            > > >
            > > > Class test
            > > > {
            > > > private bool ExitThread = false
            > > > private bool ThreadRunning = false
            > > > private Thread myThread
            > > >
            > > > private StartThread()
            > > > {
            > > > if(ThreadRunnin g)
            > > > {
            > > > ExitThread = true;
            > > > }
            > > > myThread = new Thread(new ThreadStart(Fun cThread));
            > > > }
            > > >
            > > > private FuncThread()
            > > > {
            > > > ThreadRunning = true;
            > > > while(more files)
            > > > {
            > > > if(ExitThread)
            > > > break;
            > > >
            > > > read files in directory
            > > > Invoke back to main thread and fill item to list box
            > > > }
            > > > ThreadRunning = false;
            > > > }
            > > > }
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • 100

              #7
              Re: Exiting a Thread

              Hi Maloney,
              If you want to pump events you can do it with Application.DoE vents.
              If you want to be notified when the thread exites you can use
              Delegate.BeginI nvoke and pass AsyncCallback (It should be supported by CF).

              However the example you provied has a big flaw.
              You don't guard ExitThread and ThreadRunning flags.
              So, what happens here is:
              If a thread is already running you set ExitThread to true and start a new
              thread. Imagine that the new thread starts before the old one exits. The new
              one sets ThreadRunning to true then the old one exits and set it to false.
              Now you have one running thread and you don't know about it.

              More things can happen if you use flags and you don't synchronize the access
              to them.

              B\rgds
              100





              "Maloney" <sabrogden@soft home.net> wrote in message
              news:%23xiXVEvo DHA.2964@tk2msf tngp13.phx.gbl. ..[color=blue]
              > Exit thread in C#CF. I'm createing a thread and setting a class object[/color]
              for[color=blue]
              > when to exit. Before creating the thread i check if the thread is[/color]
              running,[color=blue]
              > if so set the flag to exit the thread.
              >
              > What is the proper way of waiting for the thread to exit. In C++ i would
              > set an event and WaitForMultiple Object, peek and pump messages until the
              > handle was set.
              >
              > Class test
              > {
              > private bool ExitThread = false
              > private bool ThreadRunning = false
              > private Thread myThread
              >
              > private StartThread()
              > {
              > if(ThreadRunnin g)
              > {
              > ExitThread = true;
              > }
              > myThread = new Thread(new ThreadStart(Fun cThread));
              > }
              >
              > private FuncThread()
              > {
              > ThreadRunning = true;
              > while(more files)
              > {
              > if(ExitThread)
              > break;
              >
              > read files in directory
              > Invoke back to main thread and fill item to list box
              > }
              > ThreadRunning = false;
              > }
              > }
              >
              >[/color]


              Comment

              • Geoff Schwab [MSFT]

                #8
                Re: Exiting a Thread

                Hi Maloney,

                I have a sample at the following location. In this sample, a form starts
                several threads and then waits for them to close before exiting. The
                whitepaper is being prepped for publication so only the code is available
                but it is really simple:



                It essentially waits on a variable and then overrides OnClosing to cancel
                the exit until all threads are closed.

                --
                Geoff Schwab
                Program Manager
                Excell Data Corporation

                http://msdn.microsoft.com/mobility/p...Q/default.aspx

                This posting is provided "AS IS" with no warranties, and confers no rights.
                "Maloney" <sabrogden@soft home.net> wrote in message
                news:%23xiXVEvo DHA.2964@tk2msf tngp13.phx.gbl. ..[color=blue]
                > Exit thread in C#CF. I'm createing a thread and setting a class object[/color]
                for[color=blue]
                > when to exit. Before creating the thread i check if the thread is[/color]
                running,[color=blue]
                > if so set the flag to exit the thread.
                >
                > What is the proper way of waiting for the thread to exit. In C++ i would
                > set an event and WaitForMultiple Object, peek and pump messages until the
                > handle was set.
                >
                > Class test
                > {
                > private bool ExitThread = false
                > private bool ThreadRunning = false
                > private Thread myThread
                >
                > private StartThread()
                > {
                > if(ThreadRunnin g)
                > {
                > ExitThread = true;
                > }
                > myThread = new Thread(new ThreadStart(Fun cThread));
                > }
                >
                > private FuncThread()
                > {
                > ThreadRunning = true;
                > while(more files)
                > {
                > if(ExitThread)
                > break;
                >
                > read files in directory
                > Invoke back to main thread and fill item to list box
                > }
                > ThreadRunning = false;
                > }
                > }
                >
                >[/color]


                Comment

                • William Stacey

                  #9
                  Re: Exiting a Thread

                  You could also probably use a lock to wait. This lock will be seperate from
                  the sync lock above. When enter thread, lock a ref var that can be accessed
                  from manager (i.e. public property.) The manager can wait on the lock.
                  When the thread is done, it exits the lock block and manager will get the
                  lock and do what ever.

                  --
                  William Stacey, MVP

                  "Maloney" <sabrogden@soft home.net> wrote in message
                  news:uSaIcNvoDH A.2424@TK2MSFTN GP10.phx.gbl...[color=blue]
                  > Join is not supported by the CompactFramewor k. Thanks though
                  >
                  > "William Stacey" <staceywREMOVE@ mvps.org> wrote in message
                  > news:ewgwTJvoDH A.2964@tk2msftn gp13.phx.gbl...[color=green]
                  > > Does Join not work for your needs?
                  > >
                  > > --
                  > > William Stacey, MVP
                  > >
                  > > "Maloney" <sabrogden@soft home.net> wrote in message
                  > > news:#xiXVEvoDH A.2964@tk2msftn gp13.phx.gbl...[color=darkred]
                  > > > Exit thread in C#CF. I'm createing a thread and setting a class[/color][/color][/color]
                  object[color=blue][color=green]
                  > > for[color=darkred]
                  > > > when to exit. Before creating the thread i check if the thread is[/color]
                  > > running,[color=darkred]
                  > > > if so set the flag to exit the thread.
                  > > >
                  > > > What is the proper way of waiting for the thread to exit. In C++ i[/color][/color]
                  > would[color=green][color=darkred]
                  > > > set an event and WaitForMultiple Object, peek and pump messages until[/color][/color][/color]
                  the[color=blue][color=green][color=darkred]
                  > > > handle was set.
                  > > >
                  > > > Class test
                  > > > {
                  > > > private bool ExitThread = false
                  > > > private bool ThreadRunning = false
                  > > > private Thread myThread
                  > > >
                  > > > private StartThread()
                  > > > {
                  > > > if(ThreadRunnin g)
                  > > > {
                  > > > ExitThread = true;
                  > > > }
                  > > > myThread = new Thread(new ThreadStart(Fun cThread));
                  > > > }
                  > > >
                  > > > private FuncThread()
                  > > > {
                  > > > ThreadRunning = true;
                  > > > while(more files)
                  > > > {
                  > > > if(ExitThread)
                  > > > break;
                  > > >
                  > > > read files in directory
                  > > > Invoke back to main thread and fill item to list box
                  > > > }
                  > > > ThreadRunning = false;
                  > > > }
                  > > > }
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  Working...