防止程序在工作时进入忙碌模式 [英] prevent a program from busy mode while working

查看:31
本文介绍了防止程序在工作时进入忙碌模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个一次发送许多电子邮件的程序,我注意到该程序在运行时进入忙碌模式,即:我无法编辑任何内容或触摸任何内容,我制作了一个功能来显示成功发送的数量电子邮件和标签上的失败邮件,以便用户可以监控发送的电子邮件,现在我遇到了一个问题,程序完成发送电子邮件后标签显示整数,我想防止这种情况,我尝试使用线程模式,但是失败了.这是代码:

i am making a program which sends many emails at a time and i noticed while the program is working it goes on busy mode i.e : i can not edit anything or touch anything and i made a function to show the number of successfully sent emails and the failed ones on a label so that the user can monitor the sent emails , now i got a problem the label shows the integer number after the program has done sending emails , i want to prevent that , i tried using the threading mode but failed it . and here is the code :

int 成功 = 0;

int success = 0;

    private void success()
    {
        label17.Text = success.ToString();
    }

    private void sendmail()
    {
        SmtpClient client = new SmtpClient(comboBox1.Text);
        client.Credentials = new NetworkCredential(textBox6.Text, textBox7.Text);
        MailMessage message = new MailMessage();
        message.From = new MailAddress(textBox3.Text, textBox1.Text);
        message.Subject = textBox4.Text;
        message.Body = richTextBox1.Text;
        if (textBox5.Text != "")
        {
            message.Attachments.Add(new Attachment(textBox5.Text));
        }

        string bulkpath = textBox2.Text;

        foreach(string eachmail in File.ReadAllLines(bulkpath))
        {
            Thread t = new Thread(success);          // Kick off a new thread
            t.Start();  

            message.To.Add(eachmail);
            try
            {
                client.Send(message);
                success++;
            }
            catch
            {
                MessageBox.Show("One has not passed :(");
            }
            message.To.Clear();
        }

    }

推荐答案

或者,您可以简单地启动一个负责发送邮件的新线程

Optionally you can simply start a new Thread which is responsible for sending the mail

Thread t = new Thread(new ThreadStart(sendMail));
t.Start();

使用 BackgroundWorker 时,您可以实现进度通知,并且可以在您的操作在后台完成时收到回调.

When using the BackgroundWorker you're able to implement progress notifications and you're able to receive a callback when your action has finished in the background.

托斯滕

这篇关于防止程序在工作时进入忙碌模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆