Closed. This question needs to be more
focused。它当前不接受答案。
想要改善这个问题吗?更新问题,使它仅关注
editing this post的一个问题。
4年前关闭。
Improve this question
作为标题,当我在服务器上运行VB.NET应用程序时,如果它具有5个以上的线程,则该应用程序将在服务器上崩溃。
假设我有6个声明的线程:
Dim Thread1 As New Threading.Thread(AddressOf NoParametersSub1)
Dim Thread2 As New Threading.Thread(AddressOf NoParametersSub2)
Dim Thread3 As New Threading.Thread(AddressOf NoParametersSub3)
Dim Thread4 As New Threading.Thread(AddressOf NoParametersSub4)
Dim Thread5 As New Threading.Thread(AddressOf NoParametersSub5)
Dim Thread6 As New Threading.Thread(AddressOf NoParametersSub6)
然后,我开始前5个线程,并且工作正常:
Thread1.Start()
Thread2.Start()
Thread3.Start()
Thread4.Start()
Thread5.Start()
每当我到达第6个线程的代码部分时:
Thread6.Start()
应用程序崩溃,提示“(程序集名称)已停止工作”。
无论我放置了Try/Catch块,它都会崩溃,例如:
Try
Thread6.Start()
Catch ex As Exception
MsgBox(ex.Message)
End Try
或者:
Try
Thread6.Start()
Catch ex As Exception
End Try
它只是崩溃。
正如我在详细的崩溃信息中所看到的那样,异常是“System.InvalidOperationException”。
正如我在这里看到的:
https://msdn.microsoft.com/it-it/library/fd85b3df.aspx它说:
An System.InvalidOperationException is thrown when a method of an
object is called when the state of the object cannot support the
method call.
所以我把
System.Threading.Thread.Sleep(10000)
为了等待线程正确初始化(我猜?),然后我再次尝试,但仍然崩溃。
然后我读到以下内容:
The exception is also thrown when a method attempts to manipulate the
UI from a thread that is not the main or UI thread.
但不幸的是,我并没有尝试操纵UI。
然后在这里:
https://msdn.microsoft.com/it-it/library/6x4c42hc(v=vs.110).aspxInvalidOperationException:
This thread was created using a ThreadStart delegate instead of a
ParameterizedThreadStart delegate.
我不需要线程中的参数,但是我还是尝试了:
Thread6.Start(New Object() {})
但它仍然崩溃。
崩溃过程的示例:
Problem signature:
Problem Event Name: CLR20r3
Problem Signature 01: ProcessControlTest.exe
Problem Signature 02: 1.0.0.0
Problem Signature 03: 57cfbaee
Problem Signature 04: System
Problem Signature 05: 4.0.30319.34003
Problem Signature 06: 522ec39f
Problem Signature 07: 3083
Problem Signature 08: 2f
Problem Signature 09: System.InvalidOperationException
OS Version: 6.3.9600.2.0.0.272.7
Locale ID: 1033
Additional Information 1: 5861
Additional Information 2: 5861822e1919d7c014bbb064c64908b2
Additional Information 3: bb02
Additional Information 4: bb02c3b29c3f075104f1d0629e885cfc
该服务器安装了Windows Server 2012 R2,该应用程序已使用.NET Framework 4.5.2进行了编译,具有12个内核和4 GB的进程分配空间(总体上具有32 GB的RAM)。
我猜想Windows Framework在我的服务器中损坏了。
预先感谢您的帮助。
编辑:
不管NoParametersSub6包含什么,只要我启动6个线程,程序就会崩溃
尝试缠住thread.Start()不会对您有帮助。从您的方法NoParametersSub6中引发了异常。任何线程上未处理的异常都会使应用程序崩溃。通过一些日志记录将try/catch放入您的子目录中
Private Sub NoParametersSub6()
Try
'... my stuff
Catch ex As Exception
MessageBox.Show("Thread Terminating:" & ex.ToString())
End Try
End Sub
您应该在每次启动线程时都进行某种日志记录+错误处理,并以正常退出的方式进行处理。另外,正如汉斯·普拉森特(Hans Prassant)所提到的那样,您应该处理
AppDomain.CurrentDomain.UnhandledException
来捕获任何无法包装的情况。