views:

453

answers:

3

I have a background thread updating an array. At timed intervals I call myDataGrid.Items.Refresh(). However nothing changes on the screen. But when I for instance click on the column heading of the data grid on the screen the information is actualized immediately.

I like to see the changes on the screen at timed intervals, for instance every 2 seconds. What am I missing?

Here is the code fragment in F# that shows the situation:

...

let win = new Window()
let grid = DataGrid()
grid.HeadersVisibility <- DataGridHeadersVisibility.All
grid.ItemsSource <- myArray
win.Content <- new ScrollViewer(Content=grid)
win.Show()
...
// Background thread A
//  updating myArray

... 

// Background thread B
let updateDataGrid = 
  grid.Items.Refresh()
  Thread.Sleep(5000)
  updateDataGrid

...

[<STAThread>]
do 
  let app = new Application()
  app.Run() |> ignore
+1  A: 

Have you tried a DispatcherTimer? (code below is in C#)

timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer1_Tick;

Prevent the usage of Thread.Sleep.

Zyphrax
Adding<pre> let timer = new DispatcherTimer(); timer.Interval <- TimeSpan.FromSeconds(1.); timer.Tick.Add (fun _ -> grid.Items.Refresh()) timer.Start()</pre>is doing the job!Thanks!
A: 

Can't use formatting in comments so here my response.

The timer actually works, if I write:

[<STAThread>]
do 
  let app = new Application()
  let timer = new DispatcherTimer()
  timer.Interval <- TimeSpan.FromSeconds(2.)
  timer.Tick.Add (fun _ -> grid.Items.Refresh())
  timer.Start()
  app.Run() |> ignore

However now the problem is almost reversed, it automatically updates until I click on any of the column headings to sort. After that there are no more refreshes.

However if I perform this trick:

  timer.Tick.Add (fun _ ->
                    grid.ItemsSource <- null
                    grid.ItemsSource <- myArray
                    grid.Items.Refresh())

it refreshes fine, but loses the sort ordering.

How to maintain the sort order? I can easily sort the arrray but I do like the user to sort by himself as well.

A: 

Since you're already using WPF, is it possible to turn that array into an ObservableCollection? Last I heard, the DataGrids support it. If the objects in the collection are already DependencyObjects, then their DependancyProperties should automatically update in the grid. If not, you can reinsert them into the collection manually.

YotaXP
The update rate of the array is very high, I really need a timed refresh of the data.