creating multiple threads of a function [closed] - c#

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

Related

Pull and Push Data in .Net, in parallel [closed]

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();
}
}

Get current frame number / time of video in unity [closed]

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
I'm facing a problem, please help me if it is possible.
I'm practicing unity and I'm trying to make sth for myself. And I'm playing a video using UI and canvas raw image and what I need is this:
I want to tell animator to start a special animation when the movie reaches a specific frame or time for example when it reaches at 10 seconds I want to make the movie stop and that animation be triggered.
I don't have any problem with playing or pausing my video or triggering animations in animators...
My problem is that I don't know how to get the number of current frame playing in movie using script
I tried using time functions to solve my problem but it has two disadvantages , first its not accurate !! each time it results in a small different out come and I don know exactly why , second it becomes hard if I change something else for example actions before that movie
In short my question is this: is there any way to get the current frame number of a movie using script in unity ? ( a movie which is playing using UI and canvas and raw image )
Thanks a lot in advance...
Is there any way to get the current frame number of a movie using
script in unity ?
Yes. You can obtain the current video frame with the VideoPlayer.frame variable.
Note that it works like an array index. 0 is the first frame and 1 is the second frame.
You seem to confuse video frame with video time in the title of this question. They are two different stuff.
You can obtain the video time with VideoPlayer.time. You just check when it is 10 seconds in the Update function then do something.
VideoPlayer vidPlayer = null;
void Update()
{
if (vidPlayer.time == 10)
{
///
}
}
See here for how to play video with the VideoPlayer API.

how to make real time code in C# [closed]

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 want to read sms from my GSM modem.
I wrote C# code.
This code run when I click start button.
I want to my program read sms when received, not click button.
thanks.
Your program is keyed to activate when you press a button, some method is called. You need to call this method when SMS data is received instead. This could be done using threads (SMS thread and main thread showing data) although it could just as easily be done using a cycle. In pseudo code:
while (don't quit) {
display page;
check for sms data;
sleep for small time to allow other OS programs to run also;
}
This is a "tight loop" and can use excessive amounts of CPU time depending on the code of the actual steps. For a tight program loop one simple method is to apply some sort of sleep method.
There are other ways to do the same thing, visitor pattern could probably be used, threads, etc...
It seems that you are only lacking the cycle. Your programs is probably more like:
while (don't quit) {
display page.
wait for button press.
}
although that flow wouldn't be obviously apparent at first glance without studying your program flow.
If you are using triggers (the button press is probably a trigger) you can trigger on a timer that fires as often as you want (100ms, 1 second, whatever) that checks for SMS data when fired, if there is SMS data it updates the form.
Many, many ways to do this. A quick Google for "program flow" doesn't find any useful links at first glance that would explain the many ways you could do this. Perhaps looking at other's code would help. I've often searched open source repositories for code I could look at to see how someone else did something.

From multiprocess to multithreading [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I recently joined a team responsible for the enhancement of an existing 5-year old program.
It was developed with multithreading in mind, but not correctly implemented.
The solution is composed of multiple Windows Forms programs (let's call them Screen 1 and Screen 2).
Screen 1 can launch Screen 2 multiple times with differents parameters, but launches them as Process, not as Threads, Tasks, or by using BackgroundWorker.
Here is how it's done :
public void RunProcess(Arguments Arguments,string ExcutableName)
{
Process Prc = new Process();
Prc.StartInfo.FileName = ExcutableName;
Prc.StartInfo.Arguments = Arguments.GetProcessArguments();
Prc.Start();
_ListProcess.Add(Prc);
}
The processes are kept in memory and killed when Screen 1 closes.
I would like to refactor this in a cleaner way, going for a "single process" approach. What would you advise?
It looks like you need to port Screen 2's logic into the Screen 1 project, then replace the "Process" logic with threaded code.

How to sync system clock with global date/time? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My system clock is going crazy randomly at any moment and changing the system clock's date/time to a random one. It's not the lithium battery nor a virus because I checked. Also it's not something from the Windows.System.Time itself.
I want to create a process that will, on an interval, check to see if the system clock's date/time matches the global date/time and if not, it would sync.
I need this to run in the background. I am not even sure if a Windows process is correct way to accomplish this. I am open to any other solutions as well.
Create a new c# empty project. Click on the project and go to Properties change the output type to Windows Application (This will remove the console).
Create a new class example: Example.cs
Write the static entry point eg:
public class Example
{
static void Main(string[] args)
{
}
}
Insert your code in the Main routine.
This will create a process that contains no console/window/service.
I'm guessing this is what you want.

Categories