Ejemplo n.º 1
0
        /// <summary>產生 DataSended 事件</summary>
        /// <param name="buffer">已傳送的資料內容</param>
        /// <param name="remote">遠端端點資訊</param>
        /// <param name="extraInfo">額外資訊</param>
        protected virtual void OnDataSended(byte[] buffer, EndPoint remote, object extraInfo = null)
        {
            if (this.DataSended != null)
            {
                try
                {
                    AsyncUdpEventArgs auea = new AsyncUdpEventArgs(m_UdpSocket.Handle, m_UdpSocket.LocalEndPoint, remote, buffer);
                    auea.SetExtraInfo(extraInfo);
                    switch (this.UseAsyncCallback)
                    {
                    case EventCallbackMode.BeginInvoke:
                        #region 非同步呼叫 - BeginInvoke
                    {
                        foreach (EventHandler <AsyncUdpEventArgs> del in this.DataSended.GetInvocationList())
                        {
                            try { del.BeginInvoke(this, auea, new AsyncCallback(AsyncUdpEventCallback), del); }
                            catch (Exception ex) { Debug.Print(ex.Message); }
                        }
                        break;
                    }

                        #endregion
                    case EventCallbackMode.Invoke:
                        #region  步呼叫 - DynamicInvoke
                    {
                        foreach (Delegate del in this.DataSended.GetInvocationList())
                        {
                            try { del.DynamicInvoke(this, auea); }
                            catch (Exception ex) { Debug.Print(ex.Message); }
                        }
                        break;
                    }

                        #endregion
                    case EventCallbackMode.Thread:
                        #region 建立執行緒 - Thread
                    {
                        foreach (Delegate del in this.DataSended.GetInvocationList())
                        {
                            try
                            {
                                EventThreadVariables etv = new EventThreadVariables()
                                {
                                    InvokeMethod = del, Args = new object[] { this, auea }
                                };
                                ThreadPool.QueueUserWorkItem(new WaitCallback(EventThreadWorker), etv);
                            }
                            catch (Exception ex) { Debug.Print(ex.Message); }
                        }
                        break;
                    }
                        #endregion
                    }
                }
                catch (Exception ex) { Debug.Print(ex.Message); }
            }
        }
Ejemplo n.º 2
0
        /// <summary>產生 MonitorStoped 事件</summary>
        /// <param name="handle">產生事件的 Socket 控制碼</param>
        /// <param name="local">本地端點資訊</param>
        /// <param name="extraInfo">額外資訊</param>
        protected virtual void OnMonitorStoped(IntPtr handle, EndPoint local, object extraInfo = null)
        {
            if (this.MonitorStoped != null)
            {
                AsyncUdpEventArgs auea = new AsyncUdpEventArgs(handle, local, null);
                auea.SetExtraInfo(extraInfo);
                switch (this.UseAsyncCallback)
                {
                case EventCallbackMode.BeginInvoke:
                    #region 非同步作法 - BeginInvoke
                {
                    foreach (EventHandler <AsyncUdpEventArgs> del in this.MonitorStoped.GetInvocationList())
                    {
                        try { del.BeginInvoke(this, auea, new AsyncCallback(AsyncUdpEventCallback), del); }
                        catch (Exception ex) { Debug.Print(ex.Message); }
                    }
                    break;
                }

                    #endregion
                case EventCallbackMode.Invoke:
                    #region  步作法 - DynamicInvoke
                {
                    foreach (Delegate del in this.MonitorStoped.GetInvocationList())
                    {
                        try { del.DynamicInvoke(this, auea); }
                        catch (Exception ex) { Debug.Print(ex.Message); }
                    }
                    break;
                }

                    #endregion
                case EventCallbackMode.Thread:
                    #region 建立執行緒 - Thread
                {
                    foreach (Delegate del in this.MonitorStoped.GetInvocationList())
                    {
                        try
                        {
                            EventThreadVariables etv = new EventThreadVariables()
                            {
                                InvokeMethod = del, Args = new object[] { this, auea }
                            };
                            ThreadPool.QueueUserWorkItem(new WaitCallback(EventThreadWorker), etv);
                        }
                        catch (Exception ex) { Debug.Print(ex.Message); }
                    }
                    break;
                }
                    #endregion
                }
            }
        }