111,125
社区成员
发帖
与我相关
我的任务
分享
Thread[] TdSend;
//执行方法
private void Send(string user,string pwd)
{
//代码块。这里在断点调试时,根本没有进入
}
private void btn_send_Click(object sender, EventArgs e)
{
DataSet ds = eDao.GetSendInfo();
if (ds == null)
{
MessageBox.Show("获取信息失败!", "提示:");
return;
}
dtAccount = ds.Tables["Account"];
dtAddress = ds.Tables["Address"];
TdSend = new Thread[int.Parse(tb_ThreadNum.Text)];
for (int k = 0; k < TdSend.Length; k++)
{
string user = dtAccount.Rows[0]["UserName"].ToString();
string pwd = dtAccount.Rows[0]["PassWord"].ToString();
dtAccount.Rows.RemoveAt(0);
TdSend[k] = new Thread(delegate() { Send(user, pwd); });
TdSend[k].Start();
}
}
TdSend[k].Start();static Thread[] TdSend;
private static readonly object lck = new object();
static void Main(string[] args)
{
TdSend = new Thread[3];
string a = "1";
string b = "2";
for (int k = 0; k < TdSend.Length; k++) {
TdSend[k] = new Thread(delegate() { test(a,b); });
TdSend[k].Start();
}
int c=0;//断点1
Console.ReadLine();
}
public static void test(string user, string pwd)
{
lock (lck)
{
int d;//断点2
}
}
这次进入的顺序是:断点2、断点1、断点2、断点2
好歹也进了方法,你的没进真不明白为什么了
static Thread[] TdSend;
static void Main(string[] args)
{
TdSend = new Thread[3];
string a = "1";
string b = "2";
for (int k = 0; k < TdSend.Length; k++) {
TdSend[k] = new Thread(delegate() { test(a,b); });
TdSend[k].Start();
}
int c=0;//断点1
Console.ReadLine();
}
public static void test(string user, string pwd)
{
//断点2
}
}
断点2只进去2次,断点1不进去。
private void btn_send_Click(object sender, EventArgs e)
{
DataSet ds = eDao.GetSendInfo();
if (ds == null)
{
MessageBox.Show("获取信息失败!", "提示:");
return;
}
dtAccount = ds.Tables["Account"];
dtAddress = ds.Tables["Address"];
TdSend = new Thread[int.Parse(tb_ThreadNum.Text)];
for (int k = 0; k < TdSend.Length; k++)
{
string user = dtAccount.Rows[0]["UserName"].ToString();
string pwd = dtAccount.Rows[0]["PassWord"].ToString();
dtAccount.Rows.RemoveAt(0);
TdSend[k] = new Thread(delegate() { Send(user, pwd); });
TdSend[k].Start();
}
//线程里的方法 一定要在这个事件执行完后才开始执行线程的方法吗?
//我这下面还有一个 while 的循环
}