1

I have a ListBox which I put some files, if the file is not AVI I automatically converts it but I want when the files converting message will write on a label that the files are now converted to another format, i know i need use Dispatcher in order to update the UI thread but i use now Winform instead of WPF, and i need help with this. BTW i cannot use Task because i am using .Net 3.5

private void btnAdd_Click(object sender, EventArgs e)
{
    System.IO.Stream myStream;
    OpenFileDialog thisDialog = new OpenFileDialog();
    thisDialog.InitialDirectory = "c:\\";
    thisDialog.Filter = "All files (*.*)|*.*";
    thisDialog.FilterIndex = 1;
    thisDialog.RestoreDirectory = false;
    thisDialog.Multiselect = true; // Allow the user to select multiple files
    thisDialog.Title = "Please Select Source File";
    thisDialog.FileName = lastPath;
    List<string> list = new List<string>();

    if (thisDialog.ShowDialog() == DialogResult.OK)
    {
        foreach (String file in thisDialog.FileNames)
        {
            try
            {
                if ((myStream = thisDialog.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        listBoxFiles.Items.Add(file);
                        lastPath = file;
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }

        for (int i = 0; i < listBoxFiles.Items.Count; i++)
        {
            string path = (string)listBoxFiles.Items[i];
            FileInfo fileInfo = new FileInfo(path);

            if (fileInfo.Extension != ".AVI")
            {
                listToRemove.Add(path);
            }
        }

        (new System.Threading.Thread(sendFilesToConvertToPcap)).Start();

        foreach (string file in listToRemove) //remove all non .AVI files from listbox
        {
            listBoxFiles.Items.Remove(file);
        }
    }
}

this function need to change the Label:

        public void sendFilesToConvertToPcap()
        {
            if (listToRemove.Count == 0) // nothing to do
            {
                return;
            }

lblStatus2.Content = "Convert file to .AVI...";

            foreach (String file in listToRemove)
            {
                FileInfo fileInfo = new FileInfo(file);
                myClass = new (class who convert the files)(fileInfo);
                String newFileName = myClass.mNewFileName;
                listBoxFiles.Items.Add(myClass._newFileName);
            }

lblStatus2.Content = "Finished...";

        }
3
  • So you would like to run a long running process (aka conversion in the background) and update a label with the progress of the conversion process? Commented Mar 6, 2012 at 19:49
  • 1
    Use the BackgroundWorker component. Commented Mar 6, 2012 at 19:52
  • Hard to tell whats the question here. Long sample code. Localized. Close! Commented Mar 6, 2012 at 19:56

3 Answers 3

2

From your question, it seems that you'd like to convert several files. You may want to consider using the BackgroundWorker class and overwrite the DoWork and ProgressChanged events as described in this article. You can update the label and other controls in the ProgressChanged event.

Sign up to request clarification or add additional context in comments.

Comments

0
public void sendFilesToConvertToPcap()
{
   .....
   ....
   this.Invoke((MethodInvoker)delegate {
   lblStatus2.Text = "Convert file to .AVI..."; });

 ....
}

Comments

0

This is a very common requirement for long-running processes. If you don't explicitly call methods on a separate thread, they will automatically run on the main (see: UI) thread and cause your UI to hand (as I suspect you already know).

http://www.dotnetperls.com/backgroundworker

Here is an old, but excellent link with an example on how to use the background worker to handle the threadpool for you.

This way, you can just create a worker to manage running your conversion process on a separate thread, and I believe there is even an example for creating a process bar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.