473,811 Members | 3,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ReadLi ne() 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 4784
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.ReadLi ne() 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.AsyncDele gate;
Console.WriteLi ne ("{0}", fetcher.EndInvo ke(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.ReadLi ne() 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 numRequestsImMa king = 10;
.....
// instead of exiting the method immediately (or Console.ReadLin e)
// wait for all results to complete
while (numRequestsImM aking > 0){
System.Threadin g.Thread.Sleep( 500);
}
Then, in your AfterInfoFetch method, call:
Interlocked.Dec rement(numReque stsImMaking);
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.AsyncDele gate;
Console.WriteLi ne ("{0}", fetcher.EndInvo ke(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.ReadLi ne() 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
2498
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 perform the task within "Write". 1-10 different threads may call "Write" simultaneously and continuously, some in a greedy manner. That is to say that some of the threads calling "Write" will take all they can get, while other threads may only call...
6
2825
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 matrix by matrix (mxm) and matrix by vector (mxv) multiplication, so obviously one of my first considerations is to ask the user if they want an mxm or mxv multiplication performed. I have written the code below (this is a working snippet of the...
1
1461
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 explanation help you to help me :=) ....Let's start with some background: ..- I'm building an asp.net application that requires users to upload text files in cvs format with data exported from an AS-400. ..- The files will be something between 800kb...
2
4027
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 seeing as how the files being copied are somewhat large, the OnFileCreated event doesn't wait until the actual copying procedure is finished. Therefore, the stuff I have set to happen at the OnFileCreated event doesn't run, because the file is...
1
1331
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 button and a Result textbox. I may easily start the asynchronous execution in the Compute_Click event handler. The asynchronous service notifies me when it's result is ready, the notification is done by a delegate invoking ResultReady(...). My...
3
286
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 completing DoSomething() if i add 'Console.ReadLine() to Main() then console waits until
16
30077
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 desired threads. When this is kicked off, I would like to wait in the primary thread until all worker threads have completed and time the result... one problem... I can't figure out how to wait for all threads to complete prior to updating my...
4
1548
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 UI. I can use Application.DoEvents(), but is this the only solution? Nikolay Unguzov
0
1000
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 with the help of "Console::Read()" statement. It works fine. But when I trigger the server exe from a TCL script, control stays there.. and it is not executing the following statement in the script...(say i want to make two instances of a server in...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10393
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.