变量值随着我启动的任务而改变 [英] Variable Value changes with my launched task

查看:94
本文介绍了变量值随着我启动的任务而改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新任务,以使用以下代码行获取一些信息:

Task.Factory.StartNew(Sub() ReadOPCData(TimerToRead), TaskCreationOptions.AttachedToParent)



它可以工作,但是传递给子例程(TimerToRead)的变量似乎被创建任务的例程更改了...然后任务内的输出值最终错误.

如果我不使用任务,而只是将其作为子例程调用,则它的工作原理与预期的完全一样.结果输出正确,并且变量未更改.但是,我想使用一个任务,因为调用例程对时间敏感,所以我想尽可能多地使用其他线程,这样我就可以返回处理时钟滴答并保持其准确性.

这是被调用例程中的一些代码,这些代码在作为任务被调用时不起作用:(这只是测试代码,AddErrItem将来会实际显示错误,现在它只是一个输出位置)

DispText = "Timer: " & CurTimerToRead.ToString
If Me.InvokeRequired Then
    Me.Invoke(System.Delegate.CreateDelegate(GetType(myInvokedMethod), Me, "AddErrItem"), New Object() {DispText})
Else
    AddErrItem(DispText)
End If



任何建议,不胜感激.我对多线程编程非常陌生,对VB.Net

还很陌生,当您使用线程时,由于操作系统和处理器的原因,输出可能是不确定的排程.

您的程序运行得很好,但是如果输出顺序不是您想要的,则您将不得不做其他事情.


有效的解决方案(可能不是唯一的,也许不是)甚至最好的解决方案)是在调用例程和被调用的子任务中创建Monitor.enter()和Monitor.exit()块,然后将参数放入调用例程的队列中并将其拉出子任务.
结果中可见线程的不确定性,但结果具有每个计时器的确切数量,并且线程在所需的时间限制内执行.
感谢Mehdi的所有帮助和评论.尽管他们没有直接提供解决方案,但他们受到了专家的赞赏.


I''m creating a new task to grab some information using this line of code:

Task.Factory.StartNew(Sub() ReadOPCData(TimerToRead), TaskCreationOptions.AttachedToParent)



It works but the variable being passed to the subroutine (TimerToRead) seems to get changed by the routine that created the task ... and then the value output within the task ends up being wrong.

If I don''t use a task and simply call it as a subroutine it works exactly as expected. The results output are correct and the variable doesn''t get changed. However I want to use a task because the calling routine is time sensitive so I want to use other threads as much as possible so I can return to processing my clock ticks and keeping them accurate.

Here is some code from the called routine that isn''t working when it''s called as a task: (This is just testing code AddErrItem will actually display errors in the future, right now it''s just an output location)

DispText = "Timer: " & CurTimerToRead.ToString
If Me.InvokeRequired Then
    Me.Invoke(System.Delegate.CreateDelegate(GetType(myInvokedMethod), Me, "AddErrItem"), New Object() {DispText})
Else
    AddErrItem(DispText)
End If



Any suggestions greatfully appreciated. I''m very new to multithreaded programming and fairly new to VB.Net

解决方案

When you are using threads, the output can be non deterministic because of OS and processor scheduling.

Your program is working perfectly fine, if however the output order is not what you want, you will have to do something else.


The solution that worked (likely not the only, and perhaps not even the best solution) was to create Monitor.enter() and Monitor.exit() blocks in the calling routine and in the called sub-task ... and put the parameter into a queue in the calling routine and pull it out in the sub-task.
The non deterministic nature of the threads was visible in the results but the results had exactly the number of each timer as was expected and the threads executed within the time restrictions that were required.
Thanks Mehdi for all your help and comments. While they didn''t directly provide the solution, they were certianly appreciated.


这篇关于变量值随着我启动的任务而改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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