×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

BackgrounWorker Pass Results Back

BackgrounWorker Pass Results Back

BackgrounWorker Pass Results Back

(OP)
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 --> c#

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                                                        
                            _FilePath = x;
                        }
                    }
                }
            }
                        
            LoadFile();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        } 

RE: BackgrounWorker Pass Results Back

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.

RE: BackgrounWorker Pass Results Back

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close