private void button1_Click(object sender, EventArgs e)
{
// Comment out the following line and the StartProcess Method works fine
List<SimpleTestTable> list = new PlainTestDBEntities().SimpleTestTables.ToList();
new Thread(StartProcess).Start();
while (true)
{ }
}
private void StartProcess()
{
Thread.Sleep(2000);
Console.WriteLine("I am executed totally fine.");
Process.Start("calc.exe");
Console.WriteLine("I am never executed if Entities are loaded in the calling method.");
}
For some reason, when the list is loaded from the DB in the given example, the StartProcess Method will hang after the Call of Process.Start("calc.exe");
. So the calculator will show up, but then the execution stops. If I comment out the DB loading line, everything works fine.
Any ideas?
Thank you.
Update: Apparently my instructions on how to reproduce wasn't clear enough. I hope this will help:
- Run the following script or do it manually which will create a DataBase in your SQL Server, with the Name PlainTestDB, add a table with the name SimpleTestTable, which has two columns. First ID with bigint and primarykey identity on, second Name nvarchar(50), add 3 values A, B and C.
Important: If you run the script make sure the go statement after the CREATE DATABASE statement is on the next line.
use master
go
CREATE DATABASE [PlainTestDB] CONTAINMENT = NONE ON PRIMARY ( NAME = N'PlainTestDB', FILENAME = N'C:\Data\PlainTestDB.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB ) LOG ON ( NAME = N'PlainTestDB_log', FILENAME = N'C:\Data\PlainTestDB_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB ) WITH CATALOG_COLLATION = DATABASE_DEFAULT
GO
use [PlainTestDB]
CREATE TABLE [dbo].[SimpleTestTable]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [Name] nvarchar NOT NULL, CONSTRAINT [PK_SimpleTestTable] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]
USE [PlainTestDB]
INSERT INTO [dbo].[SimpleTestTable] ([Name]) VALUES ('A'),('B'),('C')
Create a new Windows Forms Application in your Visual Studio. And add a Button with a click handler.
Add an ADO.NET Entity Data Model to your project with the name PlainTestDB.
Choose EF Designer from Database.
Add a connection string to your newly created database and check the box for saving it to your app config.
Choose Entity Framework 6.x
Check the Tables Checkbox and click Finish
Copy and paste the code above in your Form1.cs. Make sure you override the button1_Click method with the code you paste.
Add the following using statements
using System.Threading;
using System.Diagnostics;
Place a Breaking Point on the Line
Console.WriteLine("I am never executed if Entities are loaded in the calling method.");
Run the Program in Debug mode and click on the button
You will notice that the Calculator opens, but the breakpoint never gets hit
Play around with commenting out one line at a time and you will see
Commenting out the Database call will solve the issue with the hanging Thread