gpt4 book ai didi

multithreading - F# Winforms 图表异步更新

转载 作者:行者123 更新时间:2023-12-03 13:16:01 25 4
gpt4 key购买 nike

我正在尝试在 winforms 中创建一个图表,该图表将数据绑定(bind)到内存中的列表,并随着列表的变化而动态更新。这是我的代码:

open System
open System.Linq
open System.Collections
open System.Collections.Generic
open System.Drawing
open System.Windows.Forms
open System.Windows.Forms.DataVisualization
open System.Windows.Forms.DataVisualization.Charting

let link = new LinkedList<double>()
let rnd = new System.Random()
for i in 1 .. 10 do link.AddFirst(rnd.NextDouble()) |> ignore
let series = new Series()
let chart = new System.Windows.Forms.DataVisualization.Charting.Chart(Dock = DockStyle.Fill, Palette = ChartColorPalette.Pastel)

series.Points.DataBindY(link)

let form = new Form(Visible = true, Width = 700, Height = 500)
form.Controls.Add(chart)

let formloop = async {
while not chart.IsDisposed do
link.AddFirst((new System.Random()).NextDouble()) |> ignore
link.RemoveLast()
}
do
Async.StartImmediate(formloop)
Application.Run(form)

Console.WriteLine("Done")
Console.ReadLine() |> ignore

异步似乎有效,但图表从不显示任何内容。它只是显示一个空白窗口。我究竟做错了什么?

最佳答案

LinkedList<T>无法表示它已更新,所以 Chart无法知道何时重绘自己。

为了数据绑定(bind)更新 View ,源列表必须实现 IBindingList 并在内容发生变化时引发适当的事件。

另外,我必须指出,从非 UI 线程(例如代码中的 chart.IsDisposed)直接访问 UI 属性/方法是危险的。在 WinForms 中,这个限制实际上很少被强制执行,所以有时这似乎工作得很好,只是后来在客户的机器上崩溃而无法附加调试器。

关于multithreading - F# Winforms 图表异步更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29359217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com