在当前事件结束之前,事件不会触发 [英] Event doesn't trigger until the current event is finished

查看:97
本文介绍了在当前事件结束之前,事件不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用Asterisk.NET库开发一个应用程序,我订阅了一个事件NewStateEvent来接收来自服务器的来电。

现在,我注意到NewState事件不会再次发生,直到最后一个NewStateEvent结束。我假设它在一个线程上工作;只有当最后一个线程完成时才会启动另一个线程...(我不得不在事件中调用表单来使用表单控件)...

是否有任何想法允许mumltiple实例我的NewState事件中的线程??



这是代码结构......

Hi,
I am developing an application using Asterisk.NET Library, and I am subscribed to an event "NewStateEvent" to get the incoming calls from the server..
Now, I noticed that The NewState Event doesnt occur again until the last NewStateEvent is finished. I assume that its working on a single thread; that will start another thread only when the last one is finished... (I had to Invoke form to use form controls in the event)...
Is there any idea to allow mumltiple instances of a thread within my NewState Event ??

This is the code structure...

public void New_State_Event(object sender, Asterisk.NET.Manager.Event.NewStateEvent e)
       {

               if (e.ChannelStateDesc != null && e.Attributes.ContainsKey("connectedlinenum"))
               {
                   switch (e.ChannelStateDesc.ToLower())
                   {
                       case "ringing":
                           if (ApplicationVariables.CurrentExtensions.ContainsValue(e.CallerIdNum))
                           {

                                  this.Invoke(new MethodInvoker(delegate()
                                       {
                                           Label1.Text = "Ringing";
                                       }));
                                   

                           }

                           break;
                       case "up":
                           if (CallingInfo.UniqueId == e.UniqueId)
                           {
                               this.Invoke(new MethodInvoker(delegate()
                                {
                                         Label1.Text = "Line is up";
                                }));
                           }
                           break;

                   }
               }


       }





和实际代码是这个....







and Actual code is this ....


public void New_State_Event(object sender, Asterisk.NET.Manager.Event.NewStateEvent e)
    {

        try
        {

            if (e.ChannelStateDesc != null && e.Attributes.ContainsKey("connectedlinenum"))
            {
                switch (e.ChannelStateDesc.ToLower())
                {
                    case "ringing":
                        if (ApplicationVariables.CurrentExtensions.ContainsValue(e.CallerIdNum))
                        {
                            //  Creating thread for Timer to work
                            //System.Threading.Thread timerthread = new System.Threading.Thread(delegate()
                            //{
                                try
                                {

                                    this.Invoke(new MethodInvoker(delegate()
                                    {
                                        if (this.Opacity != 98) this.Opacity = 98;
                                        if (exw != null && !exw.IsDisposed)
                                        {
                                            if (exw.calo != null && !exw.calo.IsDisposed)
                                                exw.calo.Opacity = 0;
                                            if (exw.cf != null && !exw.cf.IsDisposed)
                                                exw.cf.Opacity = 0;
                                            exw.Opacity = 0;
                                        }

                                        SetPopupLocation();
                                        button8.Visible = true;
                                        button1.Visible = true;
                                        button2.Visible = true;
                                        button4.Visible = false;
                                        linkLabel1.Visible = false;
                                        pictureBox1.Enabled = true;
                                        if (!this.Visible)
                                        {
                                            this.Show();
                                            this.TopMost = true;
                                        }
                                        if (exw != null && exw.Visible)
                                        {
                                            if (LOADSTYLE == NativeClasses.AW_VER_POSITIVE)
                                            {
                                                Taskbar tt = new Taskbar();

                                                exw.Top = 121 + tt.Size.Height;
                                            }
                                            else
                                            {
                                                Rectangle rr = new Rectangle();
                                                rr = Screen.GetWorkingArea(rr);
                                                exw.Top = (rr.Height) - (exw.Height + 121);
                                            }
                                        }

                                        //string callingperson = "", callingnumber = "";
                                        label2.Text = String.Format(Language.incoming_call_for_the_extension, e.CallerIdNum);


                                        if (e.Attributes.ContainsKey("connectedlinename") && e.Attributes.ContainsKey("connectedlinenum"))
                                        {
                                            CallingInfo.linenum = e.Attributes.ContainsKey("connectedlinenum") ? e.Attributes["connectedlinenum"] : " Unknown ";
                                            label1.Text = String.Format(Language.num_is_calling, CallingInfo.linenum, CallingInfo.linenum, "", "");
                                            Application.DoEvents();
                                            if (e.Attributes["connectedlinename"] != "" && e.Attributes["connectedlinename"] != e.Attributes["connectedlinenum"])
                                            {
                                                CallingInfo.linename = e.Attributes["connectedlinename"];
                                                //CallingInfo.linenum = e.Attributes.ContainsKey("connectedlinenum") ? e.Attributes["connectedlinenum"] : " Unknown "; //+ " ] is Calling...";
                                            }
                                            else
                                            {
                                                Type officeType = Type.GetTypeFromProgID("Outlook.Application");
                                                if (officeType == null || ApplicationVariables.look_for_contact_in_outlook == false)
                                                {
                                                    if (ApplicationVariables.use_local_phone_book == true)
                                                        CallingInfo.linename = GetPhoneContact(e.Attributes["connectedlinenum"]);
                                                    else
                                                        CallingInfo.linename = "Unknown";
                                                }
                                                else
                                                {
                                                    CallingInfo.linename = OutlookData.SearchContact(OutlookData.OutlookContactList, e.Attributes["connectedlinenum"]);
                                                    if (CallingInfo.linename == "Unknown")
                                                        CallingInfo.linename = GetPhoneContact(e.Attributes["connectedlinenum"]);
                                                }
                                                //CallingInfo.linenum = e.Attributes.ContainsKey("connectedlinenum") ? e.Attributes["connectedlinenum"] : " Unknown ";

                                            }
                                        }

                                        label1.Text = String.Format(Language.num_is_calling, CallingInfo.linename, CallingInfo.linenum,"","");
                                        CallingInfo.channel = e.Channel;
                                        CallingInfo.CalledExten = e.CallerIdNum;
                                        CallingInfo.UniqueId = e.UniqueId;
                                        CallingInfo.CallTaken = false;
                                        Status_counter = 0;
                                        timer_autohangup.Enabled = true;
                                        CallerInfo.SetCallInfo(CallingInfo.linename, CallingInfo.linenum, e.CallerIdNum, 0, DateTime.Now, e.UniqueId);
                                        SendStatustoXMPP("Phone  Ringing...");
                                    }));
                                }
                                catch (Exception ex)
                                {
                                    DebugLogger.LogRecord(ex.Message + " [ Function: " + System.Reflection.MethodBase.GetCurrentMethod().Name + ", Thread, Class: ContactList ]");
                                }
                            //});
                            //timerthread.Start();
                        }

                        break;
                    case "up":
                        if (CallingInfo.UniqueId == e.UniqueId)
                        {
                            this.Invoke(new MethodInvoker(delegate()
                             {
                                 CallingInfo.CallTaken = true;
                                 button8.Visible = false;
                                  button1.Visible = true ;
                                 button2.Visible = false;
                                 button4.Visible = true;
                                 linkLabel1.Visible = false;
                                 pictureBox1.Enabled = false ;
                                 timer_autohangup.Enabled = false;
                                 //string callingperson = "", callingnumber = "";
                                 label2.Text = String.Format(Language.talking, e.CallerIdNum);
                                 //if (e.Attributes.ContainsKey("connectedlinename") && e.Attributes.ContainsKey("connectedlinenum"))
                                 //    if (e.Attributes["connectedlinename"] != "" && e.Attributes["connectedlinename"] != e.Attributes["connectedlinenum"])
                                 //    {
                                 //        CallingInfo.linename = e.Attributes["connectedlinename"];
                                 //        CallingInfo.linenum = e.Attributes.ContainsKey("connectedlinenum") ? e.Attributes["connectedlinenum"] : " Unknown "; //+ " ] is Calling...";
                                 //    }
                                 //    else
                                 //    {
                                 //        Type officeType = Type.GetTypeFromProgID("Outlook.Application");
                                 //        if (officeType == null)
                                 //        {
                                 //            CallingInfo.linename = GetPhoneContact(e.Attributes["connectedlinenum"]);
                                 //        }
                                 //        else
                                 //        {
                                 //            CallingInfo.linename = OutlookData.SearchContact(OutlookData.OutlookContactList, e.Attributes["connectedlinenum"]);
                                 //            if (CallingInfo.linename == "Unknown")
                                 //                CallingInfo.linename = GetPhoneContact(e.Attributes["connectedlinenum"]);
                                 //        }

                                 //        CallingInfo.linenum = e.Attributes.ContainsKey("connectedlinenum") ? e.Attributes["connectedlinenum"] : " Unknown "; //
                                 //    }
                                 label1.Text = string.Format(Language.talking_to,   CallingInfo.linename , CallingInfo.linenum );
                                 CallerInfo.SetCallInfo(CallingInfo.linename, CallingInfo.linenum, e.CallerIdNum, 1, DateTime.Now, e.UniqueId);
                                 SendStatustoXMPP("Calling over Asterisk...");
                             }));
                        }
                        break;

                }
            }

        }
        catch (Exception ex)
        {
            DebugLogger.LogRecord(ex.Message + " [ Function: " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Class: " + this.GetType().Name + " ]");
        }


    }







请帮助...




Please Help...

推荐答案

我希望你合理地使用 Control.Invoke ,因为你的网络应该从一些非UI线程中调用相关事件。至少,这是你应该做的事情。并且,出于同样的原因,检查 Control.InvokeRequired 将是多余的。这是因为事件的调用确实发生在一些非UI线程中。



看起来你唯一的问题是希望并行处理事件。您应该了解 Control.Invoke 的作用。它将执行委托给UI线程。如果由于某些事件调用而将某些方法委托给UI线程,那么自然地,调用将在UI线程上被序列化,因此,没有任何东西可以使这种处理并行,更重要的是,这样的并行性没有意义。



请同时查看我过去的答案和一篇文章,其中我解释了线程调用如何工作:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ],

用于线程通信和线程间调用的简单阻塞队列 [ ^ ]。



-SA
I hope you rightfully uses Control.Invoke, because your network-related event should really be invoked from some non-UI thread. At least, this is something you should really have done. And, by the same reason, checking Control.InvokeRequired would be redundant. This is if the invocation of the event really happens in some non-UI thread.

It looks like your only problem is hoping for parallel handling of the event. You should understand what Control.Invoke does. It delegates execution to the UI thread. If you delegate some method to the UI thread as a result of some event invocation, naturally, the calls will be serialized on the UI thread, so, there is nothing which would make this handling parallel, more important, such parallelism would not make sense.

Please also see my past answer and an article where I explain how thread invocation works under the hood:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^],
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA


你是我在唱新的线程写的你调用代码。你告诉UI线程执行一个方法,所以是的,UI线程一次只能执行一个方法。



事件通常是在UI线程上引发的,所以你可能会不必要地调用这些方法。



诀窍是重新设计你的代码,这样除了操纵控件之外,正在完成的实际工作是在你为此目的而旋转的线程上完成的。
You're my singing up new threads written you Invoke code. You're telling the UI thread to execute a method, so yes, the UI thread can only execute one method at a time.

Events are normally raised on the UI thread, so you're probably needlessly Invoking those methods.

The trick is redesign your code so the actual work being done, apart from manipulating controls, is being done on threads you spin up for that purpose.


这篇关于在当前事件结束之前,事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆