|
Hi ,
To reduce the CPU Usage in for each loop , you may use
Thread.Spleep(1) (you need to use System.Threading namespace), at the starting of you loop or at the end ,
Thread.Spleep(1) , will allow the CPU to serve the other threads.This should not impact your over all performance.
following is an example
A) Without Thread.Sleep(1) , the CPU usage is almost 70%
public Form1()
{
InitializeComponent();
while (true)
{
int i = 0;
i++;
i--;
}
}
B) If we use Thread.Sleep(1) , the CPU usage comes down to 2 %
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
while (true)
{
int i = 0;
i++;
i--;
Thread.Sleep(1);
}
}
}
Let me know if it was useful
-Regards
Bharat Jain
bharat.jain.nagpur@gmail.com
|
|
|
|
|
Bharat Jain wrote: To reduce the CPU Usage in for each loop
That doesn't really reduce CPU usage - you're actually running more code in B) than in A). It only gives lower priority threads a chance to run.
|
|
|
|
|
Agreed but using the Thread.Sleep(1) , shares the CPU with the other process , in a for each loop the CPU Usage is high because it continuously doing the same work and consuming most of the CPU , making it hard for other application to run properly , hence if share the CPU with other applications it makes system run right .
-Regards
Bharat Jain
bharat.jain.nagpur@gmail.com
|
|
|
|
|
I already tried this, but the CPU usage is same...
|
|
|
|
|
Friends,
Quite frequently i need to throw exception from the code. But i don't want to write my custom exception class. Reason is that i don't have any dynamic data to be associated. In such situation, currently i am doing something like this:
skyColor = GetSkyColor();
if(skyColor == Blue)
{
Console.WriteLine("Its a day");
}
else if(skyColor == Black)
{
Console.WriteLine("It is night");
}
else
{
msg = String.Format("{0} is not a valid sky color", skyColor.ToString() );
throw new Exception(msg);
}
As you can see above i am throwing "Exception" which is considered as bad practice. The second option is that i use InvalidOperationException, but this exception is used when object state is not valid which is not the case here. Third option is that i write my own exception class InvalidSkyColorException. The case i described above is well qualified for custom exception as i can associate dynamic data SkyColor. But issue is that in most of the cases i don't even have dynamic data to associate. I just want to throw a string message. Now if i start writing my custom exception class for each of the situation, then there will be thousands of such classes in the project.
So what you guys suggest me in such cases. Shall i continue to throw generic "Exception" or use InvalidOperationException ? Or is there better solution ?
Imtiaz
|
|
|
|
|
|
KSuthar wrote: custom exception class using ApplicationException class[
Don't derive from ApplicationException class. Derive it from Exception instead. See the remarks section here[^].
|
|
|
|
|
Imtiaz Murtaza wrote: So what you guys suggest me in such cases. Shall i continue to throw generic "Exception" or use InvalidOperationException ? Or is there better solution ?
Thumb rule, do not follow blindly: If the calling code needs to perform a special function on encountering this specific error situation, use a custom exception class. If you are simply logging the message, displaying a dialog to the user, etc, and the only thing that changes is the error message, use System.Exception.
Cheers,
Vıkram.
I've never ever worked anywhere where there has not been someone who given the choice I would not work with again. It's a job, you do your work, put up with the people you don't like, accept there are probably people there that don't like you a lot, and look forward to the weekends.
- Josh Gray.
|
|
|
|
|
Hi Imtiaz,
My suggestion would be , you can group up the types of exception in our applicaion and create a custom exception class for each group , for Example all the exception related to color can be grouped into an exception class called 'InValidColorChoiceExpection' than it does not matter if it is a sky color or any other exception related to color , you may not create a individual exception class for each exception, the point here to to properly Categorize the exceptions , in some case you may use the inbuilt .net Exception classes , for example if there is invalid Operation performed , you may not require to Create something like 'CustomInValidOpreation' , there is already an exception for it.
So in short, group you exception , try to fit it in a group that already exist , if not then only go for a new custom exception class.
I hope i am understandable
-Regards
Bharat Jain
bharat.jain.nagpur@gmail.com
|
|
|
|
|
hiii
In this Case u can use Exception()...
|
|
|
|
|
how can I join Transaction 1 and 2?
<br />
IDbTransaction oTran1 = oConn1.BeginTransaction();<br />
obj1 = new SomeObj(oConn1);<br />
obj1.DoWork();<br />
<br />
IDbTransaction oTran2 = oConn2.BeginTransaction(); <br />
obj2 = new SomeObj(oConn2);<br />
obj2.DoWork();<br />
<br />
oTran1.Commit();<br />
oTran2.Commit();<br />
I want to do something like:
<br />
oTran2.Join(oTran1);<br />
Thanks
dev
|
|
|
|
|
You want that both transactions to be in a single transaction scope? If yes, TransactionScope[^] class will help.
|
|
|
|
|
Many thanks - is this Microsoft SQL Server specific though?
dev
|
|
|
|
|
No it will work with all providers who supports transactions. I have used it with SQL server and Oracle. Using it with Oracle requires Microsoft transaction service for oracle to be installed.
|
|
|
|
|
i am new in programming, i want to know ,how is java, is it very difficult than c# & C++?what is her ranking..how much is it difficult?
, what is client side language & databases for java?
& what is her technology like asp.net for c# for web based ?
thanking you
|
|
|
|
|
|
|
waqarnaeem2@hotmail.com wrote: is it very difficult than c#
I don't think so. But I felt JAVA is more verbose than C#.
waqarnaeem2@hotmail.com wrote: what is her ranking
|
|
|
|
|
When I was first reading about C# (circa 1999) someone said, "Isn't that the new Microsoft Java?"
|
|
|
|
|
Isn't it?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Java is more like C# and C# is more like Java. They share lot of common concepts. both are pure object oriented, and if you know C# it isn't difficult to learn Java. I can recommend "Head First Java" an excellent book from o'reilly to start with Java.
Sometimes Java framework like SOAP, EJB etc would scare you but once you are good with BASIC Java, Life would be much easier with them.
As far as you are concerned about the databases you can use any kind of database with Sun's ODBC drivers (just google to find them). And if you are looking to make a web application, You will need to know JSP (Java server pages, 99% like Java) and Servlets. JSP is client side and Servlet is server side scripting language.
I was a Java programmer, and I learnt C# with easy. Code project was always the best friend.
Happy learning,
Karmendra
|
|
|
|
|
Hello all,
how to configure trace listener to output trace statements to command window?
Thanks.
|
|
|
|
|
You have a custom listener in console application? If that's the case, just use Console.WriteLine.
|
|
|
|
|
its a windows application project where i want to configure tracelistener to output trace statements to command window. I have already sprinkled trace.writeline statement all over project and configured TextTraceListener, which outputs trace statements to text file.
|
|
|
|
|
As far as I know there's no 'native' support for that in framework, so I guess you should
- create a console application
- start a new process for that application
- use IPC to receive data from you trace listener
- write it to console
|
|
|
|