Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've spent the last few hours trying to debug my application, it seems like its using much more CPU than it should. I believe this is a problem with my lack of understanding on how to properly use threads in C# .NET. Just starting the application and sitting at the main window (with nothing really going on) the CPU usage idles between 2-6%. If anyone could review my program (its small) I would greatly appreciate it, also if you spot any other problems or have any advice on how to improve certain operations I would be grateful.

I create two threads, one in MainWindow.xaml.cs MainWindow() and one in App.xaml.cs Init() and dispatch jobs to them throughout the application. The application thread is a background thread, and the UI thread is a foreground thread

source code: https://bitbucket.org/looter/rcon-project/src

share|improve this question
3  
Could you please post the relevant code in the question, not just link to it (as stated in the faq)? – Glenn Rogers Oct 4 '12 at 1:49
Updated my post with the thread locations. – looter Oct 4 '12 at 7:17
1  
@looter if you post the relevant code within the question, it may be reopened. – codesparkle Oct 4 '12 at 16:35

closed as off topic by codesparkle, Glenn Rogers, palacsint, Brian Reichle, svick Oct 4 '12 at 9:59

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

If you are using WPF, I assume you are using .net4 (or later) or at least you would be able to upgrade.

In this case you should really have a look into the Task-Library thats integrated in the .net-framework. It's using a threadpool and is using as much threads as needed / are usefull.

I used it with a good feeling and it's really easy.

Example

Task locationLoadTask = Task.Factory.StartNew(() =>
{
    // Here the parallel part starts
    IDaLocationServices da = new DaLocationServices();
    var locationDtos = da.Load();
    DoSomethingWithTheDtos(locationDtos);
});

You can also do things now like

locationLoadTask.Wait(); // To wait until it's finished

IMO it's very handy and easy to use. Just follow the MSDN and this good tutorial from codeproject.

share|improve this answer

General code review:

Don't do 'God classes' that can handle everything. It's really unreadable.

Try apply MVVM architecture (separate your UI from logic): http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Also avoid using static properties that hold changeable state (not testable, hard to follow).

share|improve this answer
Thanks for the advice, I'll look into MVVM, but I dont quite understand the part about static properties that hold changeable states? should I make those non-static or something more specific? – looter Oct 4 '12 at 7:16
It depends what you need. This could be non static property, method argument or class constructor argument (this is related to dependency injection topic). The problem with changeable static properties is that they are not testable. Second problem is that it is hard to see which objects depend on them. – Kuba Oct 4 '12 at 7:45

How are you measuring CPU usage? If you are using the task manager in windows, then you should keep in mind that it is not the most accurate way to profile an application's processor consumption.

Also, please post the code that is specifically related to threading.

share|improve this answer
I create two threads, one in MainWindow.xaml.cs bitbucket.org/looter/rcon-project/src/4ee2c5911197/rcon/… ( MainWindow() ) and one in App.xaml.cs bitbucket.org/looter/rcon-project/src/4ee2c5911197/rcon/… ( Init() ) and dispatch jobs to them throughout the application. The application thread is a background thread, and the UI thread is a foreground thread. – looter Oct 4 '12 at 1:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.