Click here to Skip to main content
15,893,487 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using COM classes Pin
mav.northwind6-Sep-04 2:21
mav.northwind6-Sep-04 2:21 
GeneralRe: Using COM classes Pin
_Magnus_6-Sep-04 2:30
_Magnus_6-Sep-04 2:30 
QuestionFile.Exists question? Pin
jzb6-Sep-04 1:03
jzb6-Sep-04 1:03 
AnswerRe: File.Exists question? Pin
Tom Larsen6-Sep-04 4:13
Tom Larsen6-Sep-04 4:13 
AnswerRe: File.Exists question? Pin
Heath Stewart6-Sep-04 9:43
protectorHeath Stewart6-Sep-04 9:43 
AnswerRe: File.Exists question? Pin
Bill Dean6-Sep-04 13:36
Bill Dean6-Sep-04 13:36 
GeneralSmart Device Application with SQL Server Ce Pin
mathon6-Sep-04 0:28
mathon6-Sep-04 0:28 
GeneralDataGrid hangs when using threads Pin
tobiaseriksson6-Sep-04 0:00
tobiaseriksson6-Sep-04 0:00 
Hi,
I've created an app which adds rows to a table, which I view in a DataGrid. But the application hangs after a while, especially if I move the window around. Does anyone know how to get around this problem. I'm using a thread that once a second adds a row to the table, and I think that the DataGrid or something surrounding it is not threadsafe. I am quite surprised that it is not threadsafe, but maybe there is something underneath it all that explains it.

I've put together a testcase where this is happening, see below.

Any pointers are welcome.

Regards
Tobias

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace DataGridTest2
{
///
/// Summary description for Form1.
///

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

private System.Data.DataTable rLogDataTable = null;
private System.Data.DataSet rDataSet = null;

private DateTime rStartTimeStamp;

private int index = 0;

private Thread rResponseThread = null;

public Form1()
{
rStartTimeStamp = DateTime.Now;

//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
createLogDataTable();
dataGrid1.SetDataBinding( rDataSet, "Log" );

rResponseThread = new Thread( new ThreadStart( this.responseThread ) );
rResponseThread.Start();
}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
rResponseThread.Abort();
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.button1 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Location = new System.Drawing.Point(32, 48);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(624, 568);
this.tabControl1.TabIndex = 0;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.button1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(616, 542);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
//
// tabPage2
//
this.tabPage2.Controls.Add(this.dataGrid1);
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Size = new System.Drawing.Size(616, 542);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(272, 120);
this.button1.TabIndex = 0;
this.button1.Text = "Add Item To Grid";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 24);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(584, 496);
this.dataGrid1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(688, 646);
this.Controls.Add(this.tabControl1);
this.Name = "Form1";
this.Text = "Form1";
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
DateTime rTmp = DateTime.Now;
TimeSpan rTimeDiff = rTmp - rStartTimeStamp;
this.addToLog( ""+(index++), "test", rTimeDiff );
}

private void addToLog( string sID, string sEvent, TimeSpan rTS )
{
DataRow myDataRow = rLogDataTable.NewRow();
myDataRow["ID"] = sID;
myDataRow["Event"] = sEvent;
myDataRow["TimeDiff"] = rTS;
rLogDataTable.Rows.Add(myDataRow);
}

private void createLogDataTable()
{
// Create a new DataTable.
rLogDataTable = new DataTable("Log");

// Declare variables for DataColumn and DataRow objects.
DataColumn myDataColumn;

// Create new DataColumn, set DataType, ColumnName and add to DataTable.

//
// The ID
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "ID";
myDataColumn.AutoIncrement = false;
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
// Add the Column to the DataColumnCollection.
rLogDataTable.Columns.Add(myDataColumn);

//
// The Event
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "Event";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "ParentItem";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
// Add the column to the table.
rLogDataTable.Columns.Add(myDataColumn);

//
// The Time-string
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.TimeSpan");
myDataColumn.ColumnName = "TimeDiff";
myDataColumn.AutoIncrement = false;
myDataColumn.Caption = "ParentItem";
myDataColumn.ReadOnly = false;
myDataColumn.Unique = false;
// Add the column to the table.
rLogDataTable.Columns.Add(myDataColumn);

// Instantiate the DataSet variable.
rDataSet = new DataSet();
// Add the new DataTable to the DataSet.
rDataSet.Tables.Add(rLogDataTable);
}

private void responseThread()
{
string sThreadName = "ResponsePollingThread";
Thread.CurrentThread.Name = sThreadName;
string sTmp = null;
string sSeparator = "\n*************************************\n";
bool found = false;
string searchstring = null;

try
{
while( true )
{
DateTime rTmp = DateTime.Now;
TimeSpan rTimeDiff = rTmp - rStartTimeStamp;
this.addToLog( ""+(index++), "thread", rTimeDiff );
Thread.Sleep( 1000 );
}
}
catch( Exception e )
{
// Console.WriteLine( "Exception in "+sThreadName+", exception; "+e );
Console.WriteLine( "Updater-thread ended." );
}
}
}
}
GeneralRe: DataGrid hangs when using threads Pin
Roger Alsing6-Sep-04 3:47
Roger Alsing6-Sep-04 3:47 
GeneralRe: DataGrid hangs when using threads Pin
tobiaseriksson6-Sep-04 4:55
tobiaseriksson6-Sep-04 4:55 
GeneralChild Window with Focus *at same time as parent* Pin
Geekkit5-Sep-04 23:35
Geekkit5-Sep-04 23:35 
GeneralI have a problem,Creating xslt instructions Pin
teo_x5-Sep-04 22:37
teo_x5-Sep-04 22:37 
GeneralGet server time from remote server Pin
cbmdk5-Sep-04 22:19
cbmdk5-Sep-04 22:19 
GeneralProblems with huge files Pin
clatten5-Sep-04 22:15
clatten5-Sep-04 22:15 
GeneralRe: Problems with huge files Pin
Sebastian Schneider6-Sep-04 3:11
Sebastian Schneider6-Sep-04 3:11 
GeneralRe: Problems with huge files Pin
clatten6-Sep-04 10:07
clatten6-Sep-04 10:07 
GeneralEnabledChanged event of inputPanel of .NET CF error Pin
ting6685-Sep-04 21:25
ting6685-Sep-04 21:25 
GeneralDirectly deriving from MulticastDelegate Pin
Paul Selormey5-Sep-04 21:08
Paul Selormey5-Sep-04 21:08 
GeneralRe: Directly deriving from MulticastDelegate Pin
sreejith ss nair5-Sep-04 22:57
sreejith ss nair5-Sep-04 22:57 
GeneralRe: Directly deriving from MulticastDelegate Pin
Paul Selormey5-Sep-04 23:06
Paul Selormey5-Sep-04 23:06 
Generala question of Directx9 Pin
sssa20005-Sep-04 20:15
sssa20005-Sep-04 20:15 
GeneralRe: a question of Directx9 Pin
EssOEss6-Sep-04 5:01
EssOEss6-Sep-04 5:01 
GeneralC# equivalent of the /fx C++ compiler option Pin
Paul Selormey5-Sep-04 20:10
Paul Selormey5-Sep-04 20:10 
GeneralRe: C# equivalent of the /fx C++ compiler option Pin
leppie5-Sep-04 23:08
leppie5-Sep-04 23:08 
GeneralRe: C# equivalent of the /fx C++ compiler option Pin
Paul Selormey6-Sep-04 0:34
Paul Selormey6-Sep-04 0:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.