gpt4 book ai didi

wpf - 线程是使用不接受参数的 ThreadStart 委托(delegate)创建的

转载 作者:行者123 更新时间:2023-12-03 13:05:14 25 4
gpt4 key购买 nike

我对 vb.net 中的 ThreadStart 委托(delegate)有疑问。我正在接手一个高级程序员的项目,他比我更有经验,所以我对他想做的事情有点迷茫。我正在尝试对线程进行一些研究,但是关于这个主题有很多,并且不想浪费我的时间在与我的错误无关的东西上。我将发布整个异常消息,希望有人可以指出我可以阅读的文章的方向以了解更多信息。

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

最佳答案

如果没有代码,我只能推测您正在使用 ThreadStart 创建线程然后调用 Start 的重载这需要一个参数。

您应该使用 ParameterizedThreadStart或直接调用Start没有参数的方法。

编辑 :就像我说的,线程启动方法调用与AddJob的定义不兼容.只需使用 Thread.Start() .

关于wpf - 线程是使用不接受参数的 ThreadStart 委托(delegate)创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12781922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com