使用MultiThread将文件夹从一个位置移动到另一位置 [英] Moving folder from one place to another using MultiThread

查看:99
本文介绍了使用MultiThread将文件夹从一个位置移动到另一位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个按钮,当button.text等于"start"时,我在启动线程的地方调用了我的方法startThread().否则,我将在将布尔变量设置为false的地方调用我的方法stopthread().这种情况下只能使用一次.如果再次单击开始",则会显示以下错误:

Hi,
I have one button and when the button.text equals "start" I am calling my method startThread() where a thread is started. Otherwise my mehtod stopthread() gets called where I set a boolean variable to false. This scenario only works once. If I click start again the following error is shown:

"Thread is running or terminated; it cannot restart."



我该如何处理?

在此先感谢!



How do I handle this?

Thanks in advance!

推荐答案

AFAIK,SQL不支持线程.但是,原理很简单.本示例使用BackgroundWorker-这可能是您完成此类任务所需的全部:
AFAIK, SQL does not support threading. But, the principle is pretty simple. This example uses a BackgroundWorker - which is probably all you will need for this kind of task:
BackgroundWorker bw;
private void button1_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    if (b != null && b.Text == "Start")
        {
        bw = new BackgroundWorker();
        bw.WorkerSupportsCancellation = true;
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerAsync();
        b.Text = "Stop";
        }
    else
        {
        if (bw.IsBusy)
            {
            bw.CancelAsync();
            }
        b.Text = "Start";
        }
    }
void bw_DoWork(object sender, DoWorkEventArgs e)
    {
    BackgroundWorker worker = sender as BackgroundWorker;
    int i = 0;
    while (true)
        {
        if (worker.CancellationPending == true)
            {
            e.Cancel = true;
            break;
            }
        else
            {
            Console.WriteLine(i++);
            System.Threading.Thread.Sleep(500);
            }
        }
    }


线程可能会被挂起并恢复,但是一旦线程停止,就无法恢复原状.最好的选择是使用ThreadPool或在该按钮用于启动线程时创建一个新线程.如果我是我,则可以在名为startThread()的方法中创建一个新线程.
尝试重新启动已经停止的线程将完全导致您看到的错误.
再次阅读其他海报的建议后,我会说你坚持使用 OriginalGriff [ ^ ]在他的 answer [
A thread may be suspended and resumed, but once a thread has stopped it can not be brought back to life. Your best bet would be either to use ThreadPool or create a new thread when the button is meant to start the thread. If I were you I''d create a new thread in the method called startThread().

Trying to restart a thread that has already stopped will lead to exactly the error you have witnessed.
After reading again what the other posters suggested I''d say you stick to what OriginalGriff[^] proposed in his answer[^].

Best Regards,

—MRB


这篇关于使用MultiThread将文件夹从一个位置移动到另一位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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