“请等待”表单在调用时不呈现

时间:2016-11-05 16:26:03

标签: vb.net forms pleasewait

我正在编写一个可以同时执行大量过程的程序。为了防止用户认为程序崩溃,我决定添加一个“Please Wait”窗口。一切都很好,除了一件事:表格直到30秒后才完全渲染。在此之前,应该在表单中的“请等待......”文本只是一个白色矩形。

你能告诉我如何解决这个问题吗?谢谢!

编辑:

因为我假设每个人都有一个水晶球并且可以阅读我的虚拟心灵,我忘了包含代码>。&gt ;;;

这是:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim pleasewait As New TestEspere
    CenterForm(pleasewait)
    pleasewait.Show()
    Dim quantity, quantity_purchased As Integer
    Dim tracking_usps, lybro_id, recipient_name, order_id, order_item_id, payments_transaction_id, product_id, description, listing_id, sku,
           batch_id, buyer_email, buyer_name, address_1, address_2,
        city, state, zip, country, special_comments, upc, status_shipment, loc, tracking, etc, eff As String
    Dim total_price, peso, order_price, shipping_price As Decimal
    Dim purchase_date, payments_date As Date

    For i = 0 To DataGridView1.Rows.Count - 1
        On Error GoTo netx
        If IsDBNull(DataGridView1(0, i).Value) Then Exit For
        DataGridView1(33, i).Value = TrackingRequest(Replace(DataGridView1(32, i).Value, "'", ""))

        lybro_id = DataGridView1(0, i).Value
        order_id = DataGridView1(1, i).Value
        order_item_id = If(IsDBNull(DataGridView1(2, i).Value), "", DataGridView1(2, i).Value)
        payments_date = String.Format("{0:yyyy-MM-dd}", DataGridView1(3, i).Value.ToString)
        quantity = DataGridView1(4, i).Value
        payments_transaction_id = If(IsDBNull(DataGridView1(5, i).Value), "", DataGridView1(5, i).Value)
        product_id = DataGridView1(6, i).Value
        description = Replace(DataGridView1(7, i).Value, "'", "''")
        peso = DataGridView1(8, i).Value
        listing_id = If(IsDBNull(DataGridView1(9, i).Value), "", DataGridView1(9, i).Value)
        sku = DataGridView1(10, i).Value
        order_price = DataGridView1(11, i).Value
        shipping_price = DataGridView1(12, i).Value
        quantity_purchased = DataGridView1(13, i).Value
        total_price = DataGridView1(14, i).Value
        purchase_date = String.Format("{0:yyyy-MM-dd}", DataGridView1(15, i).Value.ToString )
        batch_id = If(IsDBNull(DataGridView1(16, i).Value), "", DataGridView1(16, i).Value)
        buyer_email = DataGridView1(17, i).Value
        buyer_name = Replace(DataGridView1(18, i).Value, "'", "''")
        recipient_name = Replace(DataGridView1(19, i).Value, "'", "''")
        address_1 = Replace(DataGridView1(20, i).Value, "'", "''")
        address_2 = If(IsDBNull(DataGridView1(21, i).Value), "", DataGridView1(21, i).Value)
        city = Replace(DataGridView1(22, i).Value, "'", "''")
        state = If(IsDBNull(DataGridView1(23, i).Value), "", Replace(DataGridView1(23, i).Value, "'", "''"))
        zip = DataGridView1(24, i).Value
        country = Replace(DataGridView1(25, i).Value, "'", "''")
        special_comments = If(IsDBNull(DataGridView1(26, i).Value), "", Replace(DataGridView1(26, i).Value, "'", "''"))
        upc = If(IsDBNull(DataGridView1(27, i).Value), "", DataGridView1(27, i).Value)
        etc = If(IsDBNull(DataGridView1(28, i).Value), "", DataGridView1(28, i).Value)
        status_shipment = DataGridView1(29, i).Value
        loc = Replace(DataGridView1(30, i).Value, "'", "''")
        eff = If(IsDBNull(DataGridView1(31, i).Value), "", DataGridView1(31, i).Value)
        tracking = If(IsDBNull(DataGridView1(32, i).Value), "", DataGridView1(32, i).Value)
        tracking_usps = Replace(DataGridView1(33, i).Value, "'", "''")

        Cmd.CommandText = String.Format("INSERT INTO re_orders_con_pesos(re_lybro_id, re_order_id, re_order_item_id, re_payments_date, re_quantity, re_payments_transaction_id, re_product_id,
                            re_description, re_peso, re_listing_id, re_sku, re_order_price, re_shipping_price, re_quantity_purchased, re_total_price, re_purchase_date, re_batch_id, re_buyer_email,
                            re_buyer_name, re_recipient_name, re_ship_address_1, re_ship_address_2, re_ship_city, re_ship_state, re_ship_zip, re_ship_country, re_special_comments, re_upc, re_etc, re_status_shipment,
                            re_location, re_eff, re_tracking, re_tracking_usps, re_status_email, re_status_db) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}',
                            '{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}','{29}','{30}','{31}','{32}', '{33}','{34}','{35}')", lybro_id, order_id, order_item_id, payments_date, quantity, payments_transaction_id,
                                        product_id, description, peso, listing_id, sku, order_price, shipping_price, quantity_purchased, total_price, purchase_date, batch_id, buyer_email,
                                        buyer_name, recipient_name, address_1, address_2, city, state, zip, country, special_comments, upc, etc, status_shipment, loc, eff, tracking, tracking_usps,
                                        ESTATUS_EMAIL_NUEVO, ESTATUS_DB_NUEVO)
        Cmd.Execute()
netx:
    Next

    pleasewait.Close()

    Beep()
End Sub

1 个答案:

答案 0 :(得分:1)

按钮点击事件处理程序的代码在UI的线程上运行。该线程应该仅处理您的显示,但您正在移动数据并使用它来将信息存储到您的数据库。尝试解决所有问题太错了,所以我和其他评论者提出了一个创可贴:Application.DoEvents。下面的位置将允许您的UI在处理datagridview中的每一行之前响应您的事件并进行更新。

此外,on error goto已弃用,因此我将其替换为较新的功能等效项Try .. Catch

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim pleasewait As New TestEspere
    CenterForm(pleasewait)
    pleasewait.Show()
    ' ... (code removed for brevity)

    For i = 0 To DataGridView1.Rows.Count - 1
        ' This call will allow your pleasewait form to update
        Application.DoEvents()
        Try
            If IsDBNull(DataGridView1(0, i).Value) Then Exit For
            DataGridView1(33, i).Value = TrackingRequest(Replace(DataGridView1(32, i).Value, "'", ""))
            lybro_id = DataGridView1(0, i).Value
            ' ... (code removed for brevity)
            tracking_usps = Replace(DataGridView1(33, i).Value, "'", "''")
            Cmd.CommandText = String.Format("INSERT INTO re_orders_con_pesos(re_lybro_id, re_order_id, re_order_item_id, re_payments_date, re_quantity, re_payments_transaction_id, re_product_id,
                        re_description, re_peso, re_listing_id, re_sku, re_order_price, re_shipping_price, re_quantity_purchased, re_total_price, re_purchase_date, re_batch_id, re_buyer_email,
                        re_buyer_name, re_recipient_name, re_ship_address_1, re_ship_address_2, re_ship_city, re_ship_state, re_ship_zip, re_ship_country, re_special_comments, re_upc, re_etc, re_status_shipment,
                        re_location, re_eff, re_tracking, re_tracking_usps, re_status_email, re_status_db) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}',
                        '{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}','{29}','{30}','{31}','{32}', '{33}','{34}','{35}')", lybro_id, order_id, order_item_id, payments_date, quantity, payments_transaction_id,
                                    product_id, description, peso, listing_id, sku, order_price, shipping_price, quantity_purchased, total_price, purchase_date, batch_id, buyer_email,
                                    buyer_name, recipient_name, address_1, address_2, city, state, zip, country, special_comments, upc, etc, status_shipment, loc, eff, tracking, tracking_usps,
                                    ESTATUS_EMAIL_NUEVO, ESTATUS_DB_NUEVO)
            Cmd.Execute()
        Catch
            ' you are ignoring exceptions, but this is where you should be handling them!
        End Try
    Next

    pleasewait.Close()
    Beep()
End Sub

在正常情况下,我想不出使用Application.DoEvents的理由。在您的情况下,没有它的解决方案需要对您的应用程序进行过多重写。