多线程应用程序的调用方法? [英] Invoke method for multi thread application?

查看:110
本文介绍了多线程应用程序的调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中存在一个错误,该错误与此处,此人遇到了同样的问题。我的应用程序是多线程的,其中工作线程正在UI上更新Waveformgraph。我认为这就是我的问题所在,为什么在运行应用程序时定期且偶尔在我的至少一个波形图对象中得到一个大的红色X。通过阅读和研究,我需要使用Invoke还是BeginInvoke方法?有人可以更好地解释并提供与我的代码相关的示例代码吗?到目前为止,我发现的样本仍然让我对如何执行此操作或需要执行的操作感到困惑。谢谢您的帮助。

I have a bug in my application which is the same as here which this person was running into the same problem. My application is multi threaded where the worker thread is updating the Waveformgraph on the UI. I believe that is where my problem is and why, periodically, and on occassion I get a big red X in at least one of my waveformgraph objects when running the application. From reading and research, I need to use an Invoke or BeginInvoke method? Can someone please explain better and provide a sample code that is relevant to my code? The samples that I've found so far still have me hazy on how I need to do this or what I need to do. Thank you for your help.

此代码位于主线程swScopeOnOff单击事件上。

This code is on the swScopeOnOff click event, main thread.

  thread2 = New System.Threading.Thread(AddressOf dataAcquiring)
  thread2.Start()

此代码在dataAcquiring子

This code is in dataAcquiring Sub

  Public Sub dataAcquiring()
    'While Scope switch is on, stream each Ai channel's data continuously to its respective WaveForm graph
    Do While swScopeOnOff.Value = True
            data = reader.ReadWaveform(readRate)
            i = 0
            For Each WaveformGraph In WFGS
                WaveformGraph.PlotWaveformAppend(data(i)) 'This line is updating the UI's waveform graphs
                i += 1
            Next
            i = 0
    Loop
End Sub


推荐答案

实际上是正确的线程安全调用并不像人们想象的那么难(即使对于线程安全事件,

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).

我建议您使用常规的 Invoke 方法,例如 Me.Invoke()(其中 Me 是当前表单,如果不是,则使用 Form1 或其他名称)。使用 BeginInvoke()可能是异步的,但是如果不调用 EndInvoke(),它会占用内存,并可能导致内存泄漏。

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.

如果您定位.NET 4.0或更高版本,则可以这样操作:

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

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

但是,如果您目标是.NET 3.5或更低版本,则它需要更多代码行。

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))

这篇关于多线程应用程序的调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆