using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SetText(string msg)
{
if (textBox1.InvokeRequired)
{
// 指定したコントロールの操作に Invoke()が必要かどうか(この場合、this.InvokeRequired としてもよい)。
Invoke(new SetTextCallback(SetText), new object[] { msg }); // 必要であれば自身を Invoke する。
return;
}
textBox1.AppendText(msg);
Application.DoEvents();
}
private void Form1_Shown(object sender, EventArgs e)
{
hoge h = new hoge(SetText);
h.hogehoe();
h.hogehoe();
h.hogehoe();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace WindowsFormsApplication1
{
delegate void SetTextCallback(string msg);
class hoge
{
SetTextCallback _SetText;
int TID;
public hoge(SetTextCallback SetText)
{
_SetText = SetText;
TID = 0;
}
public void hogehoe()
{
Thread t = new Thread(new ThreadStart(worker));
t.Start();
}
void worker()
{
int tid = TID;
TID++;
for (int i = 0; i < 100; i++)
{
_SetText("[ "+tid.ToString("0000")+" ] " + i.ToString() + Environment.NewLine);
}
}
}
}