C# DLL doesn't work without MessageBox

Multi tool use
C# DLL doesn't work without MessageBox
I have a somewhat weird problem. I have a couple of DLLs that I need to use in order to write and read with an NFC reader.
This works:
LV3_InitializeSystem(5);
setAuthCode();
MessageBox.Show(""); // I immediately click and close the box
short ret = LV3_CheckIssuer();
Console.WriteLine(ret); // 0 - Success
This doesn't work:
LV3_InitializeSystem(5);
setAuthCode();
short ret = LV3_CheckIssuer();
Console.WriteLine(ret); // 90 - Card reader can not be detected.
This also doesn't work:
LV3_InitializeSystem(5);
setAuthCode();
Thread.Sleep(5000);
short ret = LV3_CheckIssuer();
Console.WriteLine(ret); // 90 - Card reader can not be detected.
I have no idea what might be the problem. I tried using threads running the initialize part with no success. How does showing a MessageBox
enable the initialization to complete but Thread.Sleep()
doesn't?
MessageBox
Thread.Sleep()
It worked! Even without sleep Application.DoEvents() works. Can you write the solution as an answer so i can accept it?
– Çağdaş Salur
Jul 3 at 8:31
It is pretty common for a library to expect a program to create an STA thread. The kind you get automatically in a WPF or Winforms app, but not in a console mode app. Deadlock is the expected outcome. Do avoid sledgehammer solutions, just give the library a happy home.
– Hans Passant
Jul 3 at 12:21
Will do Hans. Thanks!
– Çağdaş Salur
Jul 3 at 12:46
1 Answer
1
The DLL is apparently posting some required messages on the Windows message queue. In order for the messages to be processed, the message queue must be emptied.
One way of ensuring these messages are processed is to use Application.DoEvents(). Generally Application.DoEvents() is frowned upon - see https://blog.codinghorror.com/is-doevents-evil/ for reasons why this is.
There are other ways to solve this without using Application.DoEvents(), but it would probably require restructuring your code - for example using async/await with a Task.Delay.
Thanks for the answer! I coulnd't use Task.Delay because i am using .NET 3.5 but doEvents solved my problem.
– Çağdaş Salur
Jul 3 at 9:27
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Out of curiosity, try putting an Application.DoEvents() after the Thread.Sleep version.
– MineR
Jul 3 at 7:52