List reverts to null

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 1581 Views - Last Post: 24 November 2011 - 04:39 AM Rate Topic: -----

#1 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

List reverts to null

Posted 20 November 2011 - 09:12 AM

so basically i have a list, which is changed from another thread. this thread updates the list with folder names. and then i want to be able to access this list in the main form. but it seems that the list sets itself to null whenever the thread finishes with the list updating. im not really sure wth im doing wrong, mostly since i haven't worked to much with threads.

any help is appreciated.
here's the snippet for the thread.
    DirectoryInfo[] dirs = d.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                string fileName = dir.FullName.ToLower();
                if (fileName.Contains("xvid") && fileName.Contains("dvdrip") || fileName.Contains("720p") || fileName.Contains("1080p"))
                {
                    //if (!fileName.Contains("xxx") && !fileName.Contains("censored"))
                    //{
                        Match match = Regex.Match(fileName, pattern, RegexOptions.IgnoreCase);
                        Match match2 = Regex.Match(fileName, pattern2, RegexOptions.IgnoreCase);
                        if (!match.Success && !match2.Success)
                        {
                            fldrMovies.Add(dir.FullName);
                        }
                    //}
                }
                else
                {
                    list_folders(dir);
                }
            }


and so when this has run, it changes back to null :(

Is This A Good Question/Topic? 0
  • +

Replies To: List reverts to null

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4438
  • View blog
  • Posts: 12,308
  • Joined: 18-April 07

Re: List reverts to null

Posted 20 November 2011 - 10:36 AM

I think we are going to need more information like what type of list is fldrMovies? Is it a List, an ArrayList, some other kind of collection? Did you check if it was thread safe?

Showing us where you define the list along with more of the function itself would certainly help put this in context of what you are doing and how you are doing it.

Thanks! :)
Was This Post Helpful? 1
  • +
  • -

#3 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

Re: List reverts to null

Posted 20 November 2011 - 10:40 AM

that can be arranged :) first of all, while trying to fix it i came across something bout threadsafe, but i really dont know what it is, so im not sure about how to check this. also the list is defined in a class called thread_handler, here is the code for that(sorry for the mess)

 class Thread_Handler : Form1
    {
        
        DirectoryInfo dirSource = new DirectoryInfo("D:\\test_xvid");
        public DirectoryInfo dirMovie;
        public List<string> fldrMovies = new List<string>();
        
       
        Thread thrd_movie;
        movie_handler movie;

        public void run_thread(DirectoryInfo d_source, DirectoryInfo d_movie_target)
        {
            movie = new movie_handler();
            dirSource = d_source;
            dirMovie = d_movie_target;
            thrd_movie = new Thread(handle_movies);
            thrd_movie.Start();
        }

    
        public void handle_movies()
        {
            movie.list_folders(dirSource);
        }
    }

Was This Post Helpful? 0
  • +
  • -

#4 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: List reverts to null

Posted 20 November 2011 - 10:44 AM

How do you detect when the other thread has finished? Could it be asynchronic and you are trying to access before it is done?
Was This Post Helpful? 0
  • +
  • -

#5 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

Re: List reverts to null

Posted 20 November 2011 - 10:46 AM

well havent done much about that, currently i have 2 buttons, one who starts the thread and one who lists the contents of the list, and it doesnt really matter how long i wait, comes up null. and i did some debuging, checking the count of the list on certain points, and its filled right until the thread is "finished"
Was This Post Helpful? 0
  • +
  • -

#6 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: List reverts to null

Posted 20 November 2011 - 10:55 AM

Is 'fldrMovies' a local or global/public list?
Was This Post Helpful? 0
  • +
  • -

#7 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

Re: List reverts to null

Posted 20 November 2011 - 10:57 AM

its a public list. not sure if this is what im supposed to use, but public was the only that would let me access it from the movie_handler class.
Was This Post Helpful? 0
  • +
  • -

#8 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: List reverts to null

Posted 20 November 2011 - 11:21 AM

Where do you define your list? Are you defining a local list with the same name somewhere else that disappears when the thread ends?
Was This Post Helpful? 0
  • +
  • -

#9 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

Re: List reverts to null

Posted 20 November 2011 - 11:24 AM

as you can see above, i define the list in the thread_handler class, which then calls the function that populates the list. i was trying to find something that can monitor a variable and see what changes it, but didnt find anything.
Was This Post Helpful? 0
  • +
  • -

#10 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: List reverts to null

Posted 20 November 2011 - 11:33 AM

Hi,

My first thought is that you have defined another fldrMovies list in the movie_handler class, and that is the one you are adding the items to, not the one defined in the Thread_Handler class. In fact, if the code you posted in your opening post is code from the movie_handler class, I'd say that there is a good chance that that is what you are doing wrong :)
Was This Post Helpful? 0
  • +
  • -

#11 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

Re: List reverts to null

Posted 20 November 2011 - 11:37 AM

my movie handler doesnt define another list :( the list is defined in thread_handler and then added to in the movie_handler.
Was This Post Helpful? 0
  • +
  • -

#12 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: List reverts to null

Posted 20 November 2011 - 11:44 AM

In that case, I would say you may be creating another instance of Thread_Handler in your movie_handler class, and you are adding the items to *that* instance's fldrMovies list, rather than the calling Thread_Handler instance's list.

However, that wouldn't explain this line:

fldrMovies.Add(dir.FullName);,

of which suggests your adding items to a list in the movie_handler object, not the Thread_Handler object...

Could you post your entire movie_handler class please?

This post has been edited by CodingSup3rnatur@l-360: 20 November 2011 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#13 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

Re: List reverts to null

Posted 20 November 2011 - 11:45 AM

class movie_handler : Thread_Handler
    {
        #region Pattern_Strings
        string pattern = "S([0-9][0-9]*)";
        string pattern2 = "([0-9][0-9]*)x([0-9][0-9]*)";

        #endregion

        public void list_folders(DirectoryInfo d)
        {
            DirectoryInfo[] dirs = d.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                string fileName = dir.FullName.ToLower();
                if (fileName.Contains("xvid") && fileName.Contains("dvdrip") || fileName.Contains("720p") || fileName.Contains("1080p"))
                {
                    //if (!fileName.Contains("xxx") && !fileName.Contains("censored"))
                    //{
                        Match match = Regex.Match(fileName, pattern, RegexOptions.IgnoreCase);
                        Match match2 = Regex.Match(fileName, pattern2, RegexOptions.IgnoreCase);
                        if (!match.Success && !match2.Success)
                        {
                            fldrMovies.Add(dir.FullName);
                        }
                    //}
                }
                else
                {
                    list_folders(dir);
                }
            }
}
        }

This post has been edited by hellborg: 20 November 2011 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#14 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: List reverts to null

Posted 20 November 2011 - 11:58 AM

Right, that makes a lot more sense now :)

I'll try and explain what is happening...

So, your application starts, and an instance of your Thread_Handler class is created, of which has an empty list called fldrMovies. You then click the button on the form, and it creates a new movie_handler object. This movie_handler object does have its own list, as it inherits a list from the Thread_Handler class that you are subclassing. That list is completely separate from the one that was created in the Thread_Handler instance at the start of the application, as you are dealing with two separate instances: One Thread_Handler instance, and one movie_handler instance, each of which have their own list.

So, the upshot of it is, your are adding items to the wrong list :)

This post has been edited by CodingSup3rnatur@l-360: 20 November 2011 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#15 hellborg  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 19-March 11

Re: List reverts to null

Posted 20 November 2011 - 12:03 PM

oh, lol thats ... oh crap XD well so, where do i define the list so that this doesnt happen and i can still access it from the main form?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3