473,324 Members | 2,257 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,324 software developers and data experts.

make console wait until async method is complete

static void Main(string[] args) { DoSomething(); }

static void DoSomething() {
for (int i=0; i<=10; i++) { CallAsyncMethod(); }
}

my problem is when i run the app console exists without really completing
DoSomething()

if i add 'Console.ReadLine() to Main() then console waits until
DoSomething() is complete

how do i make the app exist only after DoSomething() is complete?
Jul 21 '05 #1
3 4748
I think you will have to show more details of CallAsyncMethod().
How are you making the asynchronous calls? If you are creating your own
threads, you can use Thread.Join().
You can also set the threads so that their IsBackground property is
false - this will prevent the app from ending before those threads complete.

Joshua Flanagan

mvdevnull wrote:
static void Main(string[] args) { DoSomething(); }

static void DoSomething() {
for (int i=0; i<=10; i++) { CallAsyncMethod(); }
}

my problem is when i run the app console exists without really completing
DoSomething()

if i add 'Console.ReadLine() to Main() then console waits until
DoSomething() is complete

how do i make the app exist only after DoSomething() is complete?

Jul 21 '05 #2
....
private delegate bool InfoFetcher(int number);

private static void AfterInfoFetch (IAsyncResult result)
{
AsyncResult async = (AsyncResult) result;
InfoFetcher fetcher = (InfoFetcher) async.AsyncDelegate;
Console.WriteLine ("{0}", fetcher.EndInvoke(result));
}
....

for (int i=0; i<=10; i++) {
InfoFetcher f = new InfoFetcher (GetInfo);
f.BeginInvoke(i, new AsyncCallback (AfterInfoFetch), "state");
}

GetInfo(int number) { ...uses Process.Start() to call cmd.exe and do some
stuff... }

"Joshua Flanagan" wrote:
I think you will have to show more details of CallAsyncMethod().
How are you making the asynchronous calls? If you are creating your own
threads, you can use Thread.Join().
You can also set the threads so that their IsBackground property is
false - this will prevent the app from ending before those threads complete.

Joshua Flanagan

mvdevnull wrote:
static void Main(string[] args) { DoSomething(); }

static void DoSomething() {
for (int i=0; i<=10; i++) { CallAsyncMethod(); }
}

my problem is when i run the app console exists without really completing
DoSomething()

if i add 'Console.ReadLine() to Main() then console waits until
DoSomething() is complete

how do i make the app exist only after DoSomething() is complete?

Jul 21 '05 #3
One thing you could do in the Main calling method:

static numRequestsImMaking = 10;
.....
// instead of exiting the method immediately (or Console.ReadLine)
// wait for all results to complete
while (numRequestsImMaking > 0){
System.Threading.Thread.Sleep(500);
}
Then, in your AfterInfoFetch method, call:
Interlocked.Decrement(numRequestsImMaking);
That should work. A better way than polling might be to use a
WaitHandle. Check the .NET Framework docs for the available derived
classes of WaitHandle.
mvdevnull wrote:
...
private delegate bool InfoFetcher(int number);

private static void AfterInfoFetch (IAsyncResult result)
{
AsyncResult async = (AsyncResult) result;
InfoFetcher fetcher = (InfoFetcher) async.AsyncDelegate;
Console.WriteLine ("{0}", fetcher.EndInvoke(result));
}
...

for (int i=0; i<=10; i++) {
InfoFetcher f = new InfoFetcher (GetInfo);
f.BeginInvoke(i, new AsyncCallback (AfterInfoFetch), "state");
}

GetInfo(int number) { ...uses Process.Start() to call cmd.exe and do some
stuff... }

"Joshua Flanagan" wrote:

I think you will have to show more details of CallAsyncMethod().
How are you making the asynchronous calls? If you are creating your own
threads, you can use Thread.Join().
You can also set the threads so that their IsBackground property is
false - this will prevent the app from ending before those threads complete.

Joshua Flanagan

mvdevnull wrote:
static void Main(string[] args) { DoSomething(); }

static void DoSomething() {
for (int i=0; i<=10; i++) { CallAsyncMethod(); }
}

my problem is when i run the app console exists without really completing
DoSomething()

if i add 'Console.ReadLine() to Main() then console waits until
DoSomething() is complete

how do i make the app exist only after DoSomething() is complete?

Jul 21 '05 #4

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

Similar topics

3
by: Chris Tanger | last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can...
6
by: Dawn Minnis | last post by:
Hi (running Win xp and developing using Miracle C. Running applications in windows command prompt) I'm new to the group so be gentle with me. I am currently writing a C program to perform...
1
by: Esteban Felipe | last post by:
Hi, thanks for reading. I hope to find some help here before I commit suicide because this is driving me crazy. Please excuse me if this looks like a long post, but I hope that a complete...
2
by: NBB | last post by:
I have a FileSystemWatcher set and working with the OnFileCreated event. Only thing is, it launches too quickly! I'm copying MP3 files into a directory (the directory that the FSW monitors) and...
1
by: Henrik Dahl | last post by:
Issue: I have a remote service executing notoriously asynchronously which I must be able to use from my Compute_Click(...) event handler. The WebForm I have contains only two controls: A Compute...
3
by: mvdevnull | last post by:
static void Main(string args) { DoSomething(); } static void DoSomething() { for (int i=0; i<=10; i++) { CallAsyncMethod(); } } my problem is when i run the app console exists without really...
16
by: Thirsty Traveler | last post by:
I would like to create a test harness that simulates multiple concurrent users executing an individual thread. I would like this to be determined at runtime when the user specifies the number of...
4
by: Nikolay Unguzov | last post by:
Hi, I want to know how to start two or more async methods, but wait each one to complete, before proceed to the next one. My goal is to do many time-consuming tasks one after other but not block...
0
by: MeGeek | last post by:
Hi, I am developing a Client/Server architecture in .Net Remoting using VC++ .Net I want to make the server thread run at the background until it is killed explicitly.Currently I am doing this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.