Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Dear Sir/Madam,

I am new in C# Programming.
I make a code for communication with my embedded system using TCP /IP protocol.
there is multiple machine with different IP address are connected with one PC.
I want to communicate with all machine concurrently because all machine data are important.
(all machine are equal and at same priority).
I refer many documents for multi-threading
But that are for process based with different time
Is there any way in multi-threading by which i can make multiple thread can work as per given time-slice(same time)?
i.e. suppose there is 3 thread with same priority and given time-slice is 10 msec then,
First thread 1 start,
after 10 msec thread 1 store its current data and stop it and thread 2 start,
after 10 msec thread 2 store its current data and stop it and thread 3 start
after 10 msec thread 3 store its current data and stop it and thread 1 start
This process continue working for infinite time
I already done this type of multi-threading in embedded system.
I want to do same in c# in visual studio.
Please give me proper guidance for it with example.

From,
Vishal Akbari

What I have tried:

Currently I communicate with only one machine( only one IP or one thread )
Posted
Updated 13-Jan-17 12:25pm
v3
Comments
Jon McKee 13-Jan-17 1:09am    
You are describing the way in which many operating systems already schedule tasks onto the CPU. Given a specific platform such as Windows, you can use SetPriorityClass and SetThreadPriority to ensure a priority for your code's tasks though that is discouraged unless you have dedicated systems. Other than that, you can not guarantee your code will run every X milliseconds due to kernel-level scheduling. If that amount of scheduling is necessary, you need to create your own quasi-OS that runs on top of the hardware. That is an extremely complex topic that is far beyond what a QA answer will give.
anup.bhunia 13-Jan-17 6:44am    
Just a idea to overcome the situation...
Create three threads, then using a timer even suspend() and resume() the thread in round robin. though;
From .NET 2.0, Suspend and Resume have been deprecated, their use discouraged because of the danger inherent in arbitrarily suspending another thread. If a thread holding a lock on a critical resource is suspended, the whole application (or computer) can deadlock. This is far more dangerous than calling Abort — which results in any such locks being released (at least theoretically) by virtue of code in finally blocks.

Multi-threading does not and cannot work like that. There are two basic, widely-used approaches when it comes to multi-threading; fire-and-forget or thread pool. Fire-and-forget is for when you you have long-running, stand-alone tasks that will interact with other parts of your program, such as the UI, while maintaining program responsiveness. A thread pool is best utilized when there will be a lot of tasks, in any order and/or repeatable, that will take longer than a few milliseconds such as successive database access or network communications. It maintains a pool of threads that can be triggered to run code but doesn't give the thread resources back to OS when the thread returns.

Once a thread begins to run down a line of program execution, it cannot be stopped unless you manually code in stop points. This can be done but it is not recommended as it causes a myriad of synchronization problems. The ability to start and stop threads mid-execution is a System Kernal level control.

A thread pool is closest to what you want to achieve but you cannot arbitrarily stop them.

MSDN ThreadPool class[^]
 
Share this answer
 
This has nothing to do with C#.

You can't do that in Windows. Windows does not give you the ability to specify how much time every thread gets to run before giving up control. That is managed solely at the discretion of Windows. You can influence it a bit with thread Priority, but that's about it.

Also, you're not accounting for the thousands of other threads that are running in the system that have nothing to do with your process. Windows executing those threads can and will put ALL of your processes threads on hold while those run. Basically, your app is doing nothing at all while other threads in the system are executing.

You simply don't have that kind of granularity under Windows.
 
Share this answer
 
Comments
Jon McKee 14-Jan-17 4:27am    
I'd like to point out that you don't have that level of granularity with ANY modern OS afaik. The best you can hope for in Windows is abusing the Real-Time/Time-Critical levels of process/thread priority. Even then you can not guarantee the scheduling.

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