C# Wait for download to finish and check folder for files and display in list box ( form a dll )

elfenliedtopfan5 121 Reputation points
2020-12-14T18:37:06.787+00:00

ok i have a program where it connects to my ftp server downloads a file once downloaded and extracted i want to check the dir path that has just been created and add all the files in there to list box

but the download i have created is done in a dll, and my program creates the dir if does not exist and adds a zip once downloaded will extract this zip file and from there i want to show those files in a list box only issue i have here is i am trying to find a way of waiting for the download to complete before i look for those files,

the code i have in the dll is this.

private void wc_completed(object sender, AsyncCompletedEventArgs e)
        {
            _MessageResult.Instance.Append("Download Completed!");

            if (!Directory.Exists(Application.StartupPath + "\\Downloaded\\" + "//" +  Kaori_Base.SelectedDownloadName))
            {
                Directory.CreateDirectory(Application.StartupPath + "\\Downloaded\\" + "\\" + Kaori_Base.SelectedDownloadName + "\\" + Kaori_Base.SelectedDownloadName + ".zip");
                unzipupdate(Application.StartupPath + "//Downloaded//" + "//" + Kaori_Base.SelectedDownloadName + "\\" + Kaori_Base.SelectedDownloadName + ".zip");
                richTextBox1.Text = _MessageResult.Instance.Merge();
                XtraMessageBox.Show("Added : " + Kaori_Base.SelectedDownloadName + " To : " + Application.StartupPath + "//Downloaded//" + " And Unzipped.");
            }
            else
            {
                unzipupdate(Application.StartupPath + "\\Downloaded\\"  + Kaori_Base.SelectedDownloadName + "\\" + Kaori_Base.SelectedDownloadName + ".zip");
                richTextBox1.Text = _MessageResult.Instance.Merge();
                XtraMessageBox.Show("Added : " + Kaori_Base.SelectedDownloadName + " To : " + Application.StartupPath + "//Downloaded//" + " And Unzipped.");
            }




            listcollection();



            richTextBox1.Text = _MessageResult.Instance.Merge();
            this.Close();
        }

this is just what is triggered by the download form in the dll at the end of the download.

and in the main form i have this,

     setname();
            frmCDGPlayer cdgplayer = new frmCDGPlayer(@"C:\Users\elfen\Desktop\Karaoke Program\elfenliedtopfan5_update_fix\UpdateTest\bin\Debug\Downloaded\VSHXR\VSHXR\VSHXR-104 - Bloodhound Gang - The Bad Touch.zip");
            cdgplayer.Play();
have tryied to create a filewatcher class to watch for files and add them to listbox as it downloads does not work, comes up with the following error, 

System.InvalidOperationException: 'Cross-thread operation not valid: Control 'checkedListBoxControl1' accessed from a thread other than the thread it was created on.'

        public void filemonitor(string path)
        {
            FileSystemWatcher elfensystem = new FileSystemWatcher(path);

            elfensystem.EnableRaisingEvents = true;
            elfensystem.IncludeSubdirectories = true;

            elfensystem.Created += Elfensystem_Created;
            elfensystem.Changed += Elfensystem_Changed;


        }

        private void Elfensystem_Changed(object sender, FileSystemEventArgs e)
        {
            string paths = Application.StartupPath + "\\Downloaded\\" + Kaori_Base.SelectedDownloadName;
            var fileNames = Directory.GetFiles(paths);
            foreach (var fileName in fileNames)
            {
                // cbListBox.Items.Add(fileName); // Full path
                //elfenlist.Add(fileName.Split('\\').Last());
                add(fileName);
                 // Just filename
            }
        }



        public void add(string fileName)
        {
            checkedListBoxControl1.Items.Add(fileName.Split('\\').Last());
        }

Ideally i would like to access the run a function once the download has completed but all that stuff is accessed in the dll i created but not sure how to access the function downloaded complete form a dll if that is even possible

any help on this would be much appreciated

thanks in advance elfenliedtopfan5

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,838 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,224 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,031 Reputation points
    2020-12-14T19:52:54.863+00:00

    Hello @elfenliedtopfan5

    Basic pattern to follow

    listBox1.Invoke(new Action(() => listBox1.Items.Add(someItem)));  
    

    Generic extension method

    public static class Extensions  
    {  
    	public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : ISynchronizeInvoke  
    	{  
    		if (control.InvokeRequired)  
    		{  
    			control.Invoke(new Action(() => action(control)), null);  
    		}  
    		else  
    		{  
    			action(control);  
    		}  
    	}  
    }  
    

    Usage

    listBox1.InvokeIfRequired(listBox => {listBox.Items.Add(someItem);});  
    

0 additional answers

Sort by: Most helpful