在VB.NET中多次从文本文件中读取

时间:2015-10-18 08:18:05

标签: c# json vb.net multithreading combobox

我试图从包含JSON的文本文件中读取大约6~7 mb。有一个组合框,只要其中的项目发生变化,就会进行一次线程调用。在该线程中,首先读者读取文本文件,然后解码JSON并在多个列表框中显示。

一切正常,但完成线程需要将近10-15秒。假设这是因为从大文件中读取。但是我需要减少这个处理时间,因为组合框的值会在几分钟内发生变化。因此,不是每次都从文本文件中读取和解码,有没有办法在应用程序启动时读取文件并对其进行解码,或者当组合框中的值发生变化时,是否只显示所需的文本?

注意:解码的值每次都不同,它取决于组合框中的值。

到目前为止附上我的示例代码。

  Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        thread2 = New System.Threading.Thread(AddressOf countup2)
        thread2.Start()
    End Sub

Private Sub countup2()
        Try
            ListView1.Items.Clear()
            ListView2.Items.Clear()
            ListView3.Items.Clear()
            ListView4.Items.Clear()
            ListView5.Items.Clear()


            Dim allVersions = New List(Of Version)
            Dim JSONb = File.ReadAllText(Application.StartupPath & "\sapi.txt")
            Dim values = JsonConvert.DeserializeObject(Of JObject)(JSONb) 'Converts it to JObject

            For Each i In values("data").Children()

                If i.ToObject(Of Version).recharge_type = "val1" And i.ToObject(Of Version).circleid.Contains(ComboBox2.SelectedItem.ToString) And i.ToObject(Of Version).operatorid.Contains(ComboBox1.SelectedItem.ToString) Then
                    Dim lvi As New ListViewItem(i.ToObject(Of Version).recharge_amount)
                    lvi.SubItems.Add(i.ToObject(Of Version).recharge_validity)
                    lvi.SubItems.Add(i.ToObject(Of Version).recharge_longdesc)
                    ListView1.Items.Add(lvi)
                End If


                If i.ToObject(Of Version).recharge_type = "val2" And i.ToObject(Of Version).circleid.Contains(ComboBox2.SelectedItem.ToString) And i.ToObject(Of Version).operatorid.Contains(ComboBox1.SelectedItem.ToString) Then
                    Dim lvi As New ListViewItem(i.ToObject(Of Version).recharge_amount)
                    lvi.SubItems.Add(i.ToObject(Of Version).recharge_validity)
                    lvi.SubItems.Add(i.ToObject(Of Version).recharge_longdesc)
                    ListView2.Items.Add(lvi)
                End If


                If i.ToObject(Of Version).recharge_type = "val3" And i.ToObject(Of Version).circleid.Contains(ComboBox2.SelectedItem.ToString) And i.ToObject(Of Version).operatorid.Contains(ComboBox1.SelectedItem.ToString) Then
                    Dim lvi As New ListViewItem(i.ToObject(Of Version).recharge_amount)
                    lvi.SubItems.Add(i.ToObject(Of Version).recharge_validity)
                    lvi.SubItems.Add(i.ToObject(Of Version).recharge_longdesc)
                    ListView3.Items.Add(lvi)
                End If


                If i.ToObject(Of Version).recharge_type = "val4" And i.ToObject(Of Version).circleid.Contains(ComboBox2.SelectedItem.ToString) And i.ToObject(Of Version).operatorid.Contains(ComboBox1.SelectedItem.ToString) Then
                    Dim lvi As New ListViewItem(i.ToObject(Of Version).recharge_amount)
                    lvi.SubItems.Add(i.ToObject(Of Version).recharge_validity)
                    lvi.SubItems.Add(i.ToObject(Of Version).recharge_longdesc)
                    ListView4.Items.Add(lvi)
                End If


                If i.ToObject(Of Version).recharge_type = "val5" And i.ToObject(Of Version).circleid.Contains(ComboBox2.SelectedItem.ToString) And i.ToObject(Of Version).operatorid.Contains(ComboBox1.SelectedItem.ToString) Then
                    Dim lvi As New ListViewItem(i.ToObject(Of Version).recharge_amount)
                    lvi.SubItems.Add(i.ToObject(Of Version).recharge_validity)
                    lvi.SubItems.Add(i.ToObject(Of Version).recharge_longdesc)
                    ListView5.Items.Add(lvi)
                End If
            Next

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

0 个答案:

没有答案