2

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.

funerr
  • 7,212
  • 14
  • 81
  • 129
  • 2
    You left out the most important part of your example -- what's inside the loop? – George Johnston Oct 10 '11 at 19:10
  • For starters, you'd want (1) some kind of wait/loop construct after you start `getF` to ensure the thread actually finishes before you try do do something with the `ArrayList`, and/or (2) a lock on `files` to prevent your code from trying to read & write to it at the same time. – David Oct 10 '11 at 19:13

3 Answers3

2
        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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

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

Community
  • 1
  • 1
Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68
1

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.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • First of all I would not use that because that's the reason I have created the thread for. Secondly How can I return the files array in the same manner while using a backGroundWorker?(I want a return statement and an ArrayList to retrieve it in the main method). – funerr Oct 10 '11 at 19:32
  • @agam360: The `BackgroundWorker` link has a good example of how to use it. If you want an `ArrayList`, declare it as a class-scope reference and populate it in the `BackgroundWorker`. – Jim Mischel Oct 10 '11 at 19:46
  • That is not good either. Because I will use another method that will obtain this data recursively, therefore I need that arraylist to be in class scope. – funerr Oct 10 '11 at 19:49
  • @agam360: I suspect the answers you get will be of the form, "use `BackgroundWorker`." If that's not acceptable, then you'll have to provide more information about what your program is doing and why you think `BackgroundWorker` won't work. Incidentally, I did say that you could define your `ArrayList` at class scope. Also, you might look into using `List` rather than `ArrayList`. – Jim Mischel Oct 10 '11 at 20:47