public partial class Form1 : Form
{
private object obj = new object();
Image imageLogin; // 登陆验证码图片
string imageCodeLogin; // 登陆验证码字符
Thread threadBuy;
private void GetBuyThread()
{
threadBuy = new Thread(new ThreadStart(Start));
threadBuy.Name = "BuyThread";
threadBuy.Start();
} // 登录线程
private void Start()
{
Login();
}
public void Login()
{
for (int i = 0; i < 10000; i++)
{
try
{
Stream stream= null; // 这里省略读取过程。
if (stream != null)
{
lock (obj)
{
// 读取并保存登录验证码
imageLogin = null; // 读取图片省略
Show(imageLogin); // 主线程输出图片省略
imageCodeLogin = null; // 识别验证码省略
}
}
}
}
}
出错的代码:
复制代码
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); // 这里会报错
}
复制代码
或者这里:
public string Read(Bitmap bmp)
{
// 这里读取图片时
}
第一次用多线程,不知道怎么加锁,是不是锁加的地方不对?