C# Wait untill function is returning XY

Hello Experts,

 I need some help with code execution.
 My code works with packets TPC send / receive from http protocol
 Now what i need is for my code to "pause" or wait until some of the functions are executed
 before X function is fired.
 Now this is my code. First i posted a class with a method which is being called each time I
 receive a packet with a specific header (name).
 public class PacHandler
        static List<MInfo> list = new List<MInfo>();
        public static void HandlePacket(Packet packet, Session session)
                switch (PacketHeader)
                    case "LOCATION":
                        Mon.MList(packet, session, list);


Open in new window

 After that if header name is found i call a method to do something based on a packet i received:
  class Mon

        public static MInfo MList(Packet packet, Session session, List<MInfo> list)
            _UID        = packet.read();
            list.Add(new MInfo() { UID = _UID  });

            List<MInfo> SortedList = list.OrderBy(o => o.ID).ToList();

            var firstItem = SortedList.ElementAt(0);

			//If i received 10 packets code will execute 10 times before HandeInfo is called. After that handle movement is called and executed 10 times.
            if (firstItem.ID <= 20)
             Movement.HandeInfo(session, firstItem.ID));

            return firstItem;


Open in new window

 Ok i hope you know what i need ... i need my program to wait until HandeInfo is completed and returns something back to the calling function.
 I know in other scripting languages there are function wait_until() function returns something ...
 How can i achieve that functionality?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
hmm, I don't get it. Can you rephrase your question and post more context? Cause your methodes signature uses a Packet. Thus you have alread the data you need. So why waiting?
TagyourareitAuthor Commented:

problem is i have to respond to packet received event ... and function MList is keep on rolling without waiting for packet to be sent.

So in short this function  Movement.HandeInfo(session, firstItem.ID)); needs to be finished before next event.
ste5anSenior DeveloperCommented:
What recieved event? Your code doesn't send anything...
Bob LearnedCommented:
There are different approaches:

1) AsyncResult.WaitOne:

var asyncResult = service.BeginSomething();
asyncResult.AsyncWaitHandle.WaitOne(); // Blocks until process is complete

2) Task

using System.Threading.Tasks;


var task = Task<TResult>.Factory.FromAsync(
    service.BeginSomething, service.EndSomething, arg1, arg2, ..., null);

var result = task.Result;

3) Reactive Extensions:


The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators. Using Rx, developers represent asynchronous data streams with Observables

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.