相关疑难解决方法(0)

如何杀死C#线程?

我有一个线程,在我们的(旧)SQL服务器上查找数据.

当数据进入时,我将信息发布到模态对话框 - 在进行所有这些处理时,用户不能也不应该做任何其他事情.模态对话框只是让他们看到我正在做某事并阻止他们同时运行另一个查询.

有时(很少)当代码调用SQL服务器时,服务器没有响应(IT将其关闭以进行维护,LAN线被切断,或者PC不在网络上)或执行查询的人时间不多了.因此,模态对话框确实有一个取消按钮.

Thread对象(System.Threading.Thread)有IsBackground=true.

当有人点击取消时,我会调用我的KillThread方法.

注意:我不能在此类中使用BackgroundWorker组件,因为它与某些Windows Mobile 5代码共享,而WM5没有BackgroundWorker.

void KillThread(Thread th) {
  if (th != null) {
    ManualResetEvent mre = new ManualResetEvent(false);
    Thread thread1 = new Thread(
      () =>
      {
        try {
          if (th.IsAlive) {
            //th.Stop();
            // 'System.Threading.Thread' does not contain a definition for 'Stop'
            // and no extension method 'Stop' accepting a first argument of type
            // 'System.Threading.Thread' could be found (are you missing a using
            // directive or an assembly reference?) …
Run Code Online (Sandbox Code Playgroud)

c# multithreading

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

multithreading ×1