C#|线程执行后程序不执行任何操作

发布于 2024-12-09 00:21:30 字数 932 浏览 0 评论 0原文

我的线程应该返回一个数组列表并将其放入文件中。
我的问题是它就停止了(至少我是这么认为的)。
Thread:

 ArrayList files = new ArrayList();
            Thread getF = new Thread(delegate()
            {
                files = GetFiles(path);
            });
            getF.Start();
            if (getF.ThreadState == ThreadState.Stopped)
            {
                MessageBox.Show(files.Count.ToString());
                foreach (string file in files)
                {
                    if (file != "")
                    {...

getFiles:

ArrayList results = new ArrayList();
            try
            {
             *loops**code*...
            results.Add(srl);//add file to arrFiles
            *end loops*

                MessageBox.Show("Complete");
                return results;
            }

该程序只是给我 MessageBox.Show("Complete"),然后什么都不做。 提前致谢。

My thread should return an array-list and put it into files.
My problem is that it just stops(at least that's how I see it).
Thread:

 ArrayList files = new ArrayList();
            Thread getF = new Thread(delegate()
            {
                files = GetFiles(path);
            });
            getF.Start();
            if (getF.ThreadState == ThreadState.Stopped)
            {
                MessageBox.Show(files.Count.ToString());
                foreach (string file in files)
                {
                    if (file != "")
                    {...

getFiles:

ArrayList results = new ArrayList();
            try
            {
             *loops**code*...
            results.Add(srl);//add file to arrFiles
            *end loops*

                MessageBox.Show("Complete");
                return results;
            }

The program just gives me the MessageBox.Show("Complete") and then does nothing.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧梦荧光笔 2024-12-16 00:21:30
        getF.Start();
        if (getF.ThreadState == ThreadState.Stopped)
        {
           //...
        }

if() 语句永远不会执行。线程完成其工作需要时间。您必须插入 getF.Join() 但这违背了使用线程的意义。

使用BackgroundWorker类。

        getF.Start();
        if (getF.ThreadState == ThreadState.Stopped)
        {
           //...
        }

That if() statement will never execute. It takes time for the thread to do its job. You would have to insert getF.Join() but that defeats the point of using a thread.

Use the BackgroundWorker class.

可爱咩 2024-12-16 00:21:30

AFAIK,Thread.Start() 在线程启动后立即返回。因此,在检查 ThreadState 之前,您应该等待它完成工作。 这个问题中讨论了等待线程完成

AFAIK, Thread.Start() returns immediately after thread is started. So before checking for ThreadState, you should wait for it to finish work. Waiting for thread to finish was discussed in This question

清风夜微凉 2024-12-16 00:21:30

在执行条件之前,线程很可能不会完成。也就是说,您有:

getF.Start();
if (getF.ThreadState == ThreadState.Stopped)

在您测试线程是否已停止之前,该线程可能甚至不会启动。您必须有某种方式让主线程知道另一个线程已完成其工作。

等待线程完成的典型方法是调用Join。也就是说:

getf.Start();
getf.Join(); // suspends main thread until the child thread is done

但如果你这样做,你就会阻塞你的用户界面。

使用多个线程的原因是,主线程(在您的例子中是 UI 线程)可以在其他线程执行其工作时执行其他操作。我怀疑你想要的是一个 BackgroundWorker 它将得到文件,然后在完成后在 RunWorkerCompleted 事件处理程序中执行一些操作。

It's quite likely that the thread won't be finished before you execute the conditional. That is, you have:

getF.Start();
if (getF.ThreadState == ThreadState.Stopped)

It's possible that the thread won't even start before you test to see if it's stopped. You must have some way for the main thread to know that the other one completed its work.

The typical way to wait for a thread to complete is by calling Join. That is:

getf.Start();
getf.Join(); // suspends main thread until the child thread is done

But if you do that, you block your user interface.

The reason for using multiple threads is so that the main thread (the UI thread, in your case) can do other things while the other threads are doing their work. I suspect what you want is a BackgroundWorker that will get the files, and then execute some actions after it's done--in the RunWorkerCompleted event handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文