F
Frank Uray
Hi all
I have a big issue with threading in Windows Forms.
For example:
I have a .NET 2.0 (VS 2008) Windows Forms Solution
with one form in it (as default Form1).
I make this Form1 as MdiContainer.
In the Load Event I create a new thread and after this I
load a new Windows Form as Mdi Child.
In the thread I just read about 1mio rows in a DataTable
using standard DataReader.
Now because I have created a new thread, I would expect
the load time of this large amount of data would not affect
the loaded form, but it does ...
After starting the application, I grab the child form and I
just move it arround the Container form and it allways
hangs for some time ...
When I reduce the amount of data to just some rows,
the form feels free.
Here I send you my code and I would be very very happy
for a solution of this problem !
Thanks and best regards
Frank Uray
private static bool StopThread = false;
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Thread local_Thread = new
System.Threading.Thread(RunThread);
local_Thread.Start();
System.Windows.Forms.Form local_Form = new
System.Windows.Forms.Form();
local_Form.MdiParent = this;
local_Form.Show();
}
private void RunThread()
{
System.Data.SqlClient.SqlConnection local_SqlConnection = new
System.Data.SqlClient.SqlConnection();
System.Data.SqlClient.SqlCommand local_SqlCommand = new
System.Data.SqlClient.SqlCommand();
System.Data.SqlClient.SqlDataReader local_SqlDataReader;
local_SqlConnection.ConnectionString = @"Data
Source=MyServer\MyInstance;Initial Catalog=master;Integrated Security=true;";
local_SqlConnection.Open();
while (!StopThread)
{
// Command Object setzen
local_SqlCommand.CommandText = "SELECT * FROM SomeTable";
local_SqlCommand.CommandTimeout = 0;
local_SqlCommand.Connection = local_SqlConnection;
// DataReader
local_SqlDataReader = local_SqlCommand.ExecuteReader();
// DataTable füllen
System.Data.DataTable local_DataTable = new
System.Data.DataTable();
local_DataTable.BeginLoadData();
local_DataTable.Load(local_SqlDataReader);
local_DataTable.EndLoadData();
System.Threading.Thread.Sleep(2000);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{ StopThread = true; }
I have a big issue with threading in Windows Forms.
For example:
I have a .NET 2.0 (VS 2008) Windows Forms Solution
with one form in it (as default Form1).
I make this Form1 as MdiContainer.
In the Load Event I create a new thread and after this I
load a new Windows Form as Mdi Child.
In the thread I just read about 1mio rows in a DataTable
using standard DataReader.
Now because I have created a new thread, I would expect
the load time of this large amount of data would not affect
the loaded form, but it does ...
After starting the application, I grab the child form and I
just move it arround the Container form and it allways
hangs for some time ...
When I reduce the amount of data to just some rows,
the form feels free.
Here I send you my code and I would be very very happy
for a solution of this problem !
Thanks and best regards
Frank Uray
private static bool StopThread = false;
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Thread local_Thread = new
System.Threading.Thread(RunThread);
local_Thread.Start();
System.Windows.Forms.Form local_Form = new
System.Windows.Forms.Form();
local_Form.MdiParent = this;
local_Form.Show();
}
private void RunThread()
{
System.Data.SqlClient.SqlConnection local_SqlConnection = new
System.Data.SqlClient.SqlConnection();
System.Data.SqlClient.SqlCommand local_SqlCommand = new
System.Data.SqlClient.SqlCommand();
System.Data.SqlClient.SqlDataReader local_SqlDataReader;
local_SqlConnection.ConnectionString = @"Data
Source=MyServer\MyInstance;Initial Catalog=master;Integrated Security=true;";
local_SqlConnection.Open();
while (!StopThread)
{
// Command Object setzen
local_SqlCommand.CommandText = "SELECT * FROM SomeTable";
local_SqlCommand.CommandTimeout = 0;
local_SqlCommand.Connection = local_SqlConnection;
// DataReader
local_SqlDataReader = local_SqlCommand.ExecuteReader();
// DataTable füllen
System.Data.DataTable local_DataTable = new
System.Data.DataTable();
local_DataTable.BeginLoadData();
local_DataTable.Load(local_SqlDataReader);
local_DataTable.EndLoadData();
System.Threading.Thread.Sleep(2000);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{ StopThread = true; }