StackOverflow in System.Windows.Forms.dll

0

Hello. I am developing a desktop application in C # that performs a Serial Port routine (Writing, Reading, Capturing Information, Displaying this information). However, there are times when a Stack Overflow error appears, but each time it is on a different line

Error:

  An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

I took a good part of it already developed by another person who worked here in the company.

Follow the complete code in the link: link

    
asked by anonymous 30.09.2015 / 22:54

1 answer

0

The STAThreadAttribute is essentially a requirement for Windows to perform communication with COM components. But WindowsForms does not use COM components. Therefore, access to serial ports and other components need to be added to the [STAThread] in your method.

Example:

[STAThread] //adicionado
private void buscaSensores()
{
...

MSDN demonstrates more details on using it.

  

STAThreadAttribute indicates that the COM threading model for the   application is single-threaded apartment. This attribute must be   present on the entry point of any application that uses Windows Forms;   if it is omitted, the Windows components might not work correctly. If   the attribute is not present, the application uses the multithreaded   apartment model, which is not supported for Windows Forms.

For more details: What does [STAThread] do?

    
02.10.2015 / 20:52