多线程 等待窗体 ,错误“正在中止线程”

水桶IIII 2014-09-17 02:46:53
有一个等待界面,用线程更新的时候,老是报错,哪位能帮处理一下?


代码1 登录窗体

//登录按钮
HL.Control.Form.WaitFormService.CreateWaitForm();
this.Hide();//弹出登录提示画面
HL.Control.Form.WaitFormService.SetWaitFormContent("正在连接服务器");


代码2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace HL.Control.Form
{
public class WaitFormService
{
private WaitFormService()
{
}

public static void CreateWaitForm()
{
WaitFormService.Instance.CreateForm();
}

public static void CloseWaitForm()
{
WaitFormService.Instance.CloseForm();
}

public static void SetWaitFormText(string strCaption, string strMessage, string strContent)
{
WaitFormService.Instance.SetFormText(strCaption, strMessage, strContent);
}

public static void SetWaitFormCaption(string text)
{
WaitFormService.Instance.SetFormCaption(text);
}

public static void SetWaitFormMessage(string text)
{
WaitFormService.Instance.SetFormMessage(text);
}

public static void SetWaitFormContent(string text)
{
WaitFormService.Instance.SetFormContent(text);
}

private static WaitFormService _instance;
private static readonly Object syncLock = new Object();

public static WaitFormService Instance
{
get
{
if (WaitFormService._instance == null)
{
lock (syncLock)
{
if (WaitFormService._instance == null)
{
WaitFormService._instance = new WaitFormService();
}
}
}
return WaitFormService._instance;
}
}

private Thread waitThread;
private HL.Control.Form.WaitForm waitForm;

public void CreateForm()
{
if (waitThread != null)
{
try
{
//if (waitForm != null)
//{
// waitForm.Dispose();
// waitForm = null;
//}
waitThread.Abort();
waitThread.Join();
}
catch (Exception)
{
}
}

waitThread = new Thread(new ThreadStart(delegate()
{
waitForm = new HL.Control.Form.WaitForm("提示","正在加载中,请等待…………","");
Application.Run(waitForm);
}));
waitThread.Start();
}

public void CloseForm()
{
if (waitThread != null)
{
try
{
waitThread.Abort();
waitThread.Join();
}
catch (Exception)
{
}
}
}

public void SetFormText(string strCaption, string strMessage, string strContent)
{
if (waitForm != null)
{
try
{
waitForm.SetCaption(strCaption);
waitForm.SetMessage(strMessage);
waitForm.SetContent(strContent);
}
catch (Exception)
{
}
}
}

public void SetFormCaption(string text)
{
if (waitForm != null)
{
try
{
waitForm.SetCaption(text);
}
catch (Exception)
{
}
}
}

public void SetFormMessage(string text)
{
if (waitForm != null)
{
try
{
waitForm.SetMessage(text);
}
catch (Exception)
{
}
}
}

public void SetFormContent(string text)
{
if (waitForm != null)
{
try
{
waitForm.SetContent(text);
}
catch (Exception)
{
}
}
}
}
}


...全文
1117 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
於黾 2014-09-17
  • 打赏
  • 举报
回复
我觉得已经说的够简单了啊 到底哪里不理解? 不要老跟小鸟一样张嘴等着把代码喂你嘴里啊 放窝里都不会吃?
水桶IIII 2014-09-17
  • 打赏
  • 举报
回复
引用 5 楼 Z65443344 的回复:
原则就是:哪里open,哪里close 窗体就让窗体自己close,线程就让线程自己运行完毕结束 而不要老想着从外部操作他们
新手,求实例学习
水桶IIII 2014-09-17
  • 打赏
  • 举报
回复
引用 3 楼 bdmh 的回复:
都abort了,还要join,你想干嘛
不是说,这2个一起用更好吗?
於黾 2014-09-17
  • 打赏
  • 举报
回复
原则就是:哪里open,哪里close 窗体就让窗体自己close,线程就让线程自己运行完毕结束 而不要老想着从外部操作他们
水桶IIII 2014-09-17
  • 打赏
  • 举报
回复
引用 2 楼 Z65443344 的回复:
不要乱用线程的Abort和Join 最简单的办法,也是最不容易出问题的办法,就是定义个public bool变量 当它为TRUE的时候,waitform里的timer做判断,窗体自己close掉 窗体close了,创建它的线程也就自己退出了(因为是模态窗体) 当然,下次再显示窗体的时候,在load里先把bool变量设置为false,再true就再close
改怎么写,好一点?实用一点
bdmh 2014-09-17
  • 打赏
  • 举报
回复
都abort了,还要join,你想干嘛
於黾 2014-09-17
  • 打赏
  • 举报
回复
不要乱用线程的Abort和Join 最简单的办法,也是最不容易出问题的办法,就是定义个public bool变量 当它为TRUE的时候,waitform里的timer做判断,窗体自己close掉 窗体close了,创建它的线程也就自己退出了(因为是模态窗体) 当然,下次再显示窗体的时候,在load里先把bool变量设置为false,再true就再close
exception92 2014-09-17
  • 打赏
  • 举报
回复
waitThread.Abort(); 之后怎么能 waitThread.Join();????

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧