MM0536 2025-12-12 16:11 采纳率: 0%
浏览 2
已结题

多线程 锁 对象当前正在其他地方使用


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)
{
    // 这里读取图片时
}
第一次用多线程,不知道怎么加锁,是不是锁加的地方不对?
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月20日
    • 创建了问题 12月12日