473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Asynchronous Process Question

I have a process that takes a while to run so I have it running
asynchronously so that the user can continue working. My question is that I
want to killl the process if the user changes the search parameters before
the callback method is called. Any ideas of how to do this or if it even
matters? I did not know if calling the asynch process a new time kills the
previous one or not.

Thank you for your help.
Susan
Aug 2 '06 #1
10 2155

Susan wrote:
I have a process that takes a while to run so I have it running
asynchronously so that the user can continue working. My question is that I
want to killl the process if the user changes the search parameters before
the callback method is called. Any ideas of how to do this or if it even
matters? I did not know if calling the asynch process a new time kills the
previous one or not.

Thank you for your help.
Susan
Hi Susan,
Can you post some code? It seems like you can just kill the process
using the object reference that you have:

static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = @"C:\WINDOWS\system32\notepad.exe";
p.Start();

Console.WriteLine("press enter to kill process");
Console.ReadLine();
p.Kill();
return;
}

Hope this helps,
John

Aug 2 '06 #2
Hello Susan,

Theoretically you can dive into message sing objects that are performing
async calls where is the cancellation located
but practically it's very hard to do, you need to intercept calls, context
and etc.

standarc acyns handler classes don't have functionality to cancell async call

SI have a process that takes a while to run so I have it running
Sasynchronously so that the user can continue working. My question is
Sthat I want to killl the process if the user changes the search
Sparameters before the callback method is called. Any ideas of how to
Sdo this or if it even matters? I did not know if calling the asynch
Sprocess a new time kills the previous one or not.
S>
SThank you for your help.
SSusan
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Aug 2 '06 #3
I do not think that will work. Here's code snippets:
private CountDelegate dDeleg;

private void GetResults ()
{
/*get the actual results from database, but in limited number (25 per
page) to
increase response time*/

if (_newQuery)
{
cDeleg = new CountDelegate(Count);
AsyncCallback cb = new AsyncCallback (CountCallback);
cDeleg.BeginInvoke (tgts, cb, "countDelegate");
}
}

private void CountCallback (IAsyncResult ar)
{
cDeleg.EndInvoke (ar);
SetCount(); //just something letting the user know how many results there
are
}

I didn't know if there was a way to force the end invoke even though the
process is not completed. I know that there is not cancel even, but am hoping
there is a way to similate one... Any ideas?

Thank you,
Susan

"John Duval" wrote:
>
Susan wrote:
I have a process that takes a while to run so I have it running
asynchronously so that the user can continue working. My question is that I
want to killl the process if the user changes the search parameters before
the callback method is called. Any ideas of how to do this or if it even
matters? I did not know if calling the asynch process a new time kills the
previous one or not.

Thank you for your help.
Susan

Hi Susan,
Can you post some code? It seems like you can just kill the process
using the object reference that you have:

static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = @"C:\WINDOWS\system32\notepad.exe";
p.Start();

Console.WriteLine("press enter to kill process");
Console.ReadLine();
p.Kill();
return;
}

Hope this helps,
John

Aug 2 '06 #4
Darn. I was hoping there was an easy work around. I guess I'll change it to a
thread then.... Thank you. :)

"Michael Nemtsev" wrote:
Hello Susan,

Theoretically you can dive into message sing objects that are performing
async calls where is the cancellation located
but practically it's very hard to do, you need to intercept calls, context
and etc.

standarc acyns handler classes don't have functionality to cancell async call

SI have a process that takes a while to run so I have it running
Sasynchronously so that the user can continue working. My question is
Sthat I want to killl the process if the user changes the search
Sparameters before the callback method is called. Any ideas of how to
Sdo this or if it even matters? I did not know if calling the asynch
Sprocess a new time kills the previous one or not.
S>
SThank you for your help.
SSusan
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Aug 2 '06 #5
Hi Susan,

(sorry about the bad spelling... its late, and I am not a native
english speaker ;) )

Fact of the matter is this: You are now doing multithreaded
programming. The best way to go about it is to embrace the fact and now
be aware of the issues inherent in the paradigm.

what you should do (for some undefined value of "should") is keep 2
booleans around that keeps track of whether the async process should
stop or not, and whether it is running or not.

These 2 bools should both be locked in the standard way. Here I will
show you how:

private readonly object mSomeBooleanLock = new object();
private bool mLockedSomeBool = false; // some sane default value here

private bool SomeBoolValue()
{
bool result;
lock(mSomeBooleanLock)
{
result = mLockedSomeBool;
}
return result;
}

private void SetSomeBoolValue(bool pNewValue)
{
lock(mSomeBooleanLock)
{
mLockedSomeBool = pNewValue;
}
}
btw - put all of this in a new class that will "host" your background
process. Then make a new instance for every new search.

Now: In this class, have methods StartBackgroundJob() and EndJob().

Startjob should look something like this:
private void StartBackgroundActual(object[] pParams)
{
SetMustStop(false);
SetIsRunning(true);

bool workIsFinished = false;

while(!MustStop() || workIsFinished)
{
// do some quantum of work here
// use the pParams here to tune your search
// sowe workIsFinished = true sometime
}

if(workIsFinished)
{
// provide feedback with the results in some way
// remember this will call on a thread different from your main
ui thread
// handle with care
}

SetIsRunning(false);
}

delegate void VoidMethod();
public void StartBackgroundJob(object[] pParams)
{
VoidMethod asyncCall = delegate()
{
StartBackgroundActual(pParams);
};
asyncCall.BeginInvoke(
1,
delegate(IAsyncResult pAsyncResult)
{
asyncCall.EndInvoke(pAsyncResult);
},
null);
}

public void Endjob(bool pMustBlock)
{
SetMustStop(true);
if (pMustBlock)
{
while(IsRunning())
{
Thread.Sleep(350); // some random value... don't set it to
some power of 2. cannot remember why exactly anymore
}
}
}

Everytime that your user changes his search parameters, set the old
background job (of which you kept a handle around) to stop. Then
instantiate a new instance, set it to start running with the new
parameters received from the user.

I would seriously suggest that you read this piece:
http://www.yoda.arachsys.com/csharp/threads/index.shtml

It is the canonical work on multi-threading (free and available online)

The other piece of advice that helped me when i learned to thread was
this: investigate anonymous methods with the aim of using them for
closures. The technique helps THIIIIIIIIIIS much when you are coding to
different threads.

Regards,
Pieter

Susan wrote:
I do not think that will work. Here's code snippets:
private CountDelegate dDeleg;

private void GetResults ()
{
/*get the actual results from database, but in limited number (25 per
page) to
increase response time*/

if (_newQuery)
{
cDeleg = new CountDelegate(Count);
AsyncCallback cb = new AsyncCallback (CountCallback);
cDeleg.BeginInvoke (tgts, cb, "countDelegate");
}
}

private void CountCallback (IAsyncResult ar)
{
cDeleg.EndInvoke (ar);
SetCount(); //just something letting the user know how many results there
are
}

I didn't know if there was a way to force the end invoke even though the
process is not completed. I know that there is not cancel even, but am hoping
there is a way to similate one... Any ideas?

Thank you,
Susan

"John Duval" wrote:

Susan wrote:
I have a process that takes a while to run so I have it running
asynchronously so that the user can continue working. My question is that I
want to killl the process if the user changes the search parameters before
the callback method is called. Any ideas of how to do this or if it even
matters? I did not know if calling the asynch process a new time kills the
previous one or not.
>
Thank you for your help.
Susan
Hi Susan,
Can you post some code? It seems like you can just kill the process
using the object reference that you have:

static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = @"C:\WINDOWS\system32\notepad.exe";
p.Start();

Console.WriteLine("press enter to kill process");
Console.ReadLine();
p.Kill();
return;
}

Hope this helps,
John
Aug 2 '06 #6
"Susan" <Su***@discussions.microsoft.comwrote in message
news:2C**********************************@microsof t.com...
Darn. I was hoping there was an easy work around. I guess I'll change it
to a
thread then.... Thank you. :)
It's no easier using a Thread of your own than doing work in a method
invoked by an async delegate. The reason it's no easier - you DON'T want to
use Thread.Abort() to kill your worker thread. Rather, in either case (your
thread or async delegate), you need to send a "message" to your long-running
operation requesting that it stop. That "message" is typically just a
shared bool variable (as the excellent response by Pieter Breed outlines),
but it could also be a window message, a post to a Win32 Event or Semaphore,
or whatever is appropriate for the task at hand. Your long-running
operation then cooperatively stops when it's asked to, and sends a "message"
back to the requestor acknowledging the stop request.

-cd
Aug 2 '06 #7
Hi Susan,
I see what you're asking now... I was thrown off when you said you were
starting a 'process' asynchronously. :)

I agree with what the others have posted -- have the long-running
operation periodically check to see if it should stop. This can done
by a shared boolean flag, signalling an event, etc...

John

Susan wrote:
I do not think that will work. Here's code snippets:
private CountDelegate dDeleg;

private void GetResults ()
{
/*get the actual results from database, but in limited number (25 per
page) to
increase response time*/

if (_newQuery)
{
cDeleg = new CountDelegate(Count);
AsyncCallback cb = new AsyncCallback (CountCallback);
cDeleg.BeginInvoke (tgts, cb, "countDelegate");
}
}

private void CountCallback (IAsyncResult ar)
{
cDeleg.EndInvoke (ar);
SetCount(); //just something letting the user know how many results there
are
}

I didn't know if there was a way to force the end invoke even though the
process is not completed. I know that there is not cancel even, but am hoping
there is a way to similate one... Any ideas?

Thank you,
Susan

"John Duval" wrote:

Susan wrote:
I have a process that takes a while to run so I have it running
asynchronously so that the user can continue working. My question is that I
want to killl the process if the user changes the search parameters before
the callback method is called. Any ideas of how to do this or if it even
matters? I did not know if calling the asynch process a new time kills the
previous one or not.
>
Thank you for your help.
Susan
Hi Susan,
Can you post some code? It seems like you can just kill the process
using the object reference that you have:

static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = @"C:\WINDOWS\system32\notepad.exe";
p.Start();

Console.WriteLine("press enter to kill process");
Console.ReadLine();
p.Kill();
return;
}

Hope this helps,
John
Aug 3 '06 #8
Since the same process, but with different parameters would be started
causing the need to stop the previous process. Basically, the user would
click on one item to get the results and then change their mind and click on
another item. How do I differentiate or make sure the one is stopped before I
start the next one? The part of the process that takes the time is waiting
for the result back from the database. Do I just do a while not complete (or
back from the database at least) wait to send the second set of info? The
chance is very small that the user would change their mind quickly enough for
the database to not have returned, but do not want to take chances if you
know what I mean. :)

Thank you,
Susan

"Carl Daniel [VC++ MVP]" wrote:

It's no easier using a Thread of your own than doing work in a method
invoked by an async delegate. The reason it's no easier - you DON'T want to
use Thread.Abort() to kill your worker thread. Rather, in either case (your
thread or async delegate), you need to send a "message" to your long-running
operation requesting that it stop. That "message" is typically just a
shared bool variable (as the excellent response by Pieter Breed outlines),
but it could also be a window message, a post to a Win32 Event or Semaphore,
or whatever is appropriate for the task at hand. Your long-running
operation then cooperatively stops when it's asked to, and sends a "message"
back to the requestor acknowledging the stop request.

-cd
Aug 3 '06 #9

Hi Susan,

Actually you don't /really/ need to stop the old database query at all.
It can simply finish processing and return to a place where nobody is
interested in its results anymore.

As long as you represent your queries as seperate class instances, and
keep a reference only to the last query (and as long as those threads
actually do finish!) you can simply keep on waiting for the last query
to finish.

If you are talking about a db query, there isn't really any efficient
way in which you can interrupt the query, since the query is running on
another physical machine. (Contrast this to a filesystem search which
is local and slow) If the queries are as quick as you say, then simply
letting them finish won't really cost you much either.

Hope this helps,
Pieter

Susan wrote:
Since the same process, but with different parameters would be started
causing the need to stop the previous process. Basically, the user would
click on one item to get the results and then change their mind and click on
another item. How do I differentiate or make sure the one is stopped before I
start the next one? The part of the process that takes the time is waiting
for the result back from the database. Do I just do a while not complete (or
back from the database at least) wait to send the second set of info? The
chance is very small that the user would change their mind quickly enough for
the database to not have returned, but do not want to take chances if you
know what I mean. :)

Thank you,
Susan
Aug 4 '06 #10
Kalyan <Ka****@discussions.microsoft.comwrote:
I realize i am replying to a pretty old discussion thread. But i am facing
an asynchronous issue and of all the replies in the discussion board, i felt
yours was the most sensible one. I thought maybe you can give me some
pointers for my issue.

I have to make multiple calls (about 400K) to a webservice which returns a
string. And currently it takes about a week to make all the calls. Instead of
waiting for the webservice result before i make the next call, I rather want
to make the calls and let the results comeback at its own pace. I used
Asynchronous calling and callback method, but it does not seems to work. I am
sure, asynchronous way will improve my program execution exponentially. I
would appreciate if someone can help me with this. And by the way, i did not
see an Begin and End methods.
The proxy generated for the web service should have the BeginXXX and
EndXXX methods. However, if you have problems with that sort of
operation you could just use the ThreadPool and QueueUserWorkItem, then
make the call synchronously there.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 12 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Noel | last post by:
Hi, I am a tad confused about if there are any benefits from using asynchronous vs synchronous network communication. As my example, I have been writing a dns lookup stack with a network...
13
by: ALI-R | last post by:
Hi All, When we say events are processed asynchronously in for instance Sharepoint ,what dose it mean? Thanks for your help. Ali
0
by: Prodip Saha | last post by:
http://www.aspnet4you.com/Articles.aspx?ArticleID=5017 Asynchronous Data Access in ASP.NET (Live demo) Author: Saha,Prodip Posted:5/15/2005 6:03:17 PM Web Architecture is becoming...
1
by: Alexander Kaplunov | last post by:
I have two different scenarios: 1. I have an application that calls a web service and passes an object. Web Service takes that object and does some stuff with it, which could take...
2
by: don_spam | last post by:
Hi all, sorry if this is an over simple question but google isn't coming through for me. So I have a class that processes the data in an excel file, entering the info row by row into our...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
3
by: Varangian | last post by:
Hi.... I have a question about AddOnPreRenderCompleteAsynch....... my question is what is the difference between the first one and PageAsynchTask and RegisterAsynchTask? and when to use...
2
by: Varangian | last post by:
my question is when to use Asynchronous Pages and when to not? Considering their advantage of not locking the UI thread for a particular process whether long or not, I might think that it is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.