Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
Posted
Comments
Sergey Alexandrovich Kryukov 9-Aug-15 10:40am    
There is no such thing as "run a panel". And what is "Panel"? Do you think there is only one type named "Panel"? It all depends on what you are using, but typically .NET UI library requires all changes to be done on one thread, but the UI can be multithreading.
The question does not seem to make any sense.
—SA
Ridwan Sameer 9-Aug-15 16:22pm    
Hi Sergey,

Sorry If my question seemed vague.

By Panel, I meant this (available in the Visual Studio 2013 IDE Toolbox) https://msdn.microsoft.com/en-us/library/system.windows.forms.panel(v=vs.110).aspx

I was just looking for some guidance about how to go about the project. Maybe panels aren't the way to go, what do you recommend?

Thanks for the Reply
Sergey Alexandrovich Kryukov 9-Aug-15 21:32pm    
Thank you. Specifying that you meant System.Windows.Forms.Panel would suffice.
Now, it remains unclear what do you mean by "running a panel".
—SA
Ridwan Sameer 10-Aug-15 2:17am    
Hey,

I actually was talking about the Threads "running" (so to speak).
I consulted a senior person on the matter and they informed me that each "Section" should be a thread. And only one car can be on one thread/section at a time. I'm just looking towards guidance on how to implement that. Or maybe that's not the best way to do it? (Given this is a concurrency experiment, so I need to make use of concurrency quite a lot).

I was wondering how you could associate a Forms.Panel to a thread, Or something along those lines as well.

Again, Thanks for taking time to help me out.

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
 
Share this answer
 
Comments
Ridwan Sameer 10-Aug-15 7:27am    
Hey Sergey,

Thanks for the reply. I gave those links a read, I can't see how they translate into my project exactly though.

I consulted with my lecturer, and he explained briefly.

We can draw a rectangle as a car (lets say a car enters from the top left entrance into the first section) using a Graphics.FillRectangle or similar. After this, it checks whether the next section is free (that is, does not contain a car), If so, we then draw the same rectangle on the next section (The animation does not have to be fluid).

And basically that has to run concurrently (Handle many cars moving at the same time). And cars reach their parking spot etc. I'm allowed to have a control panel and assign cars a specific parking spot before they are created.

Basically I'm looking for a way to implement this logic.

Thanks for your answers so far, The "How to get a keydown even to operate on a different thread" was a very interesting read, Thanks a lot.
Sergey Alexandrovich Kryukov 10-Aug-15 10:08am    
Did you understand the main thing: whatever runs concurrently, all the UI updates are serialized in one thread. Yes, what I described is exactly the way to show cars moving in parallel. I cannot see what you did not understand. Say, you can control each car by a separate thread. Good. So you can animate the motion of those cars in UI.
—SA
Ridwan Sameer 10-Aug-15 10:16am    
I guess I don't exactly understand what you're referring to by UI?

What would I need to include as the thread body/method?

So far my design without any code implementation is like this, each section is a Forms.Panel

http://imgur.com/NIBtx0o

I guess if you could try and be a little more detailed into what approach I should take? Given I'm learning on my own and very new to it. Trying my best.

Thank you!

P.S I'm extremely sorry for eating away your time like this, and being a nuisance.
Sergey Alexandrovich Kryukov 10-Aug-15 10:33am    
Each thread calculates, say, car position. Then you need to update your position presented in UI. Your car-related thread runs in a cycle. In each cycle it takes current time and calculates current object states according to time, speed, previous position etc. That requires UI to update the image of the control representing the car. You do it not directly, but through Invoke. That's all.
—SA
Ridwan Sameer 10-Aug-15 10:43am    
I understand that, To move the car.

However, how does passing of the "car" from one thread to another work? And also checking whether a car already exists in the next thread/section and therefore keeping the current car object to wait till the next section is free
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
 
Share this answer
 
v2
Comments
Ridwan Sameer 11-Aug-15 11:00am    
Thanks a lot Sergey, You've been a tremendous help.

I've developed an idea of how to go ahead with it, and will hopefully have a prototype soon.

Much appreciated
Sergey Alexandrovich Kryukov 11-Aug-15 11:41am    
My pleasure.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900