关于c#并发项目的一些问题 [英] A few questions regarding a c# concurrency project

查看:74
本文介绍了关于c#并发项目的一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此我们得到了(非常)简要介绍并发性。这不是一个非常丰富的会议,但无论哪种方式,我决定承担一个小项目。



该项目是设计并行停车场模拟。



有四个入口,一辆车一次可以占用一个部分(下图中的一个部分是一个矩形)。因此,如果有另一辆车等待占用该部分,则必须等到它自由。车辆应遵守正常的单向政策。一旦车辆进入停车场,按钮就会从绿色变为红色(表示已经拍摄)。我们应该可以按红色按钮释放该车辆。



http://i.stack.imgur.com/hKydH.jpg



所以这就是我到目前为止所做的决定。



每个部分都是它自己的主题

要将汽车对象从一个线程传递到另一个线程,我们将使用缓冲区

可以使用C#面板来表示界面

A汽车可以是一个物体(具有特定的颜色和速度)

首先,使用面板是否正确?另外,我如何分配线程到这些面板并发运行?我试过在网上搜索但找不到它。 GUI中的硬编码更可取吗?这是制作矩形图形对象吗?



任何提示/阅读材料指导我完成特定任务也很棒。



谢谢

解决方案

你说section是一个矩形。一些线程很可能在部分工作,每个线程一个部分,但你不能说部分应该是一个线程。同时,无论你做什么,UI属性/方法只能从UI线程调用。



你不能从非调用与UI相关的任何内容-UI线程。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke()

使用Treeview扫描仪和MD5的问题



另请参阅有关线程的更多参考资料:

主要的.NET事件线程

如何让keydown事件在不同的线程上运行i n vb.net

在启用禁用+多线程后控制事件未触发

主线程上的.NET事件 [ ^ ]。



-SA


< blockquote>

Ridwan Sameer问:



但是汽车对象的绘图将与传递对象是分开的吗? (I.E即使我将对象从一个线程传递到另一个线程,并不意味着该对象将在适当的面板上绘制)

这是您需要学习的另一章。这取决于你的绘画是什么意思。如果你的意思是图形渲染(图形图形= / * ... * /; graphics.Draw ... ),它独立于一切。基本上,有不同的活动。 1)非UI线程:逻辑,运动场景,计算,无效(见下文),将通知推送到UI线程( Invoke ); 2)UI线程:处理通知,通过 Invoke 发送委托实例的执行;只有这部分使用了UI对象的一些属性; 3)渲染,也在UI线程中执行,但在事件的处理程序中 Control.Paint (或重写方法 Control.OnPaint )。



详情请见我过去的答案:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

如何加速我的vb.net应用程序? [ ^ ],

在面板上捕获图纸 [ ^ ],

mdi子窗体之间的绘制线 [ ^ ]。



你真的需要了解概念及其运作方式。



-SA


So we've been given a (very) brief introduction to concurrency. It wasn't a very fleshed out session, but either way I've decided to take on a small project.

The project is to design a concurrent car park simulation.

There are four entrances, and a single car can occupy one section at a time (A section is one rectangle in the below picture). So if there is another car waiting to occupy that section, it must wait till it is free. The vehicles should adhere to the normal one-way policy. Once a vehicle is in it's parking lot, the button turns from green to red (to show it is taken). We should be able to press the red button to release that vehicle.

http://i.stack.imgur.com/hKydH.jpg

So here's what I've decided so far.

Each section will be it's own thread
To pass a car object from one thread to another we will use a buffer
The interface can be designed by using C# "Panels" to represent a section
A "car" can be an object (With a specific color and speed)
So first off, Is it right to use "Panels"? Also, How would I go about assigning a thread to these panels to run concurrently? I've tried searching the web but could not find it. Would it be more advisable to hard code in the GUI? That is making rectangle Graphic objects?

Any tips/Reading material to guide me towards accomplishing specific tasks would be great as well.

Thank you

解决方案

You say that "section" is some rectangle. It's very likely that some thread can work at sections, one section per thread, but you cannot say that "section should be a thread". At the same time, no matter what you do, the UI properties/methods can only be called from the UI thread.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke(),
Problem with Treeview Scanner And MD5.

See also more references on threading:
.NET event on main thread,
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading,
.NET event on main thread[^].

—SA


Ridwan Sameer asked:


But the drawing of the car object will be separate from passing the object yes? (I.E even though I pass the object from one thread to another, does not mean the object would be drawn at the appropriate panel)

This is yet another chapter you need to learn. It depends on what do you mean by "drawing". If you mean graphics rendering (Graphics graphics = /*...*/; graphics.Draw…), it's independent from everything. Basically, there are thee different activities. 1) Non-UI thread: logic, scenario of motion, calculation, Invalidate (see below), pushing notification to the UI thread (Invoke); 2) UI-thread: handling notification, execution of delegate instance send via Invoke; only this part uses some properties of UI objects; 3) rendering, which also executed in the UI thread, but in the handler of the event Control.Paint (or overridden method Control.OnPaint).

For further detail, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

You really need to understand the concept and how it all works.

—SA


这篇关于关于c#并发项目的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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