I just wrote a simple C# to get an eventcallback from PCI-7250 (Data Acquisition Card) when any of the digital inputs go high. Here is my code:
public delegate void ReadDelegate(uint value)
public void Form1_Load(object sender, EventArgs e)
{
m_dev = DASK.Register_Card(DASK.PCI_7250,0);
ReadDelegate ReadNow = new ReadDelegate(FunctionToCall)
DASK.DI_EventCallBack((ushort)m_dev,1,(short)DASK.DBEvent,ReadNow)
}
private void FunctionToCall(uint int_value)
{
MessageBox.Show(int_value)
}
When run it just keep"s up throwing some random numbers during runtime and then finally crashes. I believe it has something to do with the EventType (DASK.DBEvent). I went through the manual but nothing more is mentioned about the DASK.DBEvent.
Kindly please advise.
Since the device doesn't have support for callbacks in its driver, you could adapt the driver's API from synchronous calls to callbacks by polling the device in a background thread.
First, create a class that polls the device for the physical event you're interested in responding to. Then, in your GUI code, put the polling work in the background and respond to the callback in the main thread.
I'm not familiar with the ADLink driver, so I'll just summarize a design approach and sketch some pseudocode that isn't threadsafe. This is a naive approach to using Tasks, but you could also update it to use continuations or async/await. Or, if you need more than one responder, make the callback raise an event that your other classes can subscribe to.
Polling class
WinForm class