1

我正在编写一个同时执行很多进程的程序。为了不让用户认为程序崩溃了,我决定添加一个“请稍候”窗口。一切正常,除了一件事:表单直到 30 秒后才完全呈现。在此之前,应该在表单中的“请稍候...”文本只是一个白色矩形。

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

编辑:

因为我假设每个人都有一个水晶球并且可以读懂我的虚拟思维,所以我忘了包含代码 >.>;;

这里是:

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
4

1 回答 1

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. 在您的情况下,没有它的解决方案将需要对您的应用程序进行过多的重写。

于 2016-11-05T17:51:50.263 回答