Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
So far I have only written single core programs. Now I want to improve my performance and trying to find out, how to pull and push data parallelized. But I even don't know whether I have the right idea about MultiThreading.
Actually it is a pretty simple case. I pull data from an external interface, rework them so that they are in the right order and push them into an OpenGL Pipeline in order to draw them.
I use a WPF-application as GUI and render my data with SharpGL(OpenGL wrapped). the program runs on a dual-core processor.
Here is a sketch of my vision.
So my idea is to use a bufferArray. Now the clue: How could I write and read in the same array from different thrads?
I was recommended to inform about OpenMP. But as it turned out it is not a good idea for .Net and C#.
Thus could you recommend some fitting papers? Maybe an explanation how to use Task Parallel Library (TPL) for this case.
The correct description for this is the producer consumer pattern. In .Net you can do this using TPL Dataflow
Another implementation can be build using a BlockingCollection. A basic version:
BlockingCollection<int> bc = new BlockingCollection<int>();
async Task Main()
{
// Fire up two readers
Task.Run(() => ReadCollection1());
Task.Run(() => ReadCollection2());
// Add items to process.
bc.Add(5);
bc.Add(6);
bc.Add(7);
bc.Add(8);
bc.Add(9);
bc.CompleteAdding(); // Signal we are finished adding items (on close of application for example)
}
void ReadCollection1()
{
foreach (var item in bc.GetConsumingEnumerable())
{
$"1 processed {item}".Dump();
}
}
void ReadCollection2()
{
foreach (var item in bc.GetConsumingEnumerable())
{
$"2 processed {item}".Dump();
}
}
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm working with Anonymous Pipes to be able to communicate between 2 applications. I have 3 classes. A base class Node which holds the incoming and outgoing streams and methods like Read and Write. Deriving from this class are Client and Server. They each initialize respectively their AnonymousPipeClientStream and AnonymousPipeServerStream and have a method to sync with each other.
Having above code allows me to communicate between the 2 applications. I start the "server" application. This application starts the "client".
When both applications are started I need to send some arguments from the server to the client. The client is basically waiting for messages from the server. On the server I need to start the reading of the arguments on the client, then send the arguments and end the reading on the client so it's free to start another task. To do this I simply need to
write the start command,
write the arguments,
write the end command and
wait for the client to confirm the task is finished.
public void ServerStartClientTask()
{
Write(ReadInputs); // (1)
Write(Arg1); // (2)
Write(Arg2); // (2)
Write(ReadInputs); // (3)
while (WaitFor(ReadInputs)); // (4)
}
This is "straightforward" when you're the writer of the code (in my opinion) and is the convention how communication with the client has to happen. I wanted to make it more clear for myself and my colleagues so I came up with the following:
public void StartClientTask(Flag flag)
{
Write(flag);
}
public void EndClientTask(Flag flag)
{
Write(flag);
while (WaitFor(flag)) { }
}
public void ServerStartClientTask()
{
StartClientTask(ReadInputs); // (1)
Write(Arg1); // (2)
Write(Arg2); // (2)
EndClientTask(ReadInputs); // (3) and (4)
}
This code merely wraps code into another method to make it more readable how the communication is dome with the client.
Now for the question.
This example is not limiting to my question but just the use case I have now and to introduce my question. Is doing this wrapping of code with just other names a good or bad practice? Both examples work perfectly fine, they're just written differently. Is there a benefit to doing the 2nd approuch or would you rather just write a comment at (1), (3) and (4) in the 1st example?
In my opinion this is a very good practice and I use it all the time.
Makes the code very readable for other developers.
this way I rarely have to use comments inside my methods because the names of the methods explain what is happening.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I making an Experiment, I have a loop of 100000 increments and inside it there is a thread that does a specific task(write a log to DB), my question is when I run it its finish in one second maybe and it start to insert them lately, then how the OS handles them and it will process them all or will skip from them?
I try awaited method with them its good. but I want to know what will happen if this code was on a server and received 100000 requests.
The Code:
for (int i = 0; i < 100000; i++)
{
Task.Run(() => log.WriteToLog(i + "", new Models.CustomerModel.CustomerModel()));
}
I am not looking for alternative ways, I need to know the behaviour and how this code handles in OS (if there is a queue, some of them will run, etc..)
PS: I know its not a good approach
1 second is a bit quick. I suspect you are not logging 100000 entries properly and entries are being lost.
Assuming that code was the only code inside say a console app's main(), then because you don't await any of the tasks, it is entirely possible your process is exiting before the logging is complete.
Change:
Task.Run(() => log.WriteToLog(i + "",
new Models.CustomerModel.CustomerModel()));
...to:
await Task.Run(() => log.WriteToLog(i + "",
new Models.CustomerModel.CustomerModel()));
Also, as ckuri mentions in the comments, spawning a great deal of tasks in a tight loop probably isn't a good idea. Consider batching the logging or using IOCP.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want a Method in C# which is going to make a break for 5 Seconds between 2 lines of codes.
Thread.sleep(5000);
isn't working because the rest of the code does also make a break. Have you got any Idea how to solve this?
falling.timespeed = 0.6f;
falling.fallingl = -50f;
// here I want a break for 5 Seconds
falling.timespeed = 1f;
falling.fallingl = -200f;
Are you familiar with the asynchronous features in modern c#?
In c# 5 and above:
async Task DoSomeWork()
{
// Do the first part of the work
await Task.Delay(5000); // Asynchronously wait for a 5 second timer to expire.
// Do the second part of the work
}
Note that, depending on the threading context that you call this method, part 2 may be executed on the same thread, or a different thread.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am working on one project where I stuck on one point where I have to run two methods in parallel.
In Function 1
In my application what I am doing is I am grabbing images from the IP cam and storing that image into the one folder.
This function is used for continues streaming of camera.
For this you can refer this question which I have asked IP Camera stops streaming.
In Function 2
I will pick images from the path where my Function2 is dumping images.
Here I am doing some other operations like:
Save Image captured from the IP Camera
Detect faces in Image
Draw Face markers on Image
Some database based on result of Face Detection
Delete image File
Function 2 takes more execution time than Function 1.
So for this purpose after searching on google I get to know I can do this by multithreading.
So, I am little bit confused about this and as I am new in c# I am not that much aware of multithreading.
So, can anyone help me out on this?
You do indeed need to use multithreading, and in your case it should not be too difficult.
You'll need to add a "using System.Threading;" to the start of any files that involve threading.
public void Function1()
{
//Do camera stuff
Image image = MagicalCameraStuff();
//Create a thread that the processing will occur on
Thread process = new Thread(() => Function2(image));
//Start the thread
process.Start();
}
public void Function2(Image i)
{
//Do some processing without blocking the main thread
}
More information on threading:
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a list of sites for which log files are generated. These logs have to be robocopied, unzipped, parsed and analysed with ruby code by running respective processes.
Can anybody suggest the best way to run these processes in parallel for all the site's logs?
Considering your data model like this:
class Website
{
public List<WebSiteLog> Logs;
}
A possible parallel solution using TPL (Task Parallel Library) is something like this:
// var sites = your sites list
var processTask = Task.Foreach(sites, site =>
{
Task.Factory.StartNew(theSite=>
{
theSite.UnzipLogs()
}.ContinueWith(unzipTask=>{
{
theSite.ParseLogs();
}.ContinueWith(parseTask=>{
{
theSite.AnalyzeLogs();
}
});
Task.WaitAll(processTask);
This is a very initial solution. Lots of exception management, partitioning and even more paralellizing on UnzipLogs, ParseLogs, AnalyzeLogs are applicable.