Threading Question

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

    #1

    Threading Question

    Hi guys

    Not sure if my approach to threading in my app is causing more problems than
    it should.

    Let's say I have a form with four buttons on it. Each one goes away and does
    something extensive (data retrieval from the web) and displays a result on
    the form.

    Whats the best way to code this, bearing in mind that if you click another
    button while the first one is processing, it needs to abort the first and
    start the second.

    Should I use one thread object? Or continually create new threads for each
    click?

    Thanks in advance

    Adam


  • Justin Rogers

    #2
    Re: Threading Question

    Depending on what you are actually doing some different schemes will come to
    mind...

    First, if you are doing any sort of web service calls then you should know
    that web service proxies have built in Async support you can use. From each
    button you could easily identify if the web service was in the process of
    being called, and then terminate and restart a new asynchronous operation
    without ever having to touch threads since the proxy handles all of that for
    you.

    If you really want to use threads, then I'd suggest using a single thread
    (by single thread I actually mean single thread variable in your program,
    this variable will be assigned a new Thread object each time a button is
    pressed) for the work to be done on so that all 4 buttons can terminate/halt
    the active working thread if one exists before restarting (restarting means
    actually creating a new thread and assigning it to the variable that holds
    your active thread) it to do their own work. You have to be careful as to
    what type of work is being done since in the .NET world certain types of
    work can hang threads and make them unusable (as managed threads, unmanaged
    thread calls can break through the mire). You'll need your worker methods
    to call SpinWait() so that there are ample times to break in. If you don't
    think that managed threads can really *die* just look at the documentation
    for the Abort() method that states *usually terminates the thread*.

    --
    Justin Rogers
    DigiTec Web Consultants, LLC.

    "Adam Barker" <adam@q-state.co.NO*SPA Muk> wrote in message
    news:eafk%233Tb DHA.2632@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Hi guys
    >
    > Not sure if my approach to threading in my app is causing more problems[/color]
    than[color=blue]
    > it should.
    >
    > Let's say I have a form with four buttons on it. Each one goes away and[/color]
    does[color=blue]
    > something extensive (data retrieval from the web) and displays a result on
    > the form.
    >
    > Whats the best way to code this, bearing in mind that if you click another
    > button while the first one is processing, it needs to abort the first and
    > start the second.
    >
    > Should I use one thread object? Or continually create new threads for each
    > click?
    >
    > Thanks in advance
    >
    > Adam
    >
    >[/color]


    Comment

    • Adam Barker

      #3
      Re: Threading Question

      Excellent response Justin, thank you.

      Essentially I am using one variable for the active thread now, but I know
      something wierd is going on when I try to terminate - my lack of knowledge
      in threading basically makes me ask "how do i effectively terminate a
      thread?" is it Suspend() or Abort()...?

      "Justin Rogers" <Justin@games4d otnet.com> wrote in message
      news:u%23ppmHUb DHA.2136@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > Depending on what you are actually doing some different schemes will come[/color]
      to[color=blue]
      > mind...
      >
      > First, if you are doing any sort of web service calls then you should know
      > that web service proxies have built in Async support you can use. From[/color]
      each[color=blue]
      > button you could easily identify if the web service was in the process of
      > being called, and then terminate and restart a new asynchronous operation
      > without ever having to touch threads since the proxy handles all of that[/color]
      for[color=blue]
      > you.
      >
      > If you really want to use threads, then I'd suggest using a single thread
      > (by single thread I actually mean single thread variable in your program,
      > this variable will be assigned a new Thread object each time a button is
      > pressed) for the work to be done on so that all 4 buttons can[/color]
      terminate/halt[color=blue]
      > the active working thread if one exists before restarting (restarting[/color]
      means[color=blue]
      > actually creating a new thread and assigning it to the variable that holds
      > your active thread) it to do their own work. You have to be careful as to
      > what type of work is being done since in the .NET world certain types of
      > work can hang threads and make them unusable (as managed threads,[/color]
      unmanaged[color=blue]
      > thread calls can break through the mire). You'll need your worker methods
      > to call SpinWait() so that there are ample times to break in. If you[/color]
      don't[color=blue]
      > think that managed threads can really *die* just look at the documentation
      > for the Abort() method that states *usually terminates the thread*.
      >
      > --
      > Justin Rogers
      > DigiTec Web Consultants, LLC.
      >
      > "Adam Barker" <adam@q-state.co.NO*SPA Muk> wrote in message
      > news:eafk%233Tb DHA.2632@TK2MSF TNGP09.phx.gbl. ..[color=green]
      > > Hi guys
      > >
      > > Not sure if my approach to threading in my app is causing more problems[/color]
      > than[color=green]
      > > it should.
      > >
      > > Let's say I have a form with four buttons on it. Each one goes away and[/color]
      > does[color=green]
      > > something extensive (data retrieval from the web) and displays a result[/color][/color]
      on[color=blue][color=green]
      > > the form.
      > >
      > > Whats the best way to code this, bearing in mind that if you click[/color][/color]
      another[color=blue][color=green]
      > > button while the first one is processing, it needs to abort the first[/color][/color]
      and[color=blue][color=green]
      > > start the second.
      > >
      > > Should I use one thread object? Or continually create new threads for[/color][/color]
      each[color=blue][color=green]
      > > click?
      > >
      > > Thanks in advance
      > >
      > > Adam
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...