C# Application.Run()在没有调试器的发布版本中的操作与在没有调试器的发布版本或调试版本中的操作不同
我在处理C#中的线程时遇到问题。基本上,当新消息到达或发送时,线程会管理聊天窗口,不幸的是,根据运行环境,我遇到了不同的情况 运行调试生成(带或不带调试器)或调试器下的发布生成时,Process()函数运行正常,显示窗口并接收消息 但是,在没有调试器的情况下运行发布版本时,Application.Run()调用似乎会停止主进程()线程的处理(请注意,此调用发生在处理线程的子线程下),因此不会再进行处理 通过使用MessageBox.Show()调用,我确定Application.Run()是在不再显示消息框之前进行的最后一次调用(它们应该显示每次while循环运行时收到的消息数量) 有人知道为什么在这种情况下Application.Run()调用的行为会有所不同吗C# Application.Run()在没有调试器的发布版本中的操作与在没有调试器的发布版本或调试版本中的操作不同,c#,multithreading,C#,Multithreading,我在处理C#中的线程时遇到问题。基本上,当新消息到达或发送时,线程会管理聊天窗口,不幸的是,根据运行环境,我遇到了不同的情况 运行调试生成(带或不带调试器)或调试器下的发布生成时,Process()函数运行正常,显示窗口并接收消息 但是,在没有调试器的情况下运行发布版本时,Application.Run()调用似乎会停止主进程()线程的处理(请注意,此调用发生在处理线程的子线程下),因此不会再进行处理 通过使用MessageBox.Show()调用,我确定Application.Run()是在不
/// <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!");
}
}
//
///处理IM消息队列,管理聊天窗口和消息。
///
私有无效进程()
{
尝试
{
Show(“MessageQueue进程已启动!”);
当(此.m_运行时)
{
列表消息=null;
锁(这个.m_锁)
{
消息=新列表(此.m_消息);
MessageBox.Show(“MessageQueue received”+this.m_Messages.Count+“this spin上的消息”);
此.m_Messages.Clear();
}
//处理所有消息
foreach(消息中的消息m)
{
触点c=m.触点;
如果(!this.m_Windows.Keys.Contains(c.ID)| | this.m_Windows[c.ID]==null)
{
Show(“MessageQueue正在创建一个新窗口。”);
bool complete=false;
线程t=新线程(()=>
{
尝试
{
ChatWindow w=新的聊天窗口(this,c,新联系人(this.m_Client.JID,null));
w、 加载+=(发送方,e)=>
{
if(m.IsTo)
w、 附加信息(m.To,m.Data);
else if(m.IsFrom)
w、 附录ReceivedMessage(m.From,m.Data);
w、 更新状态(c);
};
w、 FormClosed+=(发件人,e)=>
{
this.m_Windows[c.ID]=null;
};
c、 状态更新+=(发件人,e)=>
{
RoketPack.Manager.VoidLambda lambda=()=>
{
w、 更新状态(c);
};
如果(w.invokererequired)
w、 调用(lambda);
其他的
lambda();
};
Show(“MessageQueue现在正在显示新窗口。”);
w、 Show();
如果(!this.m_Windows.Keys.Contains(c.ID))
此.m_Windows.Add(c.ID,w);
其他的
这个.m_Windows[c.ID]=w;
完整=正确;
Show(“MessageQueue现在正在运行新窗口。”);
应用程序运行(w);
Show(“MessageQueue现在正在关闭窗口。”);
}
捕获(例外情况除外)
{
Show(例如ToString());
完整=正确;
}
});
t、 Name=“IM聊天窗口-”+c.ID;
t、 IsBackground=true;
t、 Start();
//我们必须等到表单被添加到字典中。
而(!完成);
}
其他的
{
RoketPack.Manager.VoidLambda lambda=()=>
{
if(m.IsTo)
此.m_Windows[c.ID].AppendSentMessage(m.To,m.Data);
else if(m.IsFrom)
此.m_Windows[c.ID].appendReceivedMessage(m.From,m.Data);
Show(“MessageQueue将消息附加到聊天窗口中。”);
};
Show(“MessageQueue收到一条消息,现在正在将其转发到聊天窗口。”);
if(this.m_Windows[c.ID].InvokeRequired)
此.m_Windows[c.ID].Invoke(lambda);
其他的
lambda();
}
while (!complete);