472,805 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 4707
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.