C
C
Cyrus2011-01-25 00:17:45
.NET
Cyrus, 2011-01-25 00:17:45

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>&nbsp; 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>&nbsp; <font color="#0000ff">var</font> d = <font color="#0000ff">new</font> SetTextCallback(AppendText);</li>
<li>&nbsp; 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>&nbsp; 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>

Parse.cs
<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>&nbsp;</li>
<li> <font color="#0000ff">public</font> <font color="#0000ff">void</font> WriteToBase()</li>
<li> {</li>
<li>Form1.AppendText(<font color="#A31515">&quot;Test\n&quot;</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>

Form1.AppendText(); - it swears at this - “non-static method in static context”, of course, if I make this method static Invoke does not work.
In general, I need to transfer by any means from this method to the TextBox on the form during the execution of the method. To take out the cycle from the method to the Form1 class is not an option.
There is little experience, and the search yields nothing. To transfer data from the class, this is obviously necessary, tell me how not to sculpt bicycles? Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Keith, 2011-01-25
@Cyrus

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.

counter.cs:
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.

E
equand, 2011-01-25
@equand

doesn't he need an instance? You call the method statically, and it needs an already initialized object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question