在科学的模拟程序中帮助多线程和状态 [英] Help with multithreading and state in a scientific simualtion program

查看:62
本文介绍了在科学的模拟程序中帮助多线程和状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个执行蒙特卡罗模拟的程序,并且我很难找到如何使线程模型正确的问题。我有一个

模拟类,其中包含所有模拟数据和方法,用于运行x / b $ b模拟x次扫描。该对象的一个​​实例是在我的主窗体的类中声明的
。主窗体处理图形

(它显示在窗体上移动的粒子)。我现在正试图将工作分成两部分,以便UI线程与运行的线程分开

模拟prgoram。我也可能会在

中添加一个表单,允许按图表查看相同的数据,所以我需要从多个表单中访问模拟

对象。模拟对象有一个事件,当模拟移动一个粒子时触发该事件,

是监听该事件并自行更新的形式。问题是

当我在一个单独的线程中运行模拟时我得到异常当

试图访问图形对象进行绘图时(因为我正在访问UI
$ b来自nonUI线程的$ b元素。


有人可以帮助我找出一个好的程序结构吗?

Im writing a program that does monte carlo simulation and im having
trouble figuring out how to get the threading model right. I have a
simulation class which contains all simulation data and methods to run
the simulation for an x number of sweeps. An instance of this object is
declared in the class of my main form. The main form handles graphics
(it shows the particles moving on the form). I am now trying to split
up the work so that the UI thread is separate from the thread running
the simulation prgoram. I will also probably be adding a form to
allowing viewing of the same data by chart, so i need the simualtion
object to be accessible from multiple forms. The simulation object has
an event which is triggered when the simulation moves a particle, with
the form listening for that event and updating itself. The problem is
when i run the simulation in a separate thread i get exceptions when
trying to access graphics objects for drawing (because im accessing UI
elements from a nonUI thread).

Can someone help me out to figure out a good structure for the program?

推荐答案

要绘制,你必须在创建表单的线程上调用调用(或控制你的绘图)。


控制。如果代码在非ui线程上执行,InovkeRequired将告诉您

Control.Invoke(新的MethodInvoker(ParameterlessMethod))将在ui-thread上调用一个方法来放置你的绘图

代码:


if(ctrl.InvokeRequired)

ctrl.Invoke(new MethodInvoker(DoDraw));

其他

DoDraw();


-

Dave Sexton
dave @ www..jwaonline..com

------------------------------------------------ -----------------------

< mg ****** @ gmail.com>在留言新闻中写道:11 ********************** @ g49g2000cwa.googlegr oups.com ...
To draw you''ll have to invoke calls on the thread that created the form (or control your drawing to).

Control.InovkeRequired will let you know if the code is executing on a non-ui thread
Control.Invoke(new MethodInvoker(ParameterlessMethod)) will invoke a method on the ui-thread where you should place your drawing
code:

if (ctrl.InvokeRequired)
ctrl.Invoke(new MethodInvoker(DoDraw));
else
DoDraw();

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
<mg******@gmail.com> wrote in message news:11**********************@g49g2000cwa.googlegr oups.com...
我正在写一个程序这是蒙特卡罗模拟和我有麻烦搞清楚如何正确的线程模型。我有一个
模拟类,它包含所有模拟数据和方法来运行模拟x次扫描。在我的主窗体的类中声明了此对象的实例。主窗体处理图形
(它显示在窗体上移动的粒子)。我现在正在尝试拆分工作,以便UI线程与运行模拟prgoram的线程分开。我也可能会添加一个表单来允许按图表查看相同的数据,所以我需要从多个表单中访问模拟对象。模拟对象具有一个事件,当模拟移动粒子时触发该事件,其形式是监听该事件并自行更新。问题是当我在一个单独的线程中运行模拟时,我试图访问图形对象进行绘图时会遇到异常(因为我从非UI线程访问UI
元素)。

有人可以帮我找出一个好的程序结构吗?
Im writing a program that does monte carlo simulation and im having
trouble figuring out how to get the threading model right. I have a
simulation class which contains all simulation data and methods to run
the simulation for an x number of sweeps. An instance of this object is
declared in the class of my main form. The main form handles graphics
(it shows the particles moving on the form). I am now trying to split
up the work so that the UI thread is separate from the thread running
the simulation prgoram. I will also probably be adding a form to
allowing viewing of the same data by chart, so i need the simualtion
object to be accessible from multiple forms. The simulation object has
an event which is triggered when the simulation moves a particle, with
the form listening for that event and updating itself. The problem is
when i run the simulation in a separate thread i get exceptions when
trying to access graphics objects for drawing (because im accessing UI
elements from a nonUI thread).

Can someone help me out to figure out a good structure for the program?



如果计算得那么多。问题是我的模拟对象没有
有权访问它运行的表单,所以它无法使用invoke。另外

....我注意到一些奇怪的东西和id就像一个解释......对于

某些原因如果我在运行时在表单中运行Invalidate() br />
nonui线程,form_paint在ui线程中自行执行。如何发生这种情况是什么?为什么?

if figured that much out. the problem is my simulation object does not
have access to the form it is running in, so it cant use invoke. Also
.... im noticingf something strange and id like an explanation ... for
some reason if i run Invalidate() in the form while running on the
nonui thread, the form_paint executes in the ui thread by itself. how
does this happen and why?


WinForms控件使用消息循环。调用Invalidate()时,paint消息将发送给自身。在ui线程上收到消息

,其余的都是历史记录。


我建议在模拟对象的构造函数中添加一个参数:


公共模拟器(表单所有者)

{

this.owner = owner;

}


如果您需要多个表单来访问该对象,可能会出现一个Singleton模式:


公共类模拟器

{

//线程安全单例模式实现

public static readonly模拟器实例=新模拟器();


//禁止从外部类构造对象

私有模拟器(){}


public void Run(表单所有者)

{

// Todo:开始新线程并在指定表格上运行模拟(所有者)

}


[...]


Simulator.Instance.Run(this); //这是表格参考


您可以公开表格类可以在实例上收听的事件。


-

Dave Sexton
dave @ www..jwaonline..com

------------------------------------------------ -----------------------

" Michael Gorbach" <毫克****** @ gmail.com>在留言新闻中写道:11 ********************** @ o13g2000cwo.googlegr oups.com ...
WinForms controls use the message loop. The paint message is sent to itself when you invoke Invalidate(). The message is received
on the ui thread, and the rest is history.

I suggest adding a parameter to your simulation object''s constructor:

public Simulator(Form owner)
{
this.owner = owner;
}

If you need multiple forms to access the object, a Singleton pattern might be what your after:

public class Simulator
{
// thread-safe singleton pattern implementation
public static readonly Simulator Instance = new Simulator();

// disallow construction of the object from external classes
private Simulator() {}

public void Run(Form owner)
{
// Todo: start a new thread and run the simulation on the specified form (owner)
}

[...]

Simulator.Instance.Run(this); // this, is a Form reference

You can expose events that your form classes can listen to on the instance.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Michael Gorbach" <mg******@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com...
如果想的那么多出。问题是我的模拟对象没有访问它运行的表单,所以它不能使用invoke。另外
...我注意到一些奇怪的东西,并且像是一个解释...因为某些原因如果我在
nonui线程上运行时在表单中运行Invalidate(),则form_paint执行在ui线程中本身。
是如何发生的?为什么?
if figured that much out. the problem is my simulation object does not
have access to the form it is running in, so it cant use invoke. Also
... im noticingf something strange and id like an explanation ... for
some reason if i run Invalidate() in the form while running on the
nonui thread, the form_paint executes in the ui thread by itself. how
does this happen and why?



这篇关于在科学的模拟程序中帮助多线程和状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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