开发者社区> 问答> 正文

关于Abort线程时会报出“正在中止线程”的异常 Java报错

有什么办法解决这个异常吗? 最近做个项目发送短信中遇到这个异常。
现在暂时没法发图
代码如下:
private void send()
{
try
{
//SendSMSBase(this.user, this.pwd, this.mobile, this.msg);
WebClient webc = new WebClient();
string datastr = "userid=" + this.user_id + "&username=" + this.user_name + "&passwordMd5=" + this.pwd + "&mobile=" + this.mobile + "&message=" + System.Web.HttpUtility.UrlEncode(this.msg, System.Text.Encoding.GetEncoding("gbk"));
//webc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
//byte[] res_by = webc.UploadData("http://139.129.107.160:8081/sendsms.php", "POST", System.Text.Encoding.UTF8.GetBytes(datastr));
//string res_str = System.Text.Encoding.UTF8.GetString(res_by);
//if (res_str.Contains("-"))
//{
// logger.Error("发送手机短消息接口返回错误!" + res_str);
//}
}
        catch (Exception ex)
        {
            logger.Error("发送手机短消息失败!" + ex.Message);
        }

        finally
        {
            try
            {
                if (t1.IsAlive)
                {
                    t1.Abort();
                }
            }
            catch (ThreadAbortException ex)
            {

            }
            catch (Exception eeee)
            {
                logger.Error("异常:" + eeee.Message);
                //throw eeee;
            }
        }
    }
public void SendMesPhone(string mobileGet, string msgGet)
{
// this.user = 100015002;
this.user_id = "100531";
this.user_name = "enetedu02";
//this.pwd = "!^v30&t1";
this.pwd = "55aa4312da4ed697";
this.mobile = mobileGet;
this.msg = msgGet;
        //ThreadPool.QueueUserWorkItem(new WaitCallback(send), null);
        t1 = new Thread(new ThreadStart(send));
        t1.Start();
       
    }

展开
收起
泡沫小迪 2020-05-29 16:11:10 1589 0
1 条回答
写回答
取消 提交回答
  • 在调用此方法的线程上引发 ThreadAbortException,以开始终止此线程的过程。 调用此方法通常会终止线程。 本意就是用抛出异常来终止线程, 可以try-catch下

    2020-05-29 16:11:19
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载