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;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Threading;namespace thread实验
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Form1 f = new Form1();
Thread th = new Thread(new ThreadStart (f.eg));
th.Start(); }
private void eg()
{
SqlConnection conn = new SqlConnection("Data Source=172.24.95.180;Initial Catalog=pubs;User Id=sa;Password=sa");
SqlDataAdapter adpt = new SqlDataAdapter("",conn );
DataSet dstem = new DataSet();
try
{
conn.Open();
adpt.Fill(dstem ,"f");
conn.Close(); for (int i = 0; i < dstem.Tables["f"].Rows.Count; i++)
{
this.checkedListBox1.Items.Add(dstem .Tables["f"].Rows [i][0].ToString ());
}
}
catch {
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Threading;namespace thread实验
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Form1 f = new Form1();
Thread th = new Thread(new ThreadStart (f.eg));
th.Start(); }
private void eg()
{
SqlConnection conn = new SqlConnection("Data Source=172.24.95.180;Initial Catalog=pubs;User Id=sa;Password=sa");
SqlDataAdapter adpt = new SqlDataAdapter("",conn );
DataSet dstem = new DataSet();
try
{
conn.Open();
adpt.Fill(dstem ,"f");
conn.Close(); for (int i = 0; i < dstem.Tables["f"].Rows.Count; i++)
{
this.checkedListBox1.Items.Add(dstem .Tables["f"].Rows [i][0].ToString ());
}
}
catch {
}
}
}
}
你能 给我个明路.
哦我用的是c#2008 express版本的
需要用委托才可以.
checkedlistbox1是主线程创建的.
不能用其他线程直接访问.
和还有 你线程为什么
Form1 f = new Form1();
Thread th = new Thread(new ThreadStart (f.eg));
不是this?