VB MyParameters类似乎停止了

时间:2015-01-29 17:08:32

标签: vb.net

我将值传递给后台工作者和从后台工作者传递值。为此,我使用以下类来存储变量:

Class MyParameters
        Friend _QueryStr As String
        Friend _Value_CreatedDate As Object
        Friend _AccountID As Object
        Friend _Last_Login As Object
        Friend _Motto As Object
        Friend _Bio As Object
        Friend _pFavourite_Game As Object
        Friend _pFacebookLink As Object
        Friend _pInstagram As Object
        Friend _pTwitterLink As Object
        Friend _pTumblerLink As Object
        Friend _pYoutubeLink As Object
        Friend _lblpYoutubeLink As String
        Friend _lblpTumblerLink As String
        Friend _lblpTwitterLink As String
        Friend _lblpInstagram As String
        Friend _lblFacebookLink As String
        Friend _lblpFavourite_Game As String
        Friend _lblBio As String
        Friend _lblMotto As String
        Friend _lblLast_Login As String
        Friend _AccessCode As String
        Friend _TargetUGPoints As String
        Friend _Stream As New MemoryStream()
        Friend _Image As Byte()
        Friend _DoB_Value As Object
        Friend _DoB As Date
        Friend _Years As Integer
        Friend _Months As Integer
        Friend _Cautions_Value As Integer
        Friend _Cautions As Integer
        Friend _Bitmap As New Bitmap(_Stream)
        Friend _Con As New SqlConnection("Data Source =" & My.Settings.ServerIP & ";Initial Catalog=Atlas;Integrated Security=False;User=" & My.Settings.UserName & ";Password=leaders132;")
        Friend _MyCMD As New SqlCommand(_QueryStr, _Con)
    End Class

我在表单加载事件中调用了这样的后台工作程序:

 Private Sub cdb_ProfileViewer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Loadinglbl.Visible = True
        SpinningProgress1.Visible = True
        MsgBox("Starting MyParamters")
        Dim m As New MyParameters
        MsgBox("My Parameters set to m")
        bw.RunWorkerAsync(m)
    End Sub

但是,该应用程序似乎无法到达后台工作人员。没有冻结或错误,它什么都不做。我仍然像背景工作者一样控制着这个形式,但我认为它甚至没有开始,因为它没有运行任何msgbox。例如,我在代码中添加了Msgbox,以便让我知道代码已到达哪个阶段。如果我将放在 Dim m as New Parameter之前,,而不是之后,它就可以了。

我的背景工作者,请注意我已经省略了我的代码,因为它太过分了:

Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        Dim m As MyParameters = DirectCast(e.Argument, MyParameters) 'Convert the generic Object back into a MyParameters object
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        e.Result = m

'Do some time-consuming work on this thread

end sub

My WorkerCompleted Event,也没有代码:

  Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
        Dim m As MyParameters = DirectCast(e.Result, MyParameters) 'Convert the generic Object back into a MyParameters object
        MsgBox("Reached Worker Complete")

        'Called when the BackgroundWorker is completed.

end sub

我的BW公共类代码:

   Private bw As BackgroundWorker = New BackgroundWorker
    Private Delegate Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)

负责BW的Public Sub:

  Public Sub New()
        InitializeComponent()

        bw.WorkerReportsProgress = False
        bw.WorkerSupportsCancellation = False
        AddHandler bw.DoWork, AddressOf bw_DoWork
        AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
    End Sub

有什么东西可以跳出来吗?正如我所说,我觉得这是Class MyParamters中的一些东西。

1 个答案:

答案 0 :(得分:1)

不幸的是,Load()事件吞噬了您的异常。将该代码移动到Constructor或Shown()事件,问题变得明显:

    Friend _Bitmap As New Bitmap(_Stream)

您无法从空流中创建位图...

要重现的最少代码:

Public Class MyParameters
    Friend _Stream As New MemoryStream()
    Friend _Bitmap As New Bitmap(_Stream)
End Class

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim M As New MyParameters
End Sub