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
告诉我我的错误代码在哪里?我希望在我的报告加载时显示进度条增加
答案 0 :(得分: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...
}
}
答案 1 :(得分:1)
我在您的代码中看到(至少)以下问题:
您正在主线程中执行冗长的操作(从数据库加载数据)。那是错的。由于只有主线程可以更新UI,因此您需要在主线程中执行ProgressBar,在后台线程中执行数据库操作。
在cmdView_Click
的开头,您正在启动线程T2,其中显示dS1
。但是,dS1
尚不可用,因为它只是填充在主线程中。
我不太明白你在T1中尝试做什么:你初始化i(到0),然后你把它增加到1(i += 1
),然后你执行一个空{{ 1}}陈述......
我的建议是你深入了解在执行后台操作时在WinForms中显示进度条的在线教程之一,阅读它直到你完全理解它(随意在StackOverflow上问一个更具体的问题)在这里,如果你不理解它的一部分),然后再试一次。