Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3323058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:15:24+00:00 2026-05-17T23:15:24+00:00

I’m having trouble with a processing thread in C#. Basically the thread manages chat

  • 0

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 += (sender, 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!");
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:15:25+00:00Added an answer on May 17, 2026 at 11:15 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.