4

Got a winform C# application.

It allows a stream of images to be displayed (like a video).

I also upload these jpegs 1 at time to my web server and a User can view these jpegs (like a video).

In the quest to make my code as efficient as possible I was wondering whether I can execute the part of my code that is responsible for uploading these images under a different priority - like real time for instance?

Is this possible?

If so..

Do I have to port my class object responsible for uploading these images into an external DLL? Then how would I 'launch' it under a different process priority?

If all this is possible, and If I accept the possible trade off of other processes on my PC which may be affected by all this will it actually give me an improved performance?

Thanks

Vishal Sharma
  • 2,773
  • 2
  • 24
  • 36
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179

3 Answers3

2

In the quest to make my code as efficient as possible

It's good that you're thinking about performance up-front. I.e., choosing the proper data structures and algorithms based on how they're used.

However, what you want to avoid is premature optimization. And thread/process priorities definitely fall under that category.

I was wondering whether I can execute the part of my code that is responsible for uploading these images under a different priority - like real time for instance?

...

Is this possible?

Yes, but you shouldn't.

The Windows OS already has what's called a dynamic priority boost. This means that I/O-bound threads already get a boost in priority.

It's best to let the OS handle priorities.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
1

Although the benefits of this style of design are questionable, one way to accomplish that would be to manually create a thread in your application to run the code you want prioritized and then to set the thread priority using the Thread.Priority property.

Another option is to run the high-priority code in a separate process and change the process-level priority. This might offer slightly better performance than relying on the thread priority but will add further complexity because now you have the overhead of doing inter-process communication with the rest of your application.

You may want to read this article on MSDN on Windows task scheduler priority system.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
1

I do not think the threads priorities will help you to solve the problem but if you want yes you can change the threads priority in C# and you have also a a lot of libraries which are helping you to control the thread workflow like

  • TPL, PLINQ
  • Tasks
  • Async, Await
  • BackgroundWorker
  • Threads etc C#.

TPL for Task Parallel Library very powerful on multi processors.

Think carefully before elevating a thread’s priority — it can lead to problems such as resource starvation for other threads.

using (Process p = Process.GetCurrentProcess())
  p.PriorityClass = ProcessPriorityClass.High;

enum ThreadPriority { Lowest, BelowNormal, Normal, AboveNormal, Highest }

I recommnded you to use encoding/decoding for the images this will help you to convert a sequence of images to video. look to this link:

Rendering a Sequence of Images in C# to make a video

Community
  • 1
  • 1
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • Hi, thanks for that link. Yes, this is the issue. If I can encode the images and decode on the other side then that would be the optimum route. That link has spawned other links that has given me a lot to look at. The most promising is Free Image. It states on that site there are 2 licenses available. The commercial 1 is for me but it does not look like you have to pay for it so why have 2? – Andrew Simpson Nov 22 '13 at 15:55
  • re: resource starvation. Yes, that was what I read about it myself. i was only prepared to accept/consider if the benefits to my particular app was significant. Then I could let the User decide (on an option btn) to elevate it. But, it is messy IMO. Just really reaching out here. Your response has given me another view to look at which is excellent. – Andrew Simpson Nov 22 '13 at 16:03
  • Whilst Free Image is good it did not give me much more compression than I have already achieved. But the concept of encoders has to be the way to go. thanks – Andrew Simpson Nov 22 '13 at 18:03