Invoke method for multi thread application?

柔情痞子 提交于 2019-12-02 11:49:27

Proper, thread-safe invocation is actually not as hard as one might think (not even for thread-safe events, but that's irrelevant for this question).

I would recommend you to use the normal Invoke method, such as Me.Invoke() (where Me is the current form, if not, use Form1 or whatever it's called instead). Using BeginInvoke() may be asynchronous but it stacks memory usage and can cause memory leaks if EndInvoke() is not called correctly.

If you target .NET 4.0 or higher you can simply do like this:

Me.Invoke(Sub() WaveformGraph.PlotWaveformAppend(data(i)))

However if you target .NET 3.5 or lower it requires a few more lines of code.

'Outside your Sub.
Delegate Sub WaveformAppendDelegate(ByRef WaveformGraph, ByRef data)

'Create a new sub.
Public Sub AppendData(ByRef WaveformGraph, ByRef data)
    WaveformGraph.PlotWaveformAppend(data)
End Sub

'Inside your sub, when you're going to invoke.
Me.Invoke(New WaveformAppendDelegate(AddressOf AppendData), WaveformGraph, data(i))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!