4

I want to wait for Event when I get data in my C# Project.

When program read some data GetData other program creates an event at the end of read this data (call EventForGetData). So, I need to wait for EventForGetData that finish read.

I wrote this code for this task but believe this code might write more optimal.

        public static bool WaitEvent = true;
        public void EventForGetData(string variable)
        {
            WaitEvent = false;
        }

        public static string ForWaitGetData()
        {
            WaitEvent = true;
            while (WaitEvent)
            {
                System.Threading.Thread.Sleep(5);
                Application.DoEvents();                   
            }
            return Variable;
        }

        public object GetData(){
           // start read data
              ...
           // Wait for finish to read data 
           ForWaitGetData();

         // finish read data
              ...
          return MyObject;
       }

1 Answer 1

1

Try to use Task the first Task is to get your data, at the end do your processing or launch your event: Example

Task task = Task.Factory.StartNew(() =>
            {
              //put you code here the first one 
            }).ContinueWith((rs) =>{
              //Launch your event or anything you want
            });

Note: the code that you will put inside the ContinueWith will be executed after the code that you write in the StartNew.

8
  • Sorry, but I don't know how write it for my Example... Task task = Task.Factory.StartNew(() => { webBrowser.Document.InvokeScript(""); //put you code here the first one }).ContinueWith((rs) => { ScriptInterface.ForWaitGetData(); //Launch your event or anything you want });
    – askeet
    Commented Apr 13, 2016 at 17:19
  • 1
    If I'm fully understanding your code why you didn't organize your code like this pastebin.com/BTw6SZhF as long as you don't use Variable because returning no assigning variable have no sense. Commented Apr 13, 2016 at 21:39
  • Main problem is GetData() and EventForGetData() it's one and the same thread. Because one COM object generates EventForGetData() when I use GetData() . So, when GetData waits for Event , COM never calls function EventForGetData() if I don't use Application.DoEvent() while waiting it in GetData().
    – askeet
    Commented Apr 14, 2016 at 6:17
  • The general sense of code when I'm trying to get the value, but I can't get it until COM object itself does not give a value that must be obtained. That is, solutions to this problem must be implemented mechanism waiting for.
    – askeet
    Commented Apr 14, 2016 at 6:39
  • Thanks for your answer. But it doesn't work. If ForWaitGetData() inserts in Task sector Call function GetData() freezes.
    – askeet
    Commented Apr 14, 2016 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.