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?
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
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...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.