开发者

Application.Run() operates differently in a Release build w/o a debugger than w/ or in a Debug build

开发者 https://www.devze.com 2023-01-24 11:58 出处:网络
I\'m having trouble with a processing thread in C#.Basically the thread manages chat windows when new messages arrive or are sent and unfortunately I\'m having different situations occur based on the

I'm having trouble with a processing thread in C#. Basically the thread manages chat windows when new messages arrive or are sent and unfortunately I'm having different situations occur based on the running environment.

When running a Debug build (either with or without a debugger), or a Release build under a debugger, the Process() function operates correctly, shows windows and receives messages fine.

However, when running a Release build without a debugger, the Application.Run() call seems to stop the processing of the main Process() thread (notice that this call happens under a sub-thread of the processing thread) and so no more processing occurs.

Through the use of the MessageBox.Show() call I have determined that Application.Run() is the last call to be made before no more message boxes are shown (and they should be as it shows how many messages are received each time the while loop runs).

Does anyone know why the Application.Run() call is behaving differently under this situation?

    /// <summary>
    /// Processes the IM message queue, managing the chat windows and messages.
    /// </summary>
    private void Process()
    {
        try
        {
            MessageBox.Show("MessageQueue process has started!");

            while (this.m_Running)
            {
                List<Message> messages = null;
                lock (this.m_Lock)
                {
                    messages = new List<Message>(this.m_Messages);
                    MessageBox.Show("MessageQueue received " + this.m_Messages.Count + " messages on this spin.");
                    this.m_Messages.Clear();
                }

                // Process all the messages
                foreach (Message m in messages)
                {
                    Contact c = m.Contact;

                    if (!this.m_Windows.Keys.Contains(c.ID) || this.m_Windows[c.ID] == null)
                    {
                        MessageBox.Show("MessageQueue is creating a new window.");
                        bool complete = false;
                        Thread t = new Thread(() =>
                            {
                                try
                                {
                                    ChatWindow w = new ChatWindow(this, c, new Contact(this.m_Client.JID, null));
                                    w.Load += (sender, e) =>
                                        {
                                            if (m.IsTo)
                                                w.AppendSentMessage(m.To, m.Data);
                                            else if (m.IsFrom)
                                                w.AppendRecievedMessage(m.From, m.Data);

                                            w.UpdateStatus(c);
                                        };
                                    w.FormClosed += (s开发者_高级运维ender, e) =>
                                        {
                                            this.m_Windows[c.ID] = null;
                                        };
                                    c.StatusUpdated += (sender, e) =>
                                        {
                                            RoketPack.Manager.VoidLambda lambda = () =>
                                            {
                                                w.UpdateStatus(c);
                                            };

                                            if (w.InvokeRequired)
                                                w.Invoke(lambda);
                                            else
                                                lambda();
                                        };
                                    MessageBox.Show("MessageQueue is now showing the new window.");
                                    w.Show();
                                    if (!this.m_Windows.Keys.Contains(c.ID))
                                        this.m_Windows.Add(c.ID, w);
                                    else
                                        this.m_Windows[c.ID] = w;
                                    complete = true;
                                    MessageBox.Show("MessageQueue is now running the new window.");
                                    Application.Run(w);
                                    MessageBox.Show("MessageQueue is now closing the window.");
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.ToString());
                                    complete = true;
                                }
                            });
                        t.Name = "IM Chat Window - " + c.ID;
                        t.IsBackground = true;
                        t.Start();

                        // We have to wait until the form has been added to the dictionary.
                        while (!complete) ;
                    }
                    else
                    {
                        RoketPack.Manager.VoidLambda lambda = () =>
                            {
                                if (m.IsTo)
                                    this.m_Windows[c.ID].AppendSentMessage(m.To, m.Data);
                                else if (m.IsFrom)
                                    this.m_Windows[c.ID].AppendRecievedMessage(m.From, m.Data);
                                MessageBox.Show("MessageQueue appended the message to the chat window.");
                            };

                        MessageBox.Show("MessageQueue received a message and is now forwarding it onto the chat window.");
                        if (this.m_Windows[c.ID].InvokeRequired)
                            this.m_Windows[c.ID].Invoke(lambda);
                        else
                            lambda();
                    }
                }

                // Sleep for 10 milliseconds.
                Thread.Sleep(10);
            }
        }
        finally
        {
            MessageBox.Show("MessageQueue process has terminated!");
        }
    }


Aside from what leppie wrote, this looks like a bad starting point:

while (!complete);

I don't know exactly what guarantees there are around hoisted variables and visibility, but tight-looping is almost always a bad idea.

It would generally be better to use a Wait/Notify approach or an Auto/ManualResetEvent.


Do you realize Application.Run does not return until your application closes?

It also seems you are calling Application.Run from a child thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号