The first problem is because your acquisition and triggering is software timed. With software timed operations, you simply cannot be very deterministic because Windows is not a real time operating system. If you need better determinism, you will have to change your program to incorporate hardware timing and triggering. Additionally, you will need to have a data acquistion board that supports analog triggering. If you only want to acquire and generate data when you input signal is above a certain level, you can use analog scan clock gating and share the analog input scan clock to driver you analog output operation. Below are a couple of links that illustrate how to setup these different operations.
As for the second problem, if you choose to keep your program
as it is, you can simply add some additional logic so the AO portion only executes once when the AI is in the high state. Or, if you rewrite your program according to my suggestion for problem one, you can control when your AO actually updates by sharing the AI sample clock. This way, when the AI is not acquiring, you will not be generating. I hope this information helps.
analog scan clock gating exampleanalog input and analog output