Click to See Complete Forum and Search --> : Woah, what's with all the threads?


Red Squirrel
May 26th, 2009, 09:20 PM
I have a 2 threaded C# app that is acting weird on one machine was works ok on the test machine. The only difference I can think of is the machine it's acting up on is a bit slower, and is single core.

So I decided to check in process explorer to see which thread is slowing down, yet it shows like 13 threads! What are all these?

The one that is killing the cpu is called mscorwks.dll!CreateApplicationContext+0xc461. What is this?

These threads exist on the other machine but hardly take any cpu, if any. What are things I should look for?

Also the machine that is acting strange is running win2k3 while the machine that is ok is winxp. So lot of variables...

BigEd781
May 26th, 2009, 10:17 PM
I think that you don't quite understand how threads work. On a single core machine, you can spin off all the threads you want and it is not going to help performance. You only have a single CPU, so it can only do *one* thing at a time. It is not just slower, there is only one input/output pipeline for your application to use. In that case, context switching will actually hurt performance (not noticeably however, unless ou spawn a ton of threads).

As far as the mscorwks problem, I think that this article (http://www.dotnet247.com/247reference/msgs/41/207197.aspx) may help.

Red Squirrel
May 26th, 2009, 10:39 PM
I think that you don't quite understand how threads work. On a single core machine, you can spin off all the threads you want and it is not going to help performance. You only have a single CPU, so it can only do *one* thing at a time. It is not just slower, there is only one input/output pipeline for your application to use. In that case, context switching will actually hurt performance (not noticeably however, unless ou spawn a ton of threads).

As far as the mscorwks problem, I think that this article (http://www.dotnet247.com/247reference/msgs/41/207197.aspx) may help.

I know this, but I can't see how my app on a dual core machine uses 1-5% cpu average and on a single core it uses 100%.

I'm also not sure why there are WAY more threads then I actually created. There should only be two threads.

BigEd781
May 27th, 2009, 03:05 AM
You don't create all of the threads that your app uses. Things are happening behind the scenes. The 100% CPU usage is due to an issue in mscorworks.dll. Did you read through the article I linked to?

Mutant_Fruit
May 27th, 2009, 04:15 AM
On a single core machine, you can spin off all the threads you want and it is not going to help performance

There are many reasons to thread, only one of then is for raw performance. The most important reason in a GUI application is to keep the application responsive. You simply can't do long-lasting processing inside the mainloop.

If you google for "mscorwks.dll!CreateApplicationContext 100% cpu" you get a tonne of links. Do any of them offer any useful advice for your situation?

BigEd781
May 27th, 2009, 12:52 PM
There are many reasons to thread, only one of then is for raw performance. The most important reason in a GUI application is to keep the application responsive. You simply can't do long-lasting processing inside the mainloop.


Yes, that's true, but that is really perceived performance, your tasks will not be done any faster. Of course, still a good reason to do it.

foamy
May 27th, 2009, 01:34 PM
On a single core machine, you can spin off all the threads you want and it is not going to help performance. You only have a single CPU, so it can only do *one* thing at a time.
What about a single-core CPU that supports MultiThreading such as Intel's old P4 series? I would imagine they support execution of multiple threads in a semi-simultaneous way :)

BigEd781
May 27th, 2009, 01:54 PM
I would imagine they support execution of multiple threads in a semi-simultaneous way :)

I think that is the key :). Honestly, I am no expert on CPU architectures or multi-threading, so take my advice on these subjects with a grain of salt. In the general case, threading on a single core CPU is great for keeping the UI 'alive" as Mutant_Fruit suggested.

Red Squirrel
May 27th, 2009, 06:19 PM
I googled mscorwks.dll!CreateApplicationContext but it seems everyone with issues never had their thread answered.

I also do have a dependency (the mysql library) so I have a feeling that's part of the problem, but I hope there's a fix. Guess worse comes to worse I can run the production server on winXP... Don't really want to do that though.

Arjay
May 27th, 2009, 08:25 PM
I googled mscorwks.dll!CreateApplicationContext but it seems everyone with issues never had their thread answered.

I also do have a dependency (the mysql library) so I have a feeling that's part of the problem, but I hope there's a fix. Guess worse comes to worse I can run the production server on winXP... Don't really want to do that though.If I recall correctly I believe I answered a post where I recommended that you make sure you close the db connections when you were finished with them. I don't remember if that was you for sure, but never got a reply. At any rate, make sure that you are closing the connections (as the sql provider can allocate/deallocate threads so help it do it's job by cleaning up those sql connections).

Red Squirrel
May 27th, 2009, 09:15 PM
If I recall correctly I believe I answered a post where I recommended that you make sure you close the db connections when you were finished with them. I don't remember if that was you for sure, but never got a reply. At any rate, make sure that you are closing the connections (as the sql provider can allocate/deallocate threads so help it do it's job by cleaning up those sql connections).

I'm pretty sure I am closing them but I can double check. Either way I don't think it's that, as even after a reboot/reinstall/upgrade (added SP1) no change.

I installed .net 3.0 but there is no csc in the directory so I was unable to compile it under 3.0. Is there a way to get the 3.0 version of csc (c# compiler). I want to try to compile under 3.0 and see if that helps.

Arjay
May 27th, 2009, 11:56 PM
You can set the build target to build for 2.0, 3.0 or 3.5 (if you have 2008).

Red Squirrel
May 28th, 2009, 05:39 PM
Hmm yeah guess it would help if I actually installed the latest VS. I only use the command line tools as it's easier to script, but I still need the program to get the latest tools. :P I'll try that.

Red Squirrel
June 4th, 2009, 10:34 PM
Ok guess this app is designed for 2.0. I Tried to compile it under 3.5 but it still runs under 2.0 regardless. So that test failed.

What about this mscorwks.dll!CreateApplicationContext thread, anything I can do about this? Seems like a huge issue, lot of people seem to have problems but none gotten answers. Did lot of searching on google. (even found this thread lol)

I may just change the prod server to xp but I want to try to actually fix the problem instead.

dglienna
June 5th, 2009, 12:27 AM
The compiler compiles it for multi-processor, but it runs on single-core (or even Hyper-Threading, which doesn't really work the way it sounds)

Waiting for 4 interrupts on a single-core system will cause bottle-knecks.

KIM- If you run Virtual PC 2007 on a Quad-Core machine, when the VHD is running, it uses only ONE CORE.

Anyone that has VS10 from the VHD download cannot use any of the Parallel Examples, as it only uses a single core.

Red Squirrel
June 5th, 2009, 11:04 AM
How would I tell it to compile for only one cpu? Or even better, is there a way I can make it auto detect and compile accordingly? My dev machine has 2 cores while the test machines have 1 and the production has 4. I always recompile the app when I transfer the changes to different environments.

Mutant_Fruit
June 5th, 2009, 11:12 AM
Single core or multi-core has absolutely nothing to do with it.

Red Squirrel
June 5th, 2009, 02:22 PM
I don't know, I'm starting to wonder. I will try to set the afinity to 1 on my dev box to see if it does the same thing. Though that may not be the same result as 1 cpu.

Red Squirrel
June 5th, 2009, 11:07 PM
Ok I decided to test on another win2k3 VM and the app works fine! This VM was actually cloned from the one that is not working ok, so I know as a fact it's a similar install. The VM giving trouble is a slower machine, is C# maybe just very sensitive to this? The machine is an AMD 2700+ I don't get how the performance would be THAT bad though, like really. Anyway I can optimize this? In fact, how about a way to just programaticly terminate all those extra threads? I don't need those, I don't even know where they come from.

ricky_cpp
June 8th, 2009, 09:34 AM
Hello Red Squirrel,

About a normal application, I agree with Mutant Fruit.

- Usually there is no linear relationship between CPU capacity and performance. CPU with double frequency/core might speed up a process by 50 times or more and might not speed up a process by double even. Not correct forum to explain.
- Dual, quad core have been introduced recently. I do not know what kind of application you are working on, but if it is a normal application not using some high speed i/o card for GB of data transfer per second or using pico seconds timer to make million calculations or editing heavy GigaBytes 3D images or working as server to which many clients are connected etc etc. I think you should work more on your application rather than worrying about CPUs.

Example:
- My dev PC is dual core with 4 GB RAM and test machine is 1.4 GHz, 512 MB RAM
- My application consumes 18% dual and 100% single core
- Now, I am working on it. The problem is a timer interrupt, which is occuring every 30 ms, refreshing 2 images on screen, another thread is busy to receive data continuously from serial port which updates these images.
- This means, that I have to make improvements in the application and to some extent make it CPU frequency independent (as far as possible, this does not include 10 yrs old processor of course :-) ) as company can not sell this normal application with a note "to be used with QuadCore only"

If your application is normal one and nothing too new you feel about it and is affected heavily by single/dual core, then start working on it using test PC. You will find reason for such high consumption soon.

Red Squirrel
June 11th, 2009, 06:05 PM
Hmm so can I expect that it's just a general cpu thing then? the system is just too low end to handle the app properly? I noticed that if I give it time it DOES run and do what it's suppose to do, it's just so slow compared to a slightly better system.

Mutant_Fruit
June 11th, 2009, 07:14 PM
What you're experiencing appears to be a bug which has unknown causes. It's possible (though unlikely) that it's an issue that only appears on some processors. Or maybe one of your machines has a different version of a particular piece of software and so isn't affected. The internet is quite vague on how to fix this issue, though a lot of people appear to be affected.

One thing you could do, if you haven't done it already, is run your application through a profiler and see if you notice anything strange on the machine that gives 100% CPU usage. Maybe some software you're interoping with is going into an infinite loop due to a slight difference in behaviour.

dglienna
June 11th, 2009, 07:39 PM
Poorer graphics card, or not enough memory would come to mind...

laser_lu
September 12th, 2009, 07:50 AM
I've encountered the same problem.
The number of threads keep increasing and large CPU usage will be taken after hundreds or thousands of threads were created.
Most of the threads have start address of 'mscorwks.dll!CreateApplicationContext+0xc461'.