Handle asynchronous input synchronously?

Up vote 0 down vote favorite share g+ share fb share tw.

I am working on parsing a fairly complicated data stream generated by a usb device that emulates a keyboard. The easiest way for me to conceptualize and deal with the data would be if I had a method called something like GetNextInputCharacter, and I could do all the parsing in one go without having to do it one piece at a time as I receive input. Unfortunately I won't know how many bytes to expect in the stream until I have parsed into it significantly, and I would rather not wait until the end to parse it anyway.

Is there any mechanism or design pattern that can take asynchronous input from the key event and pass it to my parse method on demand? All I can think of is an IEnumerable that does a busy wait on a FIFO that the key event populates and yields them out one at a time. That seems like a bit of a hack, but maybe it would work.

I just want a way for the parse routine to pretend like the input is already there and take it without knowing that it has to wait for the events. C# events asynchronous link|improve this question asked Jan 24 at 15:33CMP2,391725 99% accept rate.

How about something like this: ... var stream = //Set up stream var data = from dataStream in StreamStuff(stream) select dataStream; ... private IEnumerable StreamStuff(Stream stream) { stream.Open(); while(stream.Read()) { //Do some stuff to your read value yield return yourProcessedData; } stream.Close(); }.

Then the async data from the USB device can just write to the stream. You'd probably have to write your own Stream implementation, but that isn't too hard. This is a common enough pattern by the way- when you use the built-in .net serialization, the deserializers block on reading an input stream which may be coming over a network socket.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions