停止后台线程

时间:2017-06-25 17:05:42

标签: c# asp.net multithreading

此代码一次成功发送一封电子邮件。 我的问题是在点击Button2时停止发送电子邮件。单击Button2后,计时器会成功显示确认Session["stop"].Equals("true")的消息。 但是,电子邮件没有停止..任何帮助?

protected void Button2_Click(object sender, EventArgs e)
{
    Session["stop"] = "true";
}

private void email()
{
    for (int i = 1; i <= amount; i++)
    {
        Thread email = new Thread(delegate ()
        {
            SendMail(to, from, password, subject, body, amount);
        });
        email.IsBackground = true;
        email.Start();
        if (Session["stop"].Equals("true"))
        {
            email.Abort();
            return;
        }
    }
}

protected void Timer1_Tick(object sender, EventArgs e)
{
    if (Session["stop"].Equals("true"))
    {
        Label1.Text = "Mail stopped. Sent: " + allMailSent;
        Session["stop"] = "false";
    }
}

0 个答案:

没有答案