2

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?

InteXX
  • 6,135
  • 6
  • 43
  • 80
Çağdaş Salur
  • 1,360
  • 3
  • 14
  • 24
  • Out of curiosity, try putting an Application.DoEvents() after the Thread.Sleep version. – MineR Jul 03 '18 at 07:52
  • 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 03 '18 at 08: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](https://stackoverflow.com/a/21684059/17034). – Hans Passant Jul 03 '18 at 12:21
  • Will do Hans. Thanks! – Çağdaş Salur Jul 03 '18 at 12:46

1 Answers1

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.

MineR
  • 2,144
  • 12
  • 18