简体   繁体   English

为什么我的GUI不更新?

[英]Why won't my GUI update?

I am working on creating a program that works its way through thousands of excel cells and splits the strings and forms and fills a chart based on that data. 我正在创建一个程序,该程序可以通过数千个excel单元运行,并拆分字符串和表格,并根据该数据填充图表。 The issue I am having is with the GUI. 我遇到的问题与GUI有关。 I have the program split into two separate classes. 我将程序分为两个单独的类。 One handles the excel data while the other is the MainWindow class for the WPF form. 一个处理excel数据,另一个处理WPF表单的MainWindow类。

The issue lies in that the MainWindow is not updating with the information as it goes along. 问题在于MainWindow不会随着信息的更新而更新。 It is supposed to update a dialog box and a progress bar showing as it goes through the steps. 它应该更新一个对话框和一个进度条,显示逐步进行的步骤。 It does not do anything, but finally appears at the end completely filled as though it had been doing this all along. 它什么也没做,但是最后似乎完全充满了它,好像它一直在做。

It was originally suggested to me that I split the duties into multiple threads and I have done that but it does not seem to work. 最初向我建议,我将职责划分为多个线程,并且已经做到了,但是似乎没有用。 Is it something in the way I handled the threads? 是我处理线程的方式吗? I can post as much of the code as requested but it is a lot so I didn't want to dump it all if it was something simple. 我可以按要求发布尽可能多的代码,但是它太多了,所以如果它很简单,我就不想全部丢弃。

Private Sub runButton_Click(sender As Object, e As RoutedEventArgs) Handles runButton.Click

    If FillCheck() = True Then
        program = New ExcelClass(dataText.Content.ToString, saveText.Content.ToString)
        Dim excelThread As New System.Threading.Thread(AddressOf program.beginProgram)
        excelThread.Start()
        checkUpdate()

    ElseIf FillCheck() = False Then
        MsgBox("You must select both the file to get the data from and the target folder and filename", MsgBoxStyle.OkOnly, "Fill Error")

    Else
        MsgBox("Something has gone terribly amiss. Panic", MsgBoxStyle.OkOnly, "Error")
    End If
End Sub

This code is just the part in the MainWindow class where the second thread is declared and initialized. 该代码只是MainWindow类中声明和初始化第二个线程的部分。 As I said, it does what it is supposed to do, but all behind-the-scenes when I want it shown in the GUI. 就像我说的那样,它可以完成预期的工作,但是当我希望它在GUI中显示时,所有的幕后操作都可以实现。

You can use BackgroundWorker class to "send" tasks in background (see MSDN guide for more information). 您可以使用BackgroundWorker类在后台“发送”任务(有关更多信息,请参见MSDN指南 )。 Especially you should look at the WorkerReportsProgress property (it should be set as true), while you are processing data you can report progress using ReportProgress method and you should subscribe to ProgressChanged event to reflect these changes to UI. 特别是,您应该查看WorkerReportsProgress属性(应将其设置为true),在处理数据时,可以使用ReportProgress方法报告进度,还应该订阅ProgressChanged事件以将这些更改反映到UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM