Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BackgrounWorker Pass Results Back

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
0
0
US
I have the following piece of code that searches a directory for a file containing a specific order number and return the full path. Depending on the directory it searches it can take some time so I like to run this using the BackgroundWorker but I am not sure how to pass the resulth (full path) back so I can use it on my next method.

Currently I am setthing a string variable _FilePath with the result (full path) and using it on the next method to load the file into a rich text box

If anyone can help mme get the information back from the BackGroundWorker it would be much appreciated

Thanks
RJL

Right now I do not have anything on the backgroundWorker1_DoWork or backgroundWorker1_RunWorkerCompleted

Code:
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;            
        }

        private void FindInFiles(string directoryArg, string containsTextArg, bool ignoreCaseArg, params string[] fileWildcardsArg)
        {   
            // This List accumulates files found.
            List<String> files = new List<string>(); 

            foreach (string fileWildcard in fileWildcardsArg)
            {
                string[] xFiles = System.IO.Directory.GetFiles(directoryArg, fileWildcard);

                foreach (string x in xFiles)
                {
                    // If file not already found...
                    if (!files.Contains(x)) 
                    {
                        bool containsText =
                            containsTextArg.Length == 0 ||
                            ignoreCaseArg ?
                            System.IO.File.ReadAllText(x).ToLower().Contains(containsTextArg.ToLower()) :
                            System.IO.File.ReadAllText(x).Contains(containsTextArg);

                        if (containsText)
                        {
                            // This file is a keeper. Add it to the list. and set is as a varialbe                                                        
                            [highlight #FCE94F]_FilePath = x;[/highlight]
                        }
                    }
                }
            }
                        
            LoadFile();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }
 
Hi RJL1,

just add
Code:
e.Result = x;
at the end of your backgroundWorker code, and then you can use a
Code:
var filename = (string)e.Result;
in your RunWorkerCompleted and call the next method from there.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Did you get it to work?

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top