Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.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
Vb.net 如何使函数与Control.CheckForIllegalCrossThreadCalls一起工作_Vb.net - Fatal编程技术网

Vb.net 如何使函数与Control.CheckForIllegalCrossThreadCalls一起工作

Vb.net 如何使函数与Control.CheckForIllegalCrossThreadCalls一起工作,vb.net,Vb.net,当我注释Control.CheckForIllegalCrossThreadCalls=False时,我的函数工作正常,但当未注释时,它会给出一个错误 我正在使用BackgroundWorker调用一个将值插入数据库的函数,需要设置Control.CheckForIllegalCrossThreadCalls=False才能使代码正常工作。现在,当我将其设置为false时,其中一个函数无法按预期工作。这里是错误 从字符串-0-1-22转换为类型“日期”无效 然而,当该功能正常工作时 Privat

当我注释Control.CheckForIllegalCrossThreadCalls=False时,我的函数工作正常,但当未注释时,它会给出一个错误

我正在使用BackgroundWorker调用一个将值插入数据库的函数,需要设置Control.CheckForIllegalCrossThreadCalls=False才能使代码正常工作。现在,当我将其设置为false时,其中一个函数无法按预期工作。这里是错误

从字符串-0-1-22转换为类型“日期”无效

然而,当该功能正常工作时

Private Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click

    If Not BackgroundWorker1.IsBusy = True Then
        BackgroundWorker1.RunWorkerAsync()
    End If

End Sub
我希望函数将2019-06-11作为日期插入到数据库中,该数据库无需控制即可正常工作。CheckForIllegalCrossThreadCalls=False

这是密码

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    count_rows()
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    MsgBox("Products imported successfully!", MsgBoxStyle.Information, "Import")
    Me.Close()
End Sub
Public Sub count_rows()
    import_attendance_sheet(1054)
End Sub
Private Sub import_attendance_sheet(ByVal id As Integer)
    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = id
    ProgressBar1.Value = 0
    Dim path As String = txtPath.Text
    Dim excel_connection As OleDbConnection
    Dim dt As DataTable
    Dim cmd As OleDbDataAdapter
    'Dim sql As String
    'Dim result As Boolean
    Dim emp_type_id As String = ""
    Dim branch_id As String = ""
    Dim bank_id As String = ""
    'Dim sheet_dates As New List(Of String)
    'excel_connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + path + ";Extended Properties=Excel 12.0 Xml; HDR=Yes;")
    excel_connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0 Xml;HDR=No;IMEX=1;';")
    cmd = New OleDbDataAdapter("SELECT * FROM [sheet$]", excel_connection)
    dt = New DataTable
    cmd.Fill(dt)
    'initialize symbol row
    Dim count As Integer = 6
    'Loop through dates(column/header)
    For Each column As DataColumn In dt.Columns
        Dim colum_name As String = dt.Rows(0)(column.ColumnName).ToString()
        'check if column cell is empty
        If colum_name = "" Then
            'Empty do nothing
        Else
            'increment symbol row by 1
            count = count + 1
            'MsgBox(count)
            'Loop through rows of a particular date/column/header
            For Each r As DataRow In dt.Rows
                'check row(empNo) cell is not empty & does not have a string
                If r(5).ToString() = "" Or r(5).ToString() = "COY #" Then
                    'Empty do nothing
                Else
                    'show date | Emp No | Name | symbol index
                    'MsgBox(colum_name & " " & r(5).ToString() & " " & r(6).ToString() & " " & r(count).ToString())
                    'do the calculation
                    Dim employ_id As String = get_employee_id(r(5).ToString)
                    Dim basic_salary As Decimal = get_employee_basic_salary(r(5).ToString)
                    Dim staff_type_id As String = get_employee_type_id(r(5).ToString)
                    Dim days_per_month As Integer = get_employee_days_per_month(staff_type_id)
                    Dim hours_per_day As Double = get_employee_hours_per_day(staff_type_id)
                    Dim hourly_rate As Double = basic_salary / days_per_month / hours_per_day
                    Dim daily_rate As Double = basic_salary / days_per_month
                    Dim normal_working_hrs As String = get_normal_working_hrs()
                    Dim shift_duration As String = get_shift_duration()
                    'get symbol id and its rate
                    Dim symbol_id As String = get_attendance_symbol_id(r(count).ToString)
                    Dim symbol_rate As Double = get_attendance_symbol_rate(symbol_id)
                    Dim symbol_deduction_status As String = get_symbol_deduction_status(symbol_id)
                    Dim td_amount As Double = 0
                    If symbol_deduction_status = "DEDUCT" Then
                        td_amount = hourly_rate
                    Else
                        td_amount = 0
                    End If
                    Dim overtime As Double = shift_duration - normal_working_hrs
                    Dim ot_amount As Double = overtime * hourly_rate * symbol_rate
                    Dim chaka As String = Date.Now.ToString("yyyy")
                    Dim tsiku As String = Date.Now.ToString("dd")
                    Dim tsiku_mawu As String = Date.Now.ToString("dddd")
                    Dim mwezi As String = Date.Now.ToString("MMMM")
                    ' ''insert values into DB
                    Sql = "INSERT INTO tbl_attendance (employee_id,time_in,time_out,total_hours_worked,overtime,ot_amount,td_amount,attendance_code_id,attendance_code,attendance_date,comment,year,date,day,month,hourly_rate,bsalary,ot_status) VALUES ('" & employ_id & "','" & 0 & "','" & 0 & "','" & shift_duration & "','" & overtime & "','" & ot_amount & "','" & td_amount & "','" & symbol_id & "','" & r(count).ToString & "','" & calc_attendance_date(colum_name) & "','import','" & chaka & "','" & tsiku & "','" & tsiku_mawu & "','" & mwezi & "','" & hourly_rate & "','" & basic_salary & "','" & symbol_rate & "')"
                    result = save_to_db(Sql)
                    ProgressBar1.Value = ProgressBar1.Value + 1
                    'If result Then
                    '    Timer1.Start()
                    'End If
                End If
            Next
        End If
    Next
End Sub
'******* Function which shows the error ****************
Public Function calc_attendance_date(ByVal value As String)
    Dim at_date As String = ""
    Dim current_month As String = frmMain.cmbMonth.Text
    Dim current_year As String = frmMain.cmbYear.Text
    Dim mwezi As String
    Dim chaka As String
    Dim format_day As String = ""
    Dim format_month As String = ""
    'Date.Now.ToString("yyyy-MM-dd")
    '****  find previous month
    'when its january
    If current_month = "January" And value >= 22 And value <= 31 Then
        mwezi = "12"
        chaka = Convert.ToInt32(current_year) - 1
        at_date = chaka & "-" & mwezi & "-" & value
    ElseIf current_month <> "January" And value >= 22 And value <= 31 Then
        mwezi = IntMonth() - 1
        'day
        If value < 10 Then
            format_day = "0" & value
        ElseIf value >= 10 Then
            format_day = value
        End If
        'format mwezi
        If mwezi < 10 Then
            format_month = "0" & mwezi
        ElseIf mwezi >= 10 Then
            format_month = mwezi
        End If
        chaka = current_year
        at_date = chaka & "-" & format_month & "-" & format_day
    End If
    '****  find current month
    If current_month = "January" And value >= 1 And value <= 21 Then
        mwezi = IntMonth()
        chaka = current_year
        'day
        If value < 10 Then
            format_day = "0" & value
        ElseIf value >= 10 Then
            format_day = value
        End If
        'format mwezi
        If mwezi < 10 Then
            format_month = "0" & mwezi
        ElseIf mwezi >= 10 Then
            format_month = mwezi
        End If
        at_date = chaka & "-" & format_month & "-" & format_day
    ElseIf current_month <> "January" And value >= 1 And value <= 21 Then
        mwezi = IntMonth()
        chaka = current_year
        'day
        If value < 10 Then
            format_day = "0" & value
        ElseIf value >= 10 Then
            format_day = value
        End If
        'format mwezi
        If mwezi < 10 Then
            format_month = "0" & mwezi
        ElseIf mwezi >= 10 Then
            format_month = mwezi
        End If
        at_date = chaka & "-" & format_month & "-" & format_day
    End If
    Return at_date
End Function

您不应该尝试从非UI线程与UI控件交互。但是,您可以使用与UI线程上的那些控件交互的代码。有几个地方你可以这样做

内部导入考勤表

相反:

Dim path As String
Me.Invoke(
    Sub()
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = id
        ProgressBar1.Value = 0
        path = txtPath.Text
    End Sub)
...
Me.Invoke(Sub() ProgressBar1.Value += 1)
Dim current_month As String 
Dim current_year As String 
Me.Invoke(
    Sub()
        current_month = frmMain.cmbMonth.Text
        current_year = frmMain.cmbYear.Text
    End Sub)
所有这些功能,如get_employee_id、get_employee_basic_salary、get_employee_type_id,也可以与UI交互,但您不提供它们。如果它们确实存在,即从文本框或其他东西返回值,那么您也需要在它们内部调用

内部计算考勤日期

相反:

Dim path As String
Me.Invoke(
    Sub()
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = id
        ProgressBar1.Value = 0
        path = txtPath.Text
    End Sub)
...
Me.Invoke(Sub() ProgressBar1.Value += 1)
Dim current_month As String 
Dim current_year As String 
Me.Invoke(
    Sub()
        current_month = frmMain.cmbMonth.Text
        current_year = frmMain.cmbYear.Text
    End Sub)
这个月的功能我们也不知道。同样,如果它从UI读取,您应该调用执行UI交互的代码

正确的方法是仅在绝对必要时调用,即当Control.invokererequired时。看看哪一个和你的非常接近,因为他们想用我。CheckForIllegalCrossThreadCalls=False BAD


更好的是,您可以编写自动化控件。InvokeRequired模式

请提供引发异常的代码是什么?很可能是与UI的一些交互,您可以将其包装在以下内容中:Me.InvokeSub*yourcodehere*以确保它在UI线程上运行。如果看不到BackgroundWorker中运行的实际违规代码,就无法提供更多帮助。任何建议您设置Control.CheckForIllegalCrossThreadCalls=False的人都是坏人。这有点像我们这一代人的所作所为。但不是真的:这是完整的代码