kejpa Posted December 13, 2004 Posted December 13, 2004 Hi, this has surely been asked many times but I can't find the appropriate thread... Sorry :( I have a datatable that gets updated from a separate timer thread. I want to see the current records of the datatable in a datagrid on a test form of mine. But when the timer has elapsed and the table is updated I get an error that the grid is not updated from the form thread. plz help me.... TIA /Kejpa Quote
kejpa Posted December 14, 2004 Author Posted December 14, 2004 (edited) Hey there, new day, new possibilities... To make errors? ;) So, I've done an extensive search.... and found the answer! (Sing of happiness sing of Joy, I was here before Killroy...) Use Invoke Dozens of places, all the same Use Invoke Soooo, I did and it failed. Back on the same spot again. Can anyone help me out here? I'm posting my test code, a form with a datagrid and a button. The button starts the damn thingy. :) Public Class Form2 Private oRnd As cRandomizer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click oRnd = New cRandomizer AddHandler oRnd.NewData, AddressOf UpdateDatagrid End Sub Private Sub UpdateDatagrid(ByVal dt As DataTable) Dim arg As Object = dt DataGrid1.BeginInvoke(New UpdateGrid(AddressOf UpdateDG), arg) End Sub Private Delegate Sub UpdateGrid(ByVal dt As DataTable) Private Sub UpdateDG(ByVal dt As DataTable) DataGrid1.DataSource = dt End Sub End Class Friend Class cRandomizer Private trdmodRandomMaker As Threading.Thread Private fmodAlive As Boolean Private dtmodRandom As DataTable Public Event NewData(ByVal dt As DataTable) Public Sub New() Dim dr As DataRow Dim i as Integer dtmodRandom = New DataTable dtmodRandom.Columns.Add(New DataColumn("Number", GetType(System.Int32))) dtmodRandom.Columns.Add(New DataColumn("Frequency", GetType(System.Int32))) For i = 1 To 10 dr = dtmodRandom.NewRow dr(0) = i dr(1) = 0 dtmodRandom.Rows.Add(dr) Next dtmodRandom.AcceptChanges() fmodAlive = True trdmodRandomMaker = New Threading.Thread(AddressOf RandomMaker) trdmodRandomMaker.Start() End Sub Private Sub RandomMaker() Dim iNumber As Integer Do While fmodAlive Threading.Thread.Sleep(1000) iNumber = CInt(10 * Rnd()) + 1 dtmodRandom.Rows(iNumber)(1) = CInt(dtmodRandom.Rows(iNumber)(1)) + 1 RaiseEvent NewData(dtmodRandom) Loop End Sub Protected Overrides Sub Finalize() fmodAlive = False trdmodRandomMaker = Nothing MyBase.Finalize() End Sub End Class ANY help appreciated! Regards /Kejpa edit: Alright, guilty. I am using VB2005, and it obviously doesn't work like .NET 1.1.... Edited December 14, 2004 by kejpa Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.