I'm just starting to force myself to use Entity Framework (I know I'm a long way behind), but I've fallen at the first hurdle.
The program I'm writing simply watches a PLC for a bit to become true. When it does become true an event fires and then values (random at the moment) will be entered into SQL. I then set the bit to false.
The problem I'm having is that the event fires once, the entries get added, and bit gets set false. However the event never triggers again. If I comment out all of the SQL bits then and just set the bit false then it works perfectly and fires multiple times.
Here is most of the code that I've got.
Any help would be really appreciated.
static void Main(string[] args)
{
PlcListener plcListener = new PlcListener();
plcLister.BitChanged += (bitVal) => On_BitChanged(bitVal, plcListener)
plcListner.Start();
}
private static void On_BitChanged(bool bitVal, PlcListener plcListner)
{
SqlEntities sqlEntity = new SqlEntities();
SampleData sampleData = new SampleData(){ Data = new Random().Next(); };
sqlEntity.AddToSampleDatas(sampleData);
sqlEntity.SaveChanges();
plcListener.Confirm();
}
public class PlcListener
{
public void Start()
{
OPCServer opcServer = new OPCServer();
opcServer.DataChanged += On_DataChanged;
}
public void Confirm()
{
//Code to set bit to false
}
public void On_DataChanged(bool bitVal.......)
{
if(bitVal)
{
BitChangedEventHandler handler = BitChanged;
if (handler != null)
{
handler(bitVal);
}
}
}
public delegate void BitChangedEventHandler(bool bitValue);
public event BitChangedEventHandler BitChanged;
}