473,385 Members | 1,647 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,385 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 4755
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.