Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调度员没有';更新标签_C#_Wpf - Fatal编程技术网

C# 调度员没有';更新标签

C# 调度员没有';更新标签,c#,wpf,C#,Wpf,我有一个列表框,我放了一些文件,如果文件不是AVI,我会自动转换它,但我想当文件转换消息会写在标签上,文件现在被转换为另一种格式,现在发生在我身上的是,只有当程序完成转换后,它才更新标签,而不是在这个过程中 在所有修复之后: private void btnAdd_Click(object sender, RoutedEventArgs e) { btnPlay.IsEnabled = false; Stream checkStream = null; Microsoft

我有一个列表框,我放了一些文件,如果文件不是AVI,我会自动转换它,但我想当文件转换消息会写在标签上,文件现在被转换为另一种格式,现在发生在我身上的是,只有当程序完成转换后,它才更新标签,而不是在这个过程中

在所有修复之后:

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
    btnPlay.IsEnabled = false;
    Stream checkStream = null;
    Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
    openFileDialog.Multiselect = true;
    openFileDialog.InitialDirectory = "c:\\";
    openFileDialog.Filter = "All files (*.*)|*.*";
    openFileDialog.FilterIndex = 1;
    openFileDialog.Title = "Please Select Source File";

    if ((bool)openFileDialog.ShowDialog())
    {
        if ((checkStream = openFileDialog.OpenFile()) != null)
        {
            foreach (string file in openFileDialog.FileNames)
            {
                try
                {
                    FileInfo fileInfo = new FileInfo(file);
                    listBoxFiles.Items.Add(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(ProcessAviFiles)).Start();

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

    }

    if (listBoxFiles.Items.Count != 0)
    {
        btnClear.IsEnabled = true;
        btnPlay.IsEnabled = true;
    }

    listToRemove.RemoveRange(0, listToRemove.Count);

}

标签未更新,因为主UI线程正忙于执行其他操作

查看您的代码,您似乎正在主UI线程中运行AVI文件转换业务。您应该在单独的线程中运行这个耗时的任务,以确保UI保持响应

以下是问题的修复方法,将您的
foreach(listToRemove中的字符串文件){}
替换为:

Action aviConversion = new Action(() => { 
    if(listToRemove.Count == 0) return; // nothing to do
    lblStatus2.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                               new Action(() => { lblStatus2.Content = "Convert file to .AVI...";});
        );
     foreach (String file in listToRemove){
        FileInfo fileInfo = new FileInfo(file);
        editpcap = new (classes who convert the files)(fileInfo);
        String newFileName = editpcap._newFileName;
        Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                               new Action(() => { 
                               listBoxFiles.Items.Add(newFileName);
        }));
     }
     lblStatus2.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                               new Action(() => { lblStatus2.Content = "AVI file conversion finished...";});
});
// Run this action in a separate thread...
Task.Factory.StartNew(action, "beta");
(new System.Threading.Thread(ProcessAviFiles)).Start();
编辑使用
线程
而不是
任务
(OP不能使用
任务

将您的
foreach(listToRemove中的字符串文件){}
替换为:

Action aviConversion = new Action(() => { 
    if(listToRemove.Count == 0) return; // nothing to do
    lblStatus2.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                               new Action(() => { lblStatus2.Content = "Convert file to .AVI...";});
        );
     foreach (String file in listToRemove){
        FileInfo fileInfo = new FileInfo(file);
        editpcap = new (classes who convert the files)(fileInfo);
        String newFileName = editpcap._newFileName;
        Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                               new Action(() => { 
                               listBoxFiles.Items.Add(newFileName);
        }));
     }
     lblStatus2.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                               new Action(() => { lblStatus2.Content = "AVI file conversion finished...";});
});
// Run this action in a separate thread...
Task.Factory.StartNew(action, "beta");
(new System.Threading.Thread(ProcessAviFiles)).Start();

对主任务使用BackgroundWorker,对UI更新使用dispatcher

 backgroundWorker1.DoWork += worker_DoWork;
 backgroundWorker1.RunWorkerCompleted += worker_RunWorkerCompleted;
 backgroundWorker1.WorkerReportsProgress = true;
 backgroundWorker1.WorkerSupportsCancellation = true;          
 backgroundWorker1.ProgressChanged +=new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); 

我无法使用Task,因为我使用的是.Net 3.5,无法升级到.Net 4,因为它的计算机与我的工作无关,也不太容易。现在可以了,但程序在这里崩溃:listBoxFiles.Items.Add(editpcap.\u newFileName);在函数ProcessAviFiles()中,出现错误“调用线程无法访问此对象,因为另一个线程拥有它。”这是我在beginning@user979033对不起,我的错。我忘了把它也放在UI调用中。请看我的更新。非常感谢!顺便说一句,在转换过程中(“将文件转换为.AVI…”)可能会显示像圆圈进度之类的舒缓效果?@user979033是的,这是可能的。看看这篇文章,如果它对你有帮助的话,请别忘了接受它:)