подскажите пожалуйста как можно передать таблицу из одного потока в другой. Суть в том что есть основная форма с datagrid, а в другом потоке формируется таблица для отображения в этом datagrid, как сделать так чтобы грид обновлялся по мере наполнения таблицы?
03.03.05 14:38: Перенесено модератором из '.NET' — AndrewVK
От:
Kuznero
Дата: 03.03.05 08:43
Оценка:
Здравствуйте, saintAlex, Вы писали:
A>подскажите пожалуйста как можно передать таблицу из одного потока в другой. Суть в том что есть основная форма с datagrid, а в другом потоке формируется таблица для отображения в этом datagrid, как сделать так чтобы грид обновлялся по мере наполнения таблицы?
Смотри в сторону Control.Invoke
Пока мы испытываем страх перед жизнью, мы будем бояться смерти.
Здравствуйте, Kuznero, Вы писали:
K>Смотри в сторону Control.Invoke
а можно чуть подробней. Я делаю так:
ShowProgressDelegate showProgress =
new ShowProgressDelegate(this.UpdateGrid);
public void FillTable()
{
//заполнение таблицы
this.table = tmptab;
dataGrid1.Invoke(showProgress);
}
private void UpdateGrid()
{
dataGrid1.SetDataBinding(table, null);
}
но при этом вылетает ошибка: Object reference not set to an instance of an object.
что не так?
От:
Kuznero
Дата: 03.03.05 08:59
Оценка:
а что за объект не проинициализирован?
Пока мы испытываем страх перед жизнью, мы будем бояться смерти.
Здравствуйте, Kuznero, Вы писали:
K>а что за объект не проинициализирован?
static void Main()
{
Application.Run(new Form1());
}
От:
Kuznero
Дата: 03.03.05 09:02
Оценка:
Здравствуйте, saintAlex, Вы писали:
A>Здравствуйте, Kuznero, Вы писали:
K>>а что за объект не проинициализирован?
A>static void Main()
A>{
A> Application.Run(new Form1());
A>}
Я могу посмотреть твой код. Пришли мне свой solution: ray(a)onego.ru.
Пока мы испытываем страх перед жизнью, мы будем бояться смерти.
От:
Kuznero
Дата: 03.03.05 10:16
Оценка:
using System;
using System.Threading;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TestMultiThread
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null ;
private DataTable table = new DataTable("table" );
private System.Windows.Forms.Label label1;
private Thread fill;
delegate
void ShowProgressDelegate();
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if (components != null )
{
components.Dispose();
}
}
base .Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this .dataGrid1 = new System.Windows.Forms.DataGrid();
this .button1 = new System.Windows.Forms.Button();
this .label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this .dataGrid1)).BeginInit();
this .SuspendLayout();
//
// dataGrid1
//
this .dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this .dataGrid1.DataMember = "" ;
this .dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this .dataGrid1.Location = new System.Drawing.Point(0, 0);
this .dataGrid1.Name = "dataGrid1" ;
this .dataGrid1.Size = new System.Drawing.Size(292, 232);
this .dataGrid1.TabIndex = 0;
//
// button1
//
this .button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this .button1.Location = new System.Drawing.Point(208, 240);
this .button1.Name = "button1" ;
this .button1.TabIndex = 1;
this .button1.Text = "Start" ;
this .button1.Click += new System.EventHandler(this .button1_Click);
//
// label1
//
this .label1.Location = new System.Drawing.Point(8, 240);
this .label1.Name = "label1" ;
this .label1.TabIndex = 2;
this .label1.Text = "label1" ;
this .label1.Visible = false ;
//
// Form1
//
this .AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this .ClientSize = new System.Drawing.Size(292, 273);
this .Controls.Add(this .label1);
this .Controls.Add(this .button1);
this .Controls.Add(this .dataGrid1);
this .Name = "Form1" ;
this .Text = "Form1" ;
this .Closing += new System.ComponentModel.CancelEventHandler(this .Form1_Closing);
((System.ComponentModel.ISupportInitialize)(this .dataGrid1)).EndInit();
this .ResumeLayout(false );
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void FillTable()
{
DataTable tab = new DataTable("qwer" );
ShowProgressDelegate showProgress = new ShowProgressDelegate(this .UpdateGrid);
for (int i = 0; i < 100; i++)
{
tab.Columns.Add(i.ToString());
}
for (int k = 0; k < 100; k++)
{
DataRow row = tab.NewRow();
for (int l = 0; l < 20; l++)
{
row[l] = k+l+10;
}
tab.Rows.Add(row);
object obj = new object ();
obj = tab;
lock (this )
{
this .table = tab;
}
label1.Invoke(showProgress);
Thread.Sleep(500);
}
fill.Join();
}
private void button1_Click(object sender, System.EventArgs e)
{
fill = new Thread(new ThreadStart(this .FillTable));
fill.Start();
}
private void UpdateGrid()
{
dataGrid1.DataSource = table;
dataGrid1.Invalidate();
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
fill.Join(10);
fill.Abort();
}
}
}Пока мы испытываем страх перед жизнью, мы будем бояться смерти.
От:
Аноним
Дата: 04.03.05 12:44
Оценка:
Вот простой пример работы с Invoke для контролов и делегатов.
private delegate void ShowProgressDelegate(int dig, int value);
private void button1_Click(object sender, System.EventArgs e)
{
int dig = 100;
for(int i = 0; i < dig; i += 10)
{
Thread.Sleep(100);
ShowProgress(dig, i + 10);
}
}
private void ShowProgress(int dig, int val)
{
if(this.progressBar1.InvokeRequired == false)
{
progressBar1.Maximum = dig;
progressBar1.Value = val;
}
else
{
ShowProgressDelegate showProgress = new ShowProgressDelegate(ShowProgress);
this.Invoke(showProgress, new object[] {dig, val});
}
}
Пока на собственное сообщение не было ответов, его можно удалить.
Удалить