2011-06-07 91 views
0
Imports CrystalDecisions.CrystalReports.Engine 
Imports CrystalDecisions.ReportSource 
Imports CrystalDecisions.Shared 
Imports System.Data.SqlClient 
Imports CrystalDecisions.Windows.Forms 
Imports System.Configuration 
Imports System.Threading 
Public Class frmPurchaseAnalysis 
    Dim dA1 As New SqlDataAdapter 
    Dim dS1 As New DataSet 
    Dim fmRptStr As String 
    Dim lCnt As Integer 
    Dim stritm As String 
    Dim strwrk As String 


    Private Sub frmPurchaseAnalysis_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 



     'Itemmtype 
     dA1 = New SqlDataAdapter("Select ItemTypeID,ItemType from ItemTypeRoot order by ItemTypeID ", cnnDbOne) 
     dA1.Fill(dS1) 
     cmbItemType.Items.Add("Select ItemType") 

     For lCnt = 0 To dS1.Tables(0).Rows.Count - 1 
      SmQryStr = Mid(dS1.Tables(0).Rows(lCnt)("ItemType"), 1, 30) 
      SmQryStr &= Space(31 - SmQryStr.Length) & dS1.Tables(0).Rows(lCnt)("ItemTypeID") 
      cmbItemType.Items.Add(SmQryStr) 
     Next 
     cmbItemType.SelectedIndex = 0 
     dS1.Clear() 
     dA1.Dispose() 

    End Sub 

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click 
     Me.Dispose() 
     Me.Close() 
    End Sub 

    Public Sub cmdView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdView.Click 
     Dim Obj1 As New Thread(AddressOf T1) 
     Dim Obj2 As New Thread(AddressOf T2) 
     Obj1.Start() 
     Obj2.Start() 

     Dim strwrk As String 
     strwrk = txtdays.Text 
     If cmbItemType.Text <> "Select ItemType" And cmbItemType.Text <> "" Then 
      stritm = Mid(cmbItemType.Text, 32) 
     End If 
     SmSqlCmd = New SqlCommand 
     SmSqlCmd.Connection = cnnDbOne 
     SmSqlCmd.CommandTimeout = 200 
     SmSqlCmd.CommandType = CommandType.StoredProcedure 
     SmSqlCmd.CommandText = "Rpt_PurchaseAnalysis" 
     SmSqlCmd.Parameters.AddWithValue("@itemType", stritm) 
     SmSqlCmd.Parameters.AddWithValue("@Targetdays", strwrk) 
     SmSqlCmd.Parameters.AddWithValue("@enddate", Format(DTP2.Value, "MM/dd/yyyy")) 

     dA1 = New SqlDataAdapter(SmSqlCmd) 
     dS1.Clear() 
     dA1.Fill(dS1) 



    End Sub 
    Private Sub T1() 
     Dim i As Integer 
     i += 1 
     PrgrsBar.Value = i 
     If PrgrsBar.Value = PrgrsBar.Maximum Then 

     End If 
    End Sub 

    Private Sub T2() 

     Dim SmReport As ReportDocument 
     Try 
      SmReport = New RptPurchaseAnalysis 
      SmReport.SetDataSource(dS1.Tables(0)) 
      SmReport.DataDefinition.FormulaFields("StartDate").Text = """" + Format(DTP1.Value, "dd-MMM-yyyy") + """" 
      SmReport.DataDefinition.FormulaFields("enddate").Text = """" + Format(DTP2.Value, "dd-MMM-yyyy") + """" 

      Dim frmRptViewer As New frmRptViewer 
      frmRptViewer.CrRptVwer1.ReportSource = SmReport 
      frmRptViewer.Show() 
      dS1.Clear() 

     Catch ex As Exception 
      MsgBox("Some Reporting Error Has Been Occured. " + vbCrLf + ex.Message, MsgBoxStyle.Information, "Sales Statistics Reporting") 
     End Try 

    End Sub 

End Class 

告訴我,我做錯了代碼?我想說明進度增加而我的報告已被載入vb.net中的進度條線程

回答

1

我看到(至少)在代碼中存在以下問題:

  • 你正在做冗長的操作(從數據庫加載數據)在主線程中。這是錯誤的。由於只有主線程才能更新UI,因此您需要在主線程中執行ProgressBar,在後臺線程中執行數據庫操作。

  • cmdView_Click開頭,你開始線程T2,其內容爲dS1。但是,dS1尚不可用,因爲它只是在主線程中填充。

  • 我不太明白你正在嘗試在T1的事:你初始化I(0),那麼你就增加1(i += 1),然後你執行一個空if聲明...

我的建議是,您可以深入瞭解在執行後臺操作時在WinForms中顯示進度條的在線教程,閱讀它直到完全理解它爲止(隨意提出更具體的問題在StackOverflow這裏,如果你不明白它的一部分),然後再試一次。

2

當你處理GUI和線程時,你必須小心。

訪問Windows窗體控件本質上不是線程安全的。如果您有兩個或更多個線程操縱控件的狀態,則可以強制控件進入不一致狀態。

在另一個線程中更新控件的最簡單方法是使用Background Worker。你可以閱讀更多在這article

class Program 
{ 
    static BackgroundWorker _bw = new BackgroundWorker(); 

    static void Main() 
    { 
    _bw.DoWork += bw_DoWork; 
    _bw.RunWorkerAsync ("Message to worker"); 
    Console.ReadLine(); 
    } 

    static void bw_DoWork (object sender, DoWorkEventArgs e) 
    { 
    // This is called on the worker thread 
    Console.WriteLine (e.Argument);  // writes "Message to worker" 
    // Perform time-consuming task... 
    } 
}