Esempio n. 1
0
        internal static void AddMyObjectChanged(EventHandler <ObjectChangedEventArgs> callback,
                                                ref EventHandler <ObjectChangedEventArgs> objectChanged,
                                                object lockObjChangedEvent,
                                                ref RegisteredWaitHandle regObjChangedWaitHandle,
                                                ref AutoResetEvent objChangedEvent,
                                                ref SafeCollabEvent safeObjChangedEvent,
                                                WaitOrTimerCallback ObjectChangedCallback)
        {
            //
            // Register a wait handle if one has not been registered already
            //
            lock (lockObjChangedEvent){
                if (objectChanged == null)
                {
                    objChangedEvent = new AutoResetEvent(false);

                    //
                    // Register callback with a wait handle
                    //

                    regObjChangedWaitHandle = ThreadPool.RegisterWaitForSingleObject(objChangedEvent,       //Event that triggers the callback
                                                                                     ObjectChangedCallback, //callback to be called
                                                                                     null,                  //state to be passed
                                                                                     -1,                    //Timeout - aplicable only for timers
                                                                                     false                  //call us everytime the event is set
                                                                                     );

                    PEER_COLLAB_EVENT_REGISTRATION pcer = new PEER_COLLAB_EVENT_REGISTRATION();
                    pcer.eventType = PeerCollabEventType.MyObjectChanged;
                    pcer.pInstance = IntPtr.Zero;


                    //
                    // Register event with collab
                    //

                    int errorCode = UnsafeCollabNativeMethods.PeerCollabRegisterEvent(
                        objChangedEvent.SafeWaitHandle,
                        1,
                        ref pcer,
                        out safeObjChangedEvent);
                    if (errorCode != 0)
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabRegisterEvent returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_ObjectChangedRegFailed), errorCode);
                    }
                }
                objectChanged += callback;
            }

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "AddObjectChanged() successful.");
        }
Esempio n. 2
0
        internal static void CleanEventVars(ref RegisteredWaitHandle waitHandle,
                                            ref SafeCollabEvent safeEvent,
                                            ref AutoResetEvent firedEvent)
        {
            if (waitHandle != null)
            {
                waitHandle.Unregister(null);
                waitHandle = null;
            }

            if ((safeEvent != null) && (!safeEvent.IsInvalid))
            {
                safeEvent.Dispose();
            }

            if (firedEvent != null)
            {
                firedEvent.Close();
                firedEvent = null;
            }
        }
        internal static void AddMyObjectChanged(EventHandler<ObjectChangedEventArgs> callback,
                                        ref EventHandler<ObjectChangedEventArgs> objectChanged,
                                        object lockObjChangedEvent,
                                        ref RegisteredWaitHandle regObjChangedWaitHandle,
                                        ref AutoResetEvent objChangedEvent,
                                        ref SafeCollabEvent safeObjChangedEvent,
                                        WaitOrTimerCallback ObjectChangedCallback)
        {
            //
            // Register a wait handle if one has not been registered already
            //
            lock (lockObjChangedEvent){
                if (objectChanged == null){

                    objChangedEvent = new AutoResetEvent(false);

                    //
                    // Register callback with a wait handle
                    //

                    regObjChangedWaitHandle = ThreadPool.RegisterWaitForSingleObject(objChangedEvent, //Event that triggers the callback
                                            ObjectChangedCallback, //callback to be called 
                                            null, //state to be passed
                                            -1,   //Timeout - aplicable only for timers
                                            false //call us everytime the event is set
                                            );

                    PEER_COLLAB_EVENT_REGISTRATION pcer = new PEER_COLLAB_EVENT_REGISTRATION();
                    pcer.eventType = PeerCollabEventType.MyObjectChanged;
                    pcer.pInstance = IntPtr.Zero;


                    //
                    // Register event with collab
                    //

                    int errorCode = UnsafeCollabNativeMethods.PeerCollabRegisterEvent(
                                                                        objChangedEvent.SafeWaitHandle,
                                                                        1,
                                                                        ref pcer,
                                                                        out safeObjChangedEvent);
                    if (errorCode != 0){
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabRegisterEvent returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_ObjectChangedRegFailed), errorCode);
                    }
                }
                objectChanged += callback;
            }

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "AddObjectChanged() successful.");

        }
        internal static void CleanEventVars(ref RegisteredWaitHandle waitHandle,
                                            ref SafeCollabEvent safeEvent,
                                            ref AutoResetEvent firedEvent)
        {
            if (waitHandle != null){
                waitHandle.Unregister(null);
                waitHandle = null;
            }

            if ((safeEvent != null) && (!safeEvent.IsInvalid)){
                safeEvent.Dispose();
            }

            if (firedEvent != null){
                firedEvent.Close();
                firedEvent = null;
            }
        }
 internal extern static int PeerCollabGetEventData(SafeCollabEvent hPeerEvent,
                                                   out SafeCollabData ppEventData);
 internal extern static int PeerCollabRegisterEvent(SafeWaitHandle hEvent, UInt32 cEventRegistration,
                                                    ref PEER_COLLAB_EVENT_REGISTRATION pEventRegistrations,
                                                    out SafeCollabEvent phPeerEvent);
 internal extern static int PeerCollabGetEventData(SafeCollabEvent hPeerEvent,
                                                     out SafeCollabData ppEventData);
 internal extern static int PeerCollabRegisterEvent(SafeWaitHandle hEvent, UInt32 cEventRegistration,
                                                     ref PEER_COLLAB_EVENT_REGISTRATION pEventRegistrations,
                                                     out SafeCollabEvent phPeerEvent);