Winforms and SQL Threading Problem

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; }
 
P

Peter Duniho

Frank said:
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.

What does "1mio" mean?
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 ...

While it's true that running that sort of lengthy process in a new
thread is a good idea, it still can definitely affect it to some degree,
depending on what the thread is doing.
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 !

Unfortunately, you have not provided a concise-but-complete code example
that reliably demonstrates the problem. No one can tell you exactly
what the problem is. But some things to look out for:

– Dependencies between your worker thread and the GUI thread, such as
a need to update the UI based on things that happen in the worker thread.

– Excessive and/or inefficient use of the CPU in your worker thread,
which can reduce the CPU time available to the UI thread in some situations.

– Poor performance due to memory swapping, which slows _everything_
on the computer down. This can happen especially when you are
attempting to create data structures that are much too large for the
amount of physical RAM you have installed on the computer.

If you want specific advice, you need to construct and post a
concise-but-complete code example that reliably demonstrates the problem.

Pete
 
F

Frank Uray

Hi Pete

It seams to be a memory problem.
Because of performance issues I need to preload
data, the sample code is just loading one million
rows into the datatable to simulate the problem.

Thanks and best regards
Frank Uray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top