如何从另一个线程调用控制方法

时间:2015-12-08 10:28:12

标签: c# multithreading winforms invoke

我想从另一个帖子中调用RichTextBox.Find()。我怎样才能做到这一点? RichTextBox位于UserControl,我在我的表单中使用了Invoke。 我想从另一个线程更新它。我能够使用_ucResultRich.rchResult.Find(word, startIndex, RichTextBoxFinds.None);更改其属性。但无法弄清楚如何从我的帖子中调用Thread thread=new Thread(thrHighlight); thread.Start(e.RowIndex); private void ThrHighlight(object obj) { string[] words = ucSearchControls.rdbExact.Checked ? new string[] { ucSearchControls.txtSearch.Text.Trim() } : ucSearchControls.txtSearch.Text.Split(' '); foreach (string word in words) { int startIndex = 0; while (startIndex < _ucResultRich.rchResult.TextLength) { int wordStartIndex = _ucResultRich.rchResult.Find(word, startIndex, RichTextBoxFinds.None); if (wordStartIndex != -1) { _ucResultRich.rchResult.SelectionStart = wordStartIndex; _ucResultRich.rchResult.SelectionLength = word.Length; _ucResultRich.rchResult.SelectionBackColor = Color.Yellow; } else break; startIndex += wordStartIndex + word.Length; } } }

<div class="entry-content" style="visibility: visible; opacity: 1;">
 <div style="text-align:justify">
This section on C interview <span id="IL_AD1" class="IL_AD">questions and answers</span> focuses on “Variable Names”. One shall practice these <span id="IL_AD5" class="IL_AD">interview questions</span> to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C Interview questions come with detailed explanation of the <span id="IL_AD2" class="IL_AD">answers</span> which helps in better understanding of C <span id="IL_AD3" class="IL_AD">concepts</span>.<p></p>
<p>Here is a listing of C interview questions on “Variable Names” along with answers, explanations and/or solutions:
</p></div>
<p>1. C99 standard guarantees uniqueness of ____ characters for internal names.<br>
a) 31<br>
b) 63<br>
c) 12<br>
d) 14</p>
<span class="collapseomatic" id="id5489" tabindex="0" title="View Answer">View Answer</span><div id="target-id5489" class="collapseomatic_content " style="display: none;">Answer:b<br>
Explanation:ISO C99 compiler may consider only first 63 characters for internal.<br>
</div>
<p>2. C99 standard guarantess uniqueness of _____ characters for external names.<br>
a) 31<br>
b) 6<br>
c) 12<br>
d) 14</p>
<span class="collapseomatic " id="id7970" tabindex="0" title="View Answer">View Answer</span><div id="target-id7970" class="collapseomatic_content " style="display: none;">Answer:a<br>
Explanation:ISO C99 compiler may consider only first 31 characters for external<br>
variables having 31 characters due to which it may not be unique.<br>
</div>
<p>3. Which of the following is not a valid variable name declaration?<br>
a) int __a3;<br>
b) int __3a;<br>
c) int __A3;<br>
d) None of the mentioned</p>
<span class="collapseomatic " id="id5714" tabindex="0" title="View Answer">View Answer</span><div id="target-id5714" class="collapseomatic_content " style="display: none;">Answer:d<br>
Explanation:None.<br>
</div>
<p>4. Which of the following is not a valid variable name declaration?<br>
a) int _a3;<br>
b) int a_3;<br>
c) int 3_a;<br>
d) int _3a</p>

我该怎么做?

P.S:这是my first question的后续行动以及那里的@varocarbas评论

2 个答案:

答案 0 :(得分:1)

您需要将代码与UI控件分离,并在外部线程上执行业务逻辑,并在Dispatcher.BeginInvoke或Invoke上更新UI控件。

例如,您可以将文本框中的文本保存在单独的属性中,并在完成UI线程上的UI突出显示部分后,在其他线程上执行查找。

答案 1 :(得分:1)

这个答案专门用于展示如何正确使用(即通过最大化其内置功能)BackgroundWorker(这是我在previous post of the OP中写的一些评论的延续)提供预期的功能。

要使用这些行下方的代码,请启动一个新的Winforms项目,并将以下控件添加到主窗体:Buttonbutton1,点击事件button1),{{ 1}}(RichTextBox)和richTextBox1BackgroundWorkerbackgroundWorker1事件DoWorkbackgroundWorker1_DoWork事件ProgressChanged) ;另请注意,backgroundWorker1_ProgressChanged是主要表单的Form1_Load事件。

要使用该应用程序,只需输入Load中的任何文字,包括一些硬编码的字词(即&#34; word1&#34;,&#34; word2&#34;,&#34 ; word3&#34;,&#34; word4&#34;,&#34; word5&#34;),点击richTextBox1并确认它们按预期突出显示。

button1