make console wait until async method is complete

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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
 
....
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 said:
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
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?
 
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.

...
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... }

:

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
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?
 
Back
Top