I am not connecting to sql server. I am sending xml messages over tcp/
ip to a server.
so it would be BeginRead in my situation.
What i am not able to picture is how an async operation in my last
tier(communication tier) going to function?
Maybe thats why i took the easy route of starting the thread in UI.
You said that there is no way to cancel the operation to the server,
but
there is a way to execute the query asynchronously. You can use the
BeginExecute methods on SqlCommand to execute the queries asynchronously,
there is no need for the BackgroundWorker (IMO).
Basically, if you cancel, then just ignore the callback to the
asynchronous method when it returns (you would have a flag that the
callback
would check to see if the operation was cancelled or not).
Q2) Also, if i use a flag (which assume would be set by the UI) ,would
it create coupling between the UI and the last tier?
I am not trying to be JA.

just trying to stick to my architecture.
After talking to my boss, I found out that even if cancel the thread,
the query will still execute on server.So I might have to go option
2(canceling thread and ignore).
But i still would like to know
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
On Jun 3, 2:19 pm, "Nicholas Paldino [.NET/C# MVP]"
parez,
Well, tons. Does the mechanism to call the server offer a way to
cancel
it? If there is, then this implies that there is an asynchronous
method
for
calling the server, and you should be taking advantage of that, and
not
using the BackgroundWorker (it's not giving you anything here).
No there is no way to call it off.(as is) The communication layers is
blocking on a read.
If it doesn't have a way to be called asynchronously, then I would
use
the BackgroundWorker (or some other asynchronous mechanism) and then
just
ignore it if you want to cancel the operation.
I thought about it.Since this is a winforms app(and not a web app) , i
thought one extra thread living in the wild shouldnt be a big
problem. But as you correctly guessed I need to cancel the request
also on the server.
The thing is, are you doing anything that you need to cancel on
the
server as well? If so, you are going to have to roll back the change
on
the
server, or tell the server to stop somehow.
There are no rolls backs needed. Just need to make sure that SQL
query, which is taking sometime to execute, stops.
I am connecting to the server over TCP/IP
I start a BackGroundWorker to populate a grid. It is started off in
the ui layer
The thread follows( cannot think of a better word) the path
UI->Layer1->Layer2->Communication Layer
and it blocks (the server is executing somthing where which takes
time)
How do i cancel this background worker?
Is there anyway i can access the thread object used by the
backgroundworker and then stop it?
if i can is there anything wroong with it?