Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


I have a WCF service which will get an object with huge data from database in the form of collection.
I have a UI which has get and cancel buttons .

Get button : make service request and continue with data population with the returned data from service.I start it as below

Thread _workerThread = new Thread(DoExportTreeLoad);
_workerThread.Start();


Cancel : stops the above thread.It does as below

_workerThread.Abort()


My DoExportTreeLoad() has the synchronous servcie call which in turn hits database and gets data.

result = serivce.GetData(criteria);


Problem: When I cancel the operation after requesting the service, the DAL code is still running in the background (getting data from database) where it should not happen.

As soon as i cancel the operation from UI ,the transaction (to get the data ) from database need to be aborted.
It leads to big performance problem.

Please help me with any way to end up the backend task when thread is cancelled.
Thanks in advance.
Posted
Updated 18-Jun-14 2:14am
v6
Comments
SRS(The Coder) 7-Jun-14 17:33pm    
Please try to join the thread to main thread after starting it,

_workerThread.Start();
_workerThread.Join();

Hope this will work for you.
Sreepada1005 9-Jun-14 3:13am    
Thank you for your response.

its not working woth join...and my application gets hanged with this change.
Prasad Khandekar 9-Jun-14 3:46am    
Hello Sreepad,

It will be very nice if you share the code which actually gets the data from WCF service?

Regards,

Plz try below code.
C#
private object m_lock = new object();
private bool m_isRunning = false;
private bool m_isAbortRequested = false;

public void OnButtonClick(object sender, EventArgs e) {
  lock ( m_lock ) {
    if ( m_isRunning ) {
      m_isAbortRequested = true;
    } else {
      m_isAbortRequested = false;
      m_isRunning = true;
      ThreadPool.QueueUserWorkItem(BackgroundMethod);
    }
  }
}

private void BackgroundMethod() {
  try {
    DoRealWork();
  } finally {
    lock (m_lock) {
      m_isRunning = false;
    }
  }
}

private void DoRealWork() {
  ...
  if ( m_isAbortRequested ) {
    return;
  }
}
 
Share this answer
 
Comments
Sreepada1005 9-Jun-14 6:53am    
Thanks Kushal,

I call the service method in DoRealWork()(from your example) where we just return from method by checking isAbortRequested and the wcf service is not yet called.But my problem is I called my WCF service then I am not able to abort the service method.

does it work in this scenario?
Hi,

As much as I understand that's you may required to stop WCF operation after the call from client.

Now if you want such flexibility then you should go for : Asynchronously[^]

Here is a example of Implementation : http://robbincremers.me/2011/12/31/wcf-asynchronous-client-proxy-and-asynchronous-service-operations/[^]

Thanks
Suvabrata
 
Share this answer
 
Comments
Sreepada1005 9-Jun-14 10:45am    
Thanks Suvabrata,

According to context I need to call my service synchronously.Calling the method asynchronously will solve my issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900