Answer the question
In order to leave comments, you need to log in
How to pass change control parameter from outer class in thread in C# 4.0?
There is a form Form1 and a separate class whose method is executed in thread.
Form1.cs:
<font color="black"><ol> <li><font color="#008000">// ...</font></li> <li><font color="#0000ff">private</font> <font color="#0000ff">void</font> Start_Click(<font color="#0000ff">object</font> sender, <font color="#2B91AF">EventArgs</font> e)</li> <li>{</li> <li> <font color="#008000">// ...</font></li> <li> <font color="#0000ff">var</font> parse = <font color="#0000ff">new</font> Parse</li> <li> {</li> <li> param = 45</li> <li> };</li> <li> <font color="#0000ff">var</font> t = <font color="#0000ff">new</font> Thread(parse.WriteToBase);</li> <li> t.Start();</li> <li>}</li> <li><font color="#008000">// ...</font></li> <li><font color="#0000ff">delegate</font> <font color="#0000ff">void</font> SetTextCallback(<font color="#0000ff">string</font> text);</li> <li><font color="#0000ff">public</font> <font color="#0000ff">void</font> AppendText(<font color="#0000ff">string</font> text)</li> <li>{</li> <li> <font color="#0000ff">if</font> (log.InvokeRequired)</li> <li> {</li> <li> <font color="#0000ff">var</font> d = <font color="#0000ff">new</font> SetTextCallback(AppendText);</li> <li> Invoke(d, <font color="#0000ff">new</font> <font color="#0000ff">object</font>[] { text });</li> <li> }</li> <li> <font color="#0000ff">else</font></li> <li> {</li> <li> log.Text = text;</li> <li> }</li> <li>}</li> <li>// ...</li> </ol></font><font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>
<font color="black"><ol> <li><font color="#008000">// ...</font></li> <li><font color="#0000ff">class</font> Parse</li> <li>{</li> <li> <font color="#0000ff">public</font> <font color="#0000ff">int</font> xx;</li> <li> </li> <li> <font color="#0000ff">public</font> <font color="#0000ff">void</font> WriteToBase()</li> <li> {</li> <li>Form1.AppendText(<font color="#A31515">"Test\n"</font>);</li> <li> }</li> <li>}</li> <li>// ...</li> </ol></font><font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>
Answer the question
In order to leave comments, you need to log in
Equand is right. Until the end, I may not understand what exactly is required, I wrote a small example and from it, in principle, it should be clear how to run a method in a thread and call another one from Form1.cs
Form1.cs:
public Form1()
{
InitializeComponent();
var counter = new Counter(Log);
var thread = new Thread(counter.Start);
thread.Start();
}
public void Log(int a)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(() => Log(a)));
return;
}
Text = a.ToString();
}
* This source code was highlighted with Source Code Highlighter.
public class Counter
{
private readonly Action<int> _logger;
public Counter(Action<int> logger)
{
_logger = logger;
}
public void Start()
{
for(var i =0;;i++)
{
_logger(i);
Thread.Sleep(1000);
}
}
}
* This source code was highlighted with Source Code Highlighter.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question