Threads and winform

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

    Threads and winform

    Hi,
    If I have (hmm, at least I want to have :) ) a winform, displaying the
    progress of a long duration method, which is running in a separate
    thread, how do I implement canceling the thread, if I click a button on
    the form.
    Any directions, examples, etc.

    Thanks
    Sunny
  • 100

    #2
    Re: Threads and winform

    Hi Sunny,

    I would suggest using ManualResetEven t object as a flag for canceling the
    worker thread.
    When you want to cancel the worker thread you can set the event
    (event.Set()) then the worker thread has to periodically check the state of
    the event

    if(event.WaitOn e(0,false))
    {
    //stop the work and exit
    }

    When the thread starts it should reset the event (event.Reset())

    HTH
    B\rgds
    100


    "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
    news:O133eFIpDH A.1960@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hi,
    > If I have (hmm, at least I want to have :) ) a winform, displaying the
    > progress of a long duration method, which is running in a separate
    > thread, how do I implement canceling the thread, if I click a button on
    > the form.
    > Any directions, examples, etc.
    >
    > Thanks
    > Sunny[/color]


    Comment

    • Sunny

      #3
      Re: Threads and winform

      Hi 100,
      I have thought about such a thing, but I needed some working example.
      And the things are a little bit more complicated, 'because I forgot to
      mention, that actually this thread is invoking a remoting method, which
      takes a long time to execute ...

      But, if I see some good example for a non remoting program, I most
      probably will figure out hot to marshal the events, etc.

      Thanks
      Sunny

      In article <u0Vd8GJpDHA.36 88@TK2MSFTNGP11 .phx.gbl>, 100@100.com says...[color=blue]
      > Hi Sunny,
      >
      > I would suggest using ManualResetEven t object as a flag for canceling the
      > worker thread.
      > When you want to cancel the worker thread you can set the event
      > (event.Set()) then the worker thread has to periodically check the state of
      > the event
      >
      > if(event.WaitOn e(0,false))
      > {
      > //stop the work and exit
      > }
      >
      > When the thread starts it should reset the event (event.Reset())
      >
      > HTH
      > B\rgds
      > 100
      >[/color]

      Comment

      • Jeffrey Tan[MSFT]

        #4
        Re: Threads and winform


        Hi Sunny,

        I think 100 has already provided you a clear way.
        You only need follow his way to write a sample:

        static AutoResetEvent myResetEvent = new AutoResetEvent( false);
        private void button2_Click(o bject sender, System.EventArg s e)
        {
        myResetEvent.Se t();
        }

        private void button1_Click(o bject sender, System.EventArg s e)
        {
        Thread t=new Thread(new ThreadStart(lon gwork));
        t.Start();
        }

        void longwork()
        {
        while(true)
        {
        if(myResetEvent .WaitOne(0,fals e))
        {
        Console.WriteLi ne("quit");
        return;
        }
        Console.WriteLi ne("running!") ;
        }
        }

        In project setting, change the project running as console application, then
        you can see the console output of this application.

        Hope this helps,

        Best regards,
        Jeffrey Tan
        Microsoft Online Partner Support
        Get Secure! - www.microsoft.com/security
        This posting is provided "as is" with no warranties and confers no rights.

        --------------------
        | From: Sunny <sunnyask@icebe rgwireless.com>
        | Subject: Re: Threads and winform
        | Date: Thu, 6 Nov 2003 14:11:38 -0600
        | References: <O133eFIpDHA.19 60@TK2MSFTNGP12 .phx.gbl>
        <u0Vd8GJpDHA.36 88@TK2MSFTNGP11 .phx.gbl>
        | Organization: Iceberg Wireless LLC
        | MIME-Version: 1.0
        | Content-Type: text/plain; charset="iso-8859-15"
        | Content-Transfer-Encoding: 7bit
        | X-Newsreader: MicroPlanet Gravity v2.60
        | Message-ID: <eucixIKpDHA.20 00@TK2MSFTNGP12 .phx.gbl>
        | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        | NNTP-Posting-Host: 216.17.90.91
        | Lines: 1
        | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
        | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1972 89
        | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
        |
        | Hi 100,
        | I have thought about such a thing, but I needed some working example.
        | And the things are a little bit more complicated, 'because I forgot to
        | mention, that actually this thread is invoking a remoting method, which
        | takes a long time to execute ...
        |
        | But, if I see some good example for a non remoting program, I most
        | probably will figure out hot to marshal the events, etc.
        |
        | Thanks
        | Sunny
        |
        | In article <u0Vd8GJpDHA.36 88@TK2MSFTNGP11 .phx.gbl>, 100@100.com says...
        | > Hi Sunny,
        | >
        | > I would suggest using ManualResetEven t object as a flag for canceling
        the
        | > worker thread.
        | > When you want to cancel the worker thread you can set the event
        | > (event.Set()) then the worker thread has to periodically check the
        state of
        | > the event
        | >
        | > if(event.WaitOn e(0,false))
        | > {
        | > //stop the work and exit
        | > }
        | >
        | > When the thread starts it should reset the event (event.Reset())
        | >
        | > HTH
        | > B\rgds
        | > 100
        | >
        |

        Comment

        • Miha Markic

          #5
          Re: Threads and winform

          You might use Thread.Abort method and catch ThreadAbortExce ption within
          thread.

          Miha

          "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
          news:O133eFIpDH A.1960@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Hi,
          > If I have (hmm, at least I want to have :) ) a winform, displaying the
          > progress of a long duration method, which is running in a separate
          > thread, how do I implement canceling the thread, if I click a button on
          > the form.
          > Any directions, examples, etc.
          >
          > Thanks
          > Sunny[/color]


          Comment

          • Sunny

            #6
            Re: Threads and winform

            Thank you all for the posts

            Sunny

            Comment

            Working...