0

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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Nick Williams
  • 1,237
  • 6
  • 19
  • 40
  • 1
    Why is your OnBitChanged() static? – Azhar Khorasany Feb 21 '13 at 15:19
  • @Azhar Because its being called from Main() which is static? Am I being really stupid? I thought I wouldn't be able to call a non static method from a static context? – Nick Williams Feb 21 '13 at 15:44
  • Incidentally I did try creating a BitChangedHelper class which was non-static and contained the code above which is in On_BitChanged. So I created a new BitChangedHelper and called the methodd HandleBit(); – Nick Williams Feb 21 '13 at 16:09

1 Answers1

0

You did not mention which OpcServer component you are using, but the most probable problem is that the DataChange comes into your application in a COM callback thread and you are not allowed to write back to COM while handling the callback. You need to just record the incoming value and do the actual handling in your own handler thread, which then sets the bit back to false in the PLC.

Jouni Aro
  • 2,099
  • 14
  • 30
  • That was my initial thought too. However when the event fires for the first time the bit does get set back to false. i.e. plcListener.Confirm(); runs successfully. Also if the On_BitChanged() method just contains plcListener.Confirm(); everything works fine and the event fires whenever the plc bit becomes true – Nick Williams Feb 21 '13 at 21:12