My question is similar to C# Console App, slow to exit
But I have been able to isolate the cause to a certain extent.
To summarize: I have a console app that does a sqlbulkcopy.writetoserver. Everything seems to work fine. The data all gets inserted into the destination table. The stored procedures that work on the transferred data all work as expected.
The only strangeness is this: if there is no data to write then the app exits normally. But if writetoserver actually did write something - even one row, then the app takes an unusually long time to exit (at least one second sometimes more). In other words if I have a console.writeline just before the end of the main method, the message appears but then there is a delay of a second before the prompt returns. I cannot imagine this is expected behaviour.
I have had other console apps where the use of SqlConnection.ClearAllPools (as mentioned in that other question) has been the solution.
But this time ClearAllPools has made no difference. Having Pooling set to true and false in the sql connection string has made no difference. Ensuring the sqlbulkcopy is closed, the connection is closed etc, nothing I have done has made a difference to this strange delay.
I would be grateful for any ideas