views:

51

answers:

2

Hi stackoverflow,

First of all, I wanted to thank the community. You've been of great support lately ! Usually i don't even need to ask the questions because they're already there. Now i have an issue that's not directly related to code but programming itself.

I'm working with a FTDI Chip and C# programming a communication protocol in which a PC application acts like the Master (will send requests) and there is also Slave device who will answer to them, not immediately, maybe a couple of millisecs, but anyway, will take some time. I'm stuck in a conceptual/philosophical code design question.

After sending a request, should I ask right away for an answer (checking also a timeout) or should I constantly monitor the input (BackgroundWorker powered) and raise an event after receiving a data input ? What would you recommend, what is on your experience. What factors should i consider for making my choice ?

I never studied software design of programming itself so i think i lack the basic on this, but this is a personal project i'm working on and sure i'd love some feedback/pointers on this from you guys.

Thanks !

A: 

My preferred solution in this scenario would be to issue the request in async mode (such that you get called back by an event that fires when it completes), and also implement an async time out using standard .Net mechanisms, which calls you back if it appears the slave is unresponsive. This way you just start the request and the timer and then can continue doing more work, without needing any other threads to process results.

You would have to make sure that concurrent time out and response arrival is handled cleanly using a locking mechanism, so that you know for sure whether you are timing out or handling the response.

Try to avoid polling and input monitoring, unless your slave's API does not allow for deterministic generation of response events.

Steve Townsend
I was doing perfect until you said 'using a locking mechanism'. I'll go google and come back. Anyway, the slave doesn't actually raises events but, the FTDI Chip allows a EventWaitHandle subscription that i could use to raise an event on received bytes. What i don't want to do is stay in a loop until WaitEventHandle fires where i'll lose GUI response. Thanks.
Jazz.
@Jazz - a simple way to ensure single-threaded access is to define a 'static object mylock = new object();` in one of your classes and then use `lock(mylock)` around code in your timer and got-response callbacks that should not execute concurrently.
Steve Townsend
when you meant 'using standard .net mechanisms' do you mean system.timers.timer ?
Jazz.
@Jazz. - that seems a good fit, you can use this in non-recurring fashion
Steve Townsend
A: 

Normally i would work with the asynchronous approach on the low level site and maybe put some synchronization mechanism on top of this. Here is some example approach if you get data fragments and you have to put these fragments together to a whole message.

So on the low level site implement a BackgroundWorker that checks constantly for incoming data and raise some kind of event if you got something and put this into the event.

Above this is someone listening to the events of incoming data and puts all this (maybe) fragments into an internal queue. There it checks if it already has a enough data for a complete message, maybe does some error checking, etc. If it has a complete message it will raise an event to send this message to all listeners out there.

On top of this put another class that watches for messages and reacts on them. This class maybe implements some sync mechanism to watch out if an incoming message matches to something that should happened beforehand.

I think this design makes it easier to react on data that comes in when you don't expect something. And when you like to shutdown you don't have to wait for any timeouts to happen (maybe a very small one the low level BackgroundWorker is using to pull the data out of the source that doesn't support an event mechanism).

Oliver
This is exactly what i was planning to do. I think that using this approach allows me to forget about a timeout check, but my main argument here is, how do i know if i'm ready to send another request? I'd need something like an Eventwaithandle to wait for the bgw or a counter for the bgw ?
Jazz.
btw. the low level side does allow event raising. But even so, can i wait for an event (in the same thread) to happen ? afaik, the eventwaithandle only works by subscribing to event on other threads ? (please correct if wrong)
Jazz.