0

I have device that provides some real-time events, the manufacturer give me a dll for use that events and develop programs based on that device. here is some of properties and events of that dll:

    [ClassInterface(0)]
[ComSourceInterfaces("zkemkeeper._IZKEMEvents")]
[Guid("00853A19-BD51-419B-9269-2DABE57EB61F")]
[TypeLibType(2)]
public class CZKEMClass : IZKEM, CZKEM, _IZKEMEvents_Event
{
    public CZKEMClass();

    [DispId(79)]
    public virtual int AccGroup { get; set; }
    [DispId(76)]
    public virtual int BASE64 { get; set; }
    [DispId(2)]
    public virtual int CommPort { get; set; }
    [DispId(64)]
    public virtual int ConvertBIG5 { get; set; }
    [DispId(123)]
    public virtual int MachineNumber { get; set; }
    [DispId(78)]
    public virtual uint PIN2 { get; set; }
    [DispId(102)]
    public virtual int PINWidth { get; }
    [DispId(241)]
    public virtual int PullMode { get; set; }
    [DispId(1)]
    public virtual bool ReadMark { get; set; }
    [DispId(191)]
    public virtual int SSRPin { get; }

    public virtual event _IZKEMEvents_OnAlarmEventHandler OnAlarm;
    public virtual event _IZKEMEvents_OnAttTransactionEventHandler OnAttTransaction;
    public virtual event _IZKEMEvents_OnAttTransactionExEventHandler OnAttTransactionEx;
    public virtual event _IZKEMEvents_OnConnectedEventHandler OnConnected;
    public virtual event _IZKEMEvents_OnDeleteTemplateEventHandler OnDeleteTemplate;
    public virtual event _IZKEMEvents_OnDisConnectedEventHandler OnDisConnected;
    public virtual event _IZKEMEvents_OnDoorEventHandler OnDoor;
    public virtual event _IZKEMEvents_OnEMDataEventHandler OnEMData;
    public virtual event _IZKEMEvents_OnEmptyCardEventHandler OnEmptyCard;
    public virtual event _IZKEMEvents_OnEnrollFingerEventHandler OnEnrollFinger;
    public virtual event _IZKEMEvents_OnEnrollFingerExEventHandler OnEnrollFingerEx;
    public virtual event _IZKEMEvents_OnFingerEventHandler OnFinger;
    public virtual event _IZKEMEvents_OnFingerFeatureEventHandler OnFingerFeature;
    public virtual event _IZKEMEvents_OnHIDNumEventHandler OnHIDNum;
    public virtual event _IZKEMEvents_OnKeyPressEventHandler OnKeyPress;
    public virtual event _IZKEMEvents_OnNewUserEventHandler OnNewUser;
    public virtual event _IZKEMEvents_OnVerifyEventHandler OnVerify;
    public virtual event _IZKEMEvents_OnWriteCardEventHandler OnWriteCard;

they give me a sample code for realtime events like that:

if (this.RegEvent(this.iNo, 65535))//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
        {
            _zkemkeeper.OnFinger += new zkemkeeper._IZKEMEvents_OnFingerEventHandler(axCZKEM1_OnFinger);
            _zkemkeeper.OnVerify += new zkemkeeper._IZKEMEvents_OnVerifyEventHandler(axCZKEM1_OnVerify);
            _zkemkeeper.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
            _zkemkeeper.OnFingerFeature += new zkemkeeper._IZKEMEvents_OnFingerFeatureEventHandler(axCZKEM1_OnFingerFeature);
            _zkemkeeper.OnEnrollFingerEx += new zkemkeeper._IZKEMEvents_OnEnrollFingerExEventHandler(axCZKEM1_OnEnrollFingerEx);
            _zkemkeeper.OnDeleteTemplate += new zkemkeeper._IZKEMEvents_OnDeleteTemplateEventHandler(axCZKEM1_OnDeleteTemplate);
            _zkemkeeper.OnNewUser += new zkemkeeper._IZKEMEvents_OnNewUserEventHandler(axCZKEM1_OnNewUser);
            _zkemkeeper.OnHIDNum += new zkemkeeper._IZKEMEvents_OnHIDNumEventHandler(axCZKEM1_OnHIDNum);
            _zkemkeeper.OnAlarm += new zkemkeeper._IZKEMEvents_OnAlarmEventHandler(axCZKEM1_OnAlarm);
            _zkemkeeper.OnDoor += new zkemkeeper._IZKEMEvents_OnDoorEventHandler(axCZKEM1_OnDoor);
            _zkemkeeper.OnWriteCard += new zkemkeeper._IZKEMEvents_OnWriteCardEventHandler(axCZKEM1_OnWriteCard);
            _zkemkeeper.OnEmptyCard += new zkemkeeper._IZKEMEvents_OnEmptyCardEventHandler(axCZKEM1_OnEmptyCard);
            _zkemkeeper.m_bRegisteredEvents = true;
            uLog("Ends");
        }

i can use this sample in my single-thread or a windows applications and it works and properly triggered that real-time events, but when i use it on multi-thread apps or in a service layer like a basic wcf service it doesn't work! please help me

HamidEbr
  • 405
  • 1
  • 7
  • 19

3 Answers3

0

What does not work?

Typically a real time update will be provided on an IO thread. For a Windows application you will have to marshall the call onto a UI thread if you wish to interact with a UI thread affine element.

0

Calling events like this will not work because events raised inside a thread will only trigger subscribers in that particular thread. You need to use events that can signal other threads instead.

I recommend reading through this question board on Microsoft's website: https://social.msdn.microsoft.com/Forums/en-US/13f30e33-7f61-498e-a91a-ef982a63453c/event-handling-in-multithreaded-apps?forum=netfxbcl

The answers give detailed information and on how to create events that can signal other threads efficiently.

Kathryn Hazuka
  • 216
  • 1
  • 7
  • thanks for your help, but i cant edit dll codes that gives me those events – HamidEbr Apr 25 '16 at 08:26
  • @HamidSOS I don't think i understand what you mean. You shouldn't need to edit any additional .dll files to implement cross thread events. What exactly are you trying to edit, and in what .dll ? – Kathryn Hazuka Apr 25 '16 at 08:30
  • i don't need to edit that dll, all i need is use those real-time events properly in a multi-thread or wcf apps – HamidEbr Apr 25 '16 at 08:33
  • i also read this q & a http://stackoverflow.com/questions/6185610/how-to-handle-com-events-from-a-console-application – HamidEbr Apr 25 '16 at 08:40
  • Ah... I see now that i have misread your initial statements. I don't know enough about that level of operations to help you much here. Sorry! – Kathryn Hazuka Apr 25 '16 at 08:44
0

Create inside STA thread

Thread createComAndMessagePumpThread = new Thread(() =>
{
    //you code here
});
createComAndMessagePumpThread.SetApartmentState(ApartmentState.STA);
createComAndMessagePumpThread.Start();