当我点击btnstop事件时,deletethread必须停止该过程。有人可以帮忙吗?感谢adavance [英] When I click on btnstop event the deletethread must stop the process. Can anyone help? Thank in adavance

查看:81
本文介绍了当我点击btnstop事件时,deletethread必须停止该过程。有人可以帮忙吗?感谢adavance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击btnstop事件时,它不会停止DeleteThread的工作过程。



我尝试了什么:

when I am click on btnstop event it doesn't stop the DeleteThread working process.

What I have tried:

this is another btnDelete event.

ivate void btnDelete_Click(object sender, EventArgs e)
        {
            Thread DeleteThread = new Thread(deleteThread);
                DeleteThread.Start();
            if(btnDelete.Enabled==false)
            {
                DeleteThread.Abort();
            }
        }



deleteThread函数


function for deleteThread

blic void deleteThread()
        {
            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("delete from demo where id='" + item.Cells[0].Value.ToString() + "'", con);
                Thread.Sleep(1000);
                cmd.ExecuteNonQuery();
                MessageBox.Show("your selected " + item.Cells[0].Value.ToString() + " record is deleted");
                con.Close();
            }
        }



这是btnstop事件


this is the btnstop event

private void btnStop_Click(object sender, EventArgs e)
        {
            btnDelete.Enabled = false;
        }

推荐答案

嗯。这里有很多错误。

您的线程可以在bytnDelete Click处理程序中访问,每次用户单击按钮时您都会创建一个新线程 - 所以一旦您离开处理程序方法,无法中止它,因为您无法访问该线程本身。

设置Enabled为false意味着您无法按下该按钮(因为系统不会让用户这样做到禁用控制)所以即使你可以访问处理程序方法中的现有线程,你也无法让用户点击按钮。

唯一可以中止的线程是你的那个刚刚创建并启动了,所以如果你可以完全放弃它就不会走得太远了!

你不能访问UI控件,除了创建它们的线程 - UI线程 - 如果你尝试,就像你在这里使用DataGridView一样,你将获得Cross Thread Exceptions。在运行线程之前从DGV读取数据并将待删除ID的集合传递给要删除的线程。

中止线程是一件危险的事情:特别是当它是做一些复杂的事情,比如数据库活动,因为你不知道线程在进程中的位置。除了在真正的紧急情况下,不要使用Abort。

不要那样做DB访问!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



如果你想杀死线程,那么使用一个类级别变量,在每次循环结束时检查它 - 如果它是 true 然后通过返回存在线程,否则再次循环。
Um. There are quite a few things wrong here.
Your thread is on accessible within the bytnDelete Click handler, and every time the user clicks the button you create a new thread - so once you leave the handler method you can't Abort it because you have no access to the thread itself.
Setting Enabled to false means you can't press the button (because the system won't let the user do that to a disabled control) so even if you could access the existing thread in the handler method, you couldn't get the user to click the button.
The only thread that can be Aborted as it stands is the one you just created and started, so it's not going to get very far if you could Abort it at all!
You cannot access UI controls except from the thread that created them - the UI thread - if you try, like you are doing here with the DataGridView you will get Cross Thread Exceptions. Read the data from the DGV before you run your thread and pass a collection of the "to be deleted" IDs to your thread to delete.
Aborting a thread is a dangerous thing to do: particularly when it's doing something complex like database activity, because you do not know where in the process the thread is. Don't use Abort except in real emergencies.
Don't do DB access like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

If you want to kill the thread then use a class level variable which is checked at the end of each pass through the loop - if it's true then exist the thread via a return, otherwise go round again.


这篇关于当我点击btnstop事件时,deletethread必须停止该过程。有人可以帮忙吗?感谢adavance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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