How to pass change parameter control from an external class in a stream in C# 4.0?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
6 views
There is form Form1 and a separate class method which is executed in the thread.

Form1.cs:
// ...private void Start_Click(object sender, EventArgs e){ // ... var parse = new Parse { param = 45 }; var t = new Thread(parse.WriteToBase); t.Start();}// ...delegate void SetTextCallback(string text);public void AppendText(string text){ if (log.InvokeRequired) {  var d = new SetTextCallback(AppendText); Invoke(d, new object[] { text }); } else { log.Text = text; }}// ...* This source code was highlighted with Source Code Highlighter.


Parse.cs
// ...class Parse{ public int xx;  public void WriteToBase() {Form1.AppendText("Test\"); }}// ...* This source code was highlighted with Source Code Highlighter.



Form1.AppendText(); — this swear — "non-static method in static context", of course if this make the static method Invoke is not working.


In General, I need to get by any means from this method in the TextBox on the form during the execution of the method. Make a loop from a method in Form1 not the answer.


Little experience, and the search does nothing. To pass data from a class, it's obvious you need to tell me how not to sculpt a Bicycle? Thank you!
by | 6 views

2 Answers

0 like 0 dislike
equand rights. To the end can not understand what is required, I wrote a small example and it should, in principle, to be understood as in the flow run method and call the other from Form1.cs
\r
\rForm1.cs:
\r
public Form1()
{
InitializeComponent();
\r var counter = new Counter(Log);
\r var thread = new Thread(counter.Start);
thread.Start();
}
\r
\rpublic void Log(int a)
{
\r if (InvokeRequired)
{
Invoke(new MethodInvoker(() => Log(a)));
\r return;
}
Text = a.ToString();
}

\r
\r* This source code was highlighted with Source Code Highlighter.

\r
\rCounter.cs:
\r
public class Counter
{
\r private readonly Action<int> _logger;
\r
\r public Counter(Action<int> logger)
{
_logger = logger;
}
\r
\r public void Start()
{
\r for(var i =0;;i++)
{
_logger(i);
Thread.Sleep(1000);
}
}
}

\r
\r* This source code was highlighted with Source Code Highlighter.
by
0 like 0 dislike
doesn't he need instance? You call the method statically, and he is already initialized object
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
asked Mar 22, 2019 by andreycha
110,608 questions
257,186 answers
0 comments
27,925 users