By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 1,922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

make console wait until async method is complete

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
....
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.