C # Cantilever Thread

I have a program that uses a console and a graphical interface for very important aspects of my program. I assume that these two parts use the same thread.

The problem is that I use a multimeter with an output and to receive a bit from it, the program sends commands for this - it uses SCPI, these commands are launched through the console.

The problem is that whenever I send a command to the multimeter, it causes the program to stop responding until it returns the data, I know why, and I also know how to fix it, but I wonder if there is a better one way.

I am currently changing the default thread that the graphical interface runs on:

[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Thread applicationThread = new Thread(() => Application.Run(new Form1())); applicationThread.SetApartmentState(ApartmentState.STA); applicationThread.Start(); } 

This stops the program when I send the commands to the counter, but I am not 100% sure if it will create any other problems that I have not seen yet?

My questions:

  • Is there a better way to change the thread the GUI runs on?
  • If not, will this method create any problems that I have not yet seen?
  • Can I change the console stream instead?
+5
source share
1 answer
 [STAThread] static void Main() 

This is very unreasonable, you are breaking the STAThread contract. This means that you must pump the outline of the message. Application.Run () in a .NET program. You did it right for a thread that displays the user interface, but not for the main thread. The one you are supposed to use to talk with the device.

The worst problem is that it will look easy as if you can handle it. But this is a quiet time bomb that is ready to explode in your face at any time. Misbehavior varies from an arbitrary deadlock, especially from the unpleasant look that causes the .NET finalizer thread to hang. It happens when the CLR tries to free the shell of COM interop. It is very difficult to diagnose, your program eventually crashes when memory runs out. It takes some time, invariably longer than your patience checking the application. The random incorrect behavior of the counter object itself, usually diagnosed by a call to lock the object or the object that does not raise the expected event.

Finishing the finalizer deadlock is by far the worst problem, bad enough to make you change that. Focus on the real problem, this is the counter code that behaves incorrectly. Give him a safe harbor on his own, a properly designed STA jet that pumps. You will find the code for the boiler plate in this message . Or simply change the attribute to [MTAThread], which forces COM to provide a safe haven for the COM object on its own. But it’s better to take the bull with your horn so that you know what is happening, instead of relying on indestructible black magic.

I also highly recommend contacting the supplier or author of this component. Mesh COM servers should not behave this way. Something is wrong.

+3
source

All Articles