PDA

View Full Version : سوال: Hang در زمان invoke کنترل خارج از نخ جاری



mehdi_csharp
یک شنبه 17 بهمن 1389, 15:41 عصر
دوستان علت hang کردن این برنامه در قسمت invoke چیست؟؟

using System;
using System.Threading;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication37
{
public partial class Form1 : Form
{
public delegate void MyDelegate(int o);
MyDelegate myDelegate;

void RunInThread(object u)
{

myDelegate = new MyDelegate(AddControl);
IAsyncResult j= this.BeginInvoke(myDelegate,(int)u);
MessageBox.Show("Hello");
EndInvoke(j); //hang***********
//Add your code that needs to be executed in separate thread
//except UI updation


}
void AddControl(int r)
{

TextBox textBox1 = new TextBox();
this.Controls.Add(textBox1);
label1.Text = r.ToString();
}

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
int i = 0;
while (i<100)
{

Thread t = new Thread(new ParameterizedThreadStart(RunInThread));
t.Start(i);
t.Join();
i++;

}
}
}
}