[英]ThreadState exception in initialization
It has been a long question, so here is the summary first, 这是一个很长的问题,所以这里是摘要,
I don't think you'll need the details but you can find the detailed question I first intented to post below. 我认为您不需要详细信息,但是您可以在下面找到我首先要发布的详细问题。
Many thanks. 非常感谢。
I'm working on a messenger project (Server is a Windows Service and Client is a Windows Forms Application) using Net.Socket's async methods. 我正在使用Net.Socket的异步方法处理Messenger项目(服务器是Windows服务,客户端是Windows窗体应用程序)。 I fire Client's MessageReceived event in callback of Socket.BeginReceive;
我在Socket.BeginReceive的回调中触发Client的MessageReceived事件; Everything is how I want them to be until here.
一切都是我希望他们成为的样子。
I use MessageReceived event in my form (the one that two people writes to each other) I can do anything I want to the UI using Control.Invoke method (if required) with one annoying exception. 我在表单中使用MessageReceived事件(两个人互相写的事件),可以使用Control.Invoke方法(如果需要)对UI进行任何我想做的事情,但有一个令人讨厌的异常。
I have tabbed conversations in the form so when a message arrives, I check if there is an open conversation (tab) with the sender. 我在表单中使用选项卡式对话,因此,在收到邮件时,我会检查与发件人之间是否存在打开的对话(选项卡)。 If yes, I select that tab and display the message.
如果是,我选择该选项卡并显示消息。 If no, I create a new ConversationTab.
如果否,则创建一个新的ConversationTab。
Now, I'm sorry if it's being a long question than it should be or if I can't explain myself sufficently. 现在,我很抱歉,如果这是一个很长的问题,或者我不能充分解释自己。 English is not my first language and this is my first question in Stack Overflow.
英语不是我的母语,这是我在Stack Overflow中的第一个问题。 So, here is the ConversationTab:
因此,这是ConversationTab:
public class ConversationTab : TabPage
{
public User Friend { get; private set; }
public MessageBrowser MessageBrowser { get; private set; }
public ConversationTab(User friend) : base(friend.DisplayName)
{
Friend = friend;
MessageBrowser = new MessageBrowser();
Controls.Add(MessageBrowser);
MessageBrowser.Dock = DockStyle.Fill;
}
}
MessageBrowser derives from WebBrowser and the reason I use this is because I could not apply custom styles (color, font, size) 'per message' using RichTextBox. MessageBrowser派生自WebBrowser,而我之所以使用它,是因为我无法使用RichTextBox应用“每条消息”的自定义样式(颜色,字体,大小)。 RichTextBox.SelectedColor doesn't always work or I couldn't make it work as intended.
RichTextBox.SelectedColor并不总是有效,或者我无法使其按预期工作。 MessageBrowser let's me use CSS instead.
MessageBrowser让我们改用CSS。 Wandering off of the subject?
徘徊于主题之外? Sorry.
抱歉。
Here is the NewConversation method I call when MessageReceived event fires: 这是MessageReceived事件触发时我调用的NewConversation方法:
public void NewConversation(User friend)
{
ConversationTab tab = Conversations.FirstOrDefault(c => c.Friend.Id == friend.Id);
if (tab != null)
ActiveConversation = tab;
else
{
tab = new ConversationTab(friend);
// add tab to TabControl
}
// bla
}
"Conversations" gets the tab pages of the TabControl and "ActiveConversation" gets or sets the SelectedTab property of the TabControl. “对话”获取TabControl的标签页,而“ ActiveConversation”获取或设置TabControl的SelectedTab属性。 My point in creating these properties are mostly thread-safety logic inside.
创建这些属性的主要目的是内部的线程安全逻辑。
So the question: It's throwing ThreadStateException in "tab = new ConversationTab(friend)" part of the above code. 所以问题来了:在上面代码的“ tab = new ConversationTab(friend)”部分中抛出ThreadStateException。 It is the "MessageBrowser = new MessageBrowser()" part of the first code and the constructor of MessageBrowser.
它是第一个代码的“ MessageBrowser = new MessageBrowser()”部分,也是MessageBrowser的构造函数。 The reason of why I didn't provide MessageBrowser's constructor code is because the exception is thrown before any line of inner code gets executed (It is about WebBrowser's constructor, I get this exception when I try to initalize a WebBrowser, too.) Actually I don't even get an exception, the application just closes there without notifying me about anything.
我之所以不提供MessageBrowser的构造函数代码,是因为该异常是在执行任何内部代码行之前引发的(这与WebBrowser的构造函数有关,当我尝试初始化WebBrowser时,也会出现此异常。)甚至没有异常,应用程序只是在不通知我任何事情的情况下关闭了。 I saw the exception when I try to call "MessageBrowser = new MessageBrowser()" on ConversationTab's constructor in Watch window.
当我尝试在“监视”窗口中的ConversationTab的构造函数上调用“ MessageBrowser = new MessageBrowser()”时,看到了异常。
I'm kind of new to using threads and asynchronous methods. 我对使用线程和异步方法有点陌生。 MSDN says:
MSDN说:
ThreadStateException is thrown by methods that cannot perform the requested operation due to the current state of a thread.
由于线程的当前状态而无法执行请求的操作的方法抛出ThreadStateException。
In my case, the thread's state is "Background". 就我而言,线程的状态为“背景”。 I have no clue about what am I doing wrong.
我不知道我在做什么错。
Thank you very much if you read the whole thing and thank you much more if you can help. 如果您阅读了全部内容,将非常感谢;如果您能提供帮助,则非常感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.