Threading Question

  • Thread starter Thread starter Adam Barker
  • Start date Start date
A

Adam Barker

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
 
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*.
 
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()...?
 
Back
Top