Esempio 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); }
            }
        }
Esempio n. 2
0
 private void EventThreadWorker(object o)
 {
     try
     {
         EventThreadVariables etv = (EventThreadVariables)o;
         etv.InvokeMethod.DynamicInvoke(etv.Args);
     }
     catch (Exception ex) { Debug.Print(ex.Message); }
 }
Esempio n. 3
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
                }
            }
        }
Esempio n. 4
0
        /// <summary>產生 CounterChanged 事件</summary>
        /// <param name="sBytes">已發送的 Bytes</param>
        /// <param name="rBytes">已接收的 Bytes</param>
        /// <param name="wBytes">等待發送的 Bytes</param>
        protected virtual void OnCounterChanged(long sBytes, long rBytes, long wBytes)
        {
            if (this.CounterChanged != null)
            {
                DataTransEventArgs dtea = new DataTransEventArgs(sBytes, rBytes, wBytes);
                switch (this.UseAsyncCallback)
                {
                case EventCallbackMode.BeginInvoke:
                    #region 非同步呼叫 - BeginInvoke
                {
                    foreach (EventHandler <DataTransEventArgs> del in this.CounterChanged.GetInvocationList())
                    {
                        try { del.BeginInvoke(this, dtea, new AsyncCallback(TransferCounterCallback), del); }
                        catch (Exception ex) { Debug.Print(ex.Message); }
                    }
                    break;
                }

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

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