2

vb.netのThreadStartデリゲートについて質問があります。私はシニアプログラマーのプロジェクトを引き継いでいますが、彼は私よりもはるかに経験豊富なので、彼がやろうとしていたことに少し迷っています。私はスレッディングについていくつかの調査をしようとしていますが、このトピックについてはたくさんあり、ここでの私のエラーに関係のないもので時間を無駄にしたくありません。私は例外メッセージ全体を投稿します。うまくいけば、誰かが私がそれについてもっと学ぶために読むことができる記事の方向に私を向けることができます。

System.InvalidOperationException was unhandled
  Message=The thread was created with a ThreadStart delegate that does not accept a parameter.
  Source=mscorlib
  StackTrace:
       at System.Threading.Thread.Start(Object parameter)
       at LabelLibrary.LabelPrinter.Print(PrintQueue queue) in C:\Documents and Settings\bjorandb\Desktop\LabelPrintingService\LabelTemplates\clsLabelPrinter.vb:line 94
       at LabelLibrary.LabelPrinter.Print() in C:\Documents and Settings\bjorandb\Desktop\LabelPrintingService\LabelTemplates\clsLabelPrinter.vb:line 53
       at PrintApplyApplication.HomeController.PrintThread.Print() in C:\Documents and Settings\bjorandb\Desktop\LabelPrintingService\PrintApplyApplication\Controllers\HomeController.vb:line 85
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

これがエラーがスローされる関数ですが、それだけでどれだけ役立つかわかりません。

Public Function Print(ByVal queue As PrintQueue) As PrintResult
    Dim result As PrintResult = Nothing
    Dim job As PrintSystemJobInfo = Nothing
    Dim thread As Threading.Thread = Nothing

    result = New PrintResult
    If queue Is Nothing Then
        result.Status = "Can not find printer " & queue.Name & "."
    ElseIf PrintFilePath Is Nothing Then
        result.Status = "Print File Path Cannot be nothing."
    Else
        Try
            thread = New Threading.Thread(AddressOf AddJob)
            thread.SetApartmentState(Threading.ApartmentState.STA)
            thread.Start(queue) <--- (Exception is being thrown here!!)
            result.Status &= "Print Sent Successfully"
        Catch ex As PrintJobException
            If ex.InnerException.Message = "File contains corrupted data." Then
                result.Status &= "Could not generate a label with given xps file. Check the xps file format and if it is corrupted."
            End If
            result.Status &= "There was an error printing."
            result.ErrorMessage = ex.Message
        End Try
        If queue.IsOutOfPaper Then
            result.Status &= "The printer is out of Paper."
        End If
        If queue.IsPaperJammed Then
            result.Status &= "The Printer is jammed."
        End If
        If (queue.IsOutOfMemory) Then
            result.Status &= "The Printer is out of memory."
        End If
    End If
    Return result
End Function

そしてこれがAddJobメソッドです

Private Sub AddJob()
    Dim job As PrintSystemJobInfo = Nothing

    Try
        job = LocalPrintServer.GetDefaultPrintQueue.AddJob("Text", PrintFilePath, False)
        job.Refresh()
        While Not (job.IsCompleted Or job.IsDeleted)
            job.Refresh()
        End While
    Catch ex As PrintJobException
    End Try
    If System.IO.File.Exists(PrintFilePath) Then
        System.IO.File.Delete(PrintFilePath)
    End If
End Sub
4

3 に答える 3

1

コードがなければ、パラメータを使用してスレッドを作成し、ThreadStartそのオーバーロードを呼び出すことしか推測できません。Start

パラメータを使用せずにメソッドを使用するParameterizedThreadStartか、単に呼び出す必要があります。Start

編集:私が言ったように、スレッド開始メソッド呼び出しはの定義と互換性がありませんAddJob。単に使用してThread.Start()ください。

于 2012-10-08T13:02:50.707 に答える
0

AddJobメソッドには正しい署名がないため、オブジェクトパラメータが1つだけ必要です。

于 2012-10-08T13:16:19.943 に答える
0

あなたのAddJob潜水艦はパラメータを受け入れません。パラメータを受け入れるようにする場合は、タイプが単一のパラメータを受け入れるように署名を変更する必要がありますObject。もちろん、実際の効果を得るには、メッセージの本文を変更して、そのパラメーターを利用する必要があります。

于 2012-10-08T13:20:22.033 に答える