How to have main program wait for data  
Author Message
dkoco





PostPosted: Visual C# General, How to have main program wait for data Top

I have a program that basically does this:

Main()
Start
GetQuotes();
Calculations(from GetQuotes)
Thread.Sleep(3000);
Loop back to start

Sometimes GetQuotes() takes 1-2 seconds, sometimes 5-6 seconds(I am getting streaming stock quotes). If it takes longer than 3 seconds, then my program freezes.

My question is this: How can I get the main program to wait until GetQuotes is completed I have tried Thread.Sleep, Waitone(in the GetQuotes sub). I have an autoreset event in GetQuotes, which I think is correct. Any help would be appreciated.







Visual C#16  
 
 
RizwanSharp





PostPosted: Visual C# General, How to have main program wait for data Top

GetQuotes() does the Async work If yes thats why the control is moved to the next line, If you need to wait try to modify you GetQuotes function to return the control only when it has done its work, You'll not need any Thread.Sleep then.

Give some more description about how these functions are working.

Best regards,

Rizwan



 
 
dkoco





PostPosted: Visual C# General, How to have main program wait for data Top


Yes, Async works.

Here is the GetQuotes() sub:

GetQuotes()
GotData = new AutoResetEvent(false);
new AsyncQuery.DataHandler(TalOnData)<<<<<This gets quotes
GotData.Set()
GotData.WaitOne();


 
 
dkoco





PostPosted: Visual C# General, How to have main program wait for data Top

If you need to wait try to modify you GetQuotes function to return the control only when it has done its work, You'll not need any Thread.Sleep then.

How do I make it wait Thanks.



 
 
RizwanSharp





PostPosted: Visual C# General, How to have main program wait for data Top

There are different solutions.

Basically you are gettign Quotes in a loop, You want that once a loop should complete and work is done only when it should recall from the begining.. Right

1) You can remove Asynchronous stuff from your code (but it'll halt the GUI, it depends on the requirement where you need this. if it has to be used in Windows Service or Console Application then this solution is ok. But even you have to do it within a Windows Application you can use BackgroundWorker componenent to tun this piece of blocking code.

2) You can use your above code + besides using it in a loop create an event which should be raised when GetQuotes completes its work and in that event handler make another call to GetQoutes.......

There may be many other solution if you think.

Best Regards,

Rizwan