Yesterday I’ve been in parents home and I had conversation with my youngest sister. I have two of them. She was complaining about her MP3 player, she said that I have much better one and her is completely out of battery and she could not listen to music even longer than hour. Also she continued saying that it was cheap player and that is the reason why it is useless.
She always gets some money from me. I do not say that give her much money. Sometimes 20, sometimes 50, rarely 100 (those are Ukrainian hryvnias). And I do not like how she spends it, so I’ve decided to have a test with her. I asked her following: “How much money do you have now?” “60 hryvnia” - she said. Then I asked how she wants to spend them. She said that she wants roller-skates. Ok, then I followed with questions that brought to her mind and if she will not spend money this month she will get 120 next month or much more. Finally we went to the 1400 next year, but ONLY if she will not spend them on different useless things and if she will not buy rollers. She admitted that if she will stop spending money on crap she will get “rich” in her understanding .
After that I asked question: “Decide now, you either give me your 60 hryvnias and my MP3 is yours, or either you keep them for future!” She tried to avoid direct answer, but I gave her firm offer. Finally she came to me and said, that she will keep money. I praised her and with words “Keep in mind that sometimes right decisions bring your much more than you expect” I gifted her with MP3.
She was so surprised and with shining face ran to my parents saying that she gained MP3 and that is because she is smart girl. I’m sure that if we had no have that conversation before she most likely chose MP3 instead of keeping money.
I think this is simple example how you could show others that some things are good. So you basically change her mind, then you examine her and if she admits your ideas praise her (This will happen most likely, because now she thinks that this is part of her thoughts). In this way you will keep lesson in her mind for some evident time.
P/S Why have I posted this? With my future career growth I will have to be good on managing people. That is why time-to-time you will see such blog posts. But be calm, my blog is still tech blog unless I'm manager.
Sunday, June 27, 2010
Friday, June 25, 2010
Resharper: Suspend - Resume
Sometimes when you have few huge solutions opened performance of your PC is very critical and having Resharper over that is notable.
For our good luck with new versions of Resharper you could suspend it easily:
But going into menu is boring thing, so I think you would prefer to setup few hotkeys for that.
Basicly you will need to map Resharper_Suspend and/or Resharper_Resume. I bound those to "Ctrl+R, Ctrl+S" for Suspend and "Ctrl+R, Ctrl+R" for Resume.
Also, as I found over internet, JetBrains promise to have 5.1 being faster and without performance issues, but did not found any official words about this, just twits.
Personally I use Resharper Nightly Builds and enjoy them very much.
Hope this helps.
For our good luck with new versions of Resharper you could suspend it easily:
But going into menu is boring thing, so I think you would prefer to setup few hotkeys for that.
Also, as I found over internet, JetBrains promise to have 5.1 being faster and without performance issues, but did not found any official words about this, just twits.
Personally I use Resharper Nightly Builds and enjoy them very much.
Hope this helps.
Wednesday, June 9, 2010
Threading.Timer vs. Timers.Timer
I agree that title doesn't promise a lot of interesting stuff at first glance especially for experienced .net developers. But unless you encounter some issue due to incorrect usage of timers you will never think that root is in timers.
System.Threading.Timer vs. System.Windows.Forms.Timer
In few words what are differences between Threading and Forms timers just to start with something.
System.Threading.Timer executes some method on periodic bases. But what is interesting is that execution of method is performed in separate thread taken from ThreadPool. In other words it calls QueueUserWorkItem somewhere internally for your method at specified intervals.
System.Windows.Forms.Timer ensure as that execution of our method will be in the same thread where we've created timer.
What if operation takes longer than period?
Let's now think what will happen if the operation we set for execution takes longer than interval.
When I have following code:
my application behaves well - prints "a" twice a second. I took a look for number of threads in Task Manager and it stays constantly (7 threads).
Let now change following line: Thread.Sleep(500) to Thread.Sleep(8000). What will happen now? Just think before continue to read.
I'm almost completely sure that you predicted printing "a" every second after 8 seconds have passed. As you already guessed each of the "a" printings are scheduled in separate threads allocated from ThreadPool. So... amount of threads is constantly increasing... (Every 1.125 seconds :) )
Issue I've been investigating
Some mister X also figured out that Console.WriteLine("a") is critical and should run in one thread, at least because he is not sure how much does it take to execute Thread.Sleep(500). To ensure it will run in one thread he decided to have lock, like in code below:
Yes, this code ensures that section under lock is executed in one thread. And you know this code works well unless your execution takes few hours and you will be out of threads and out of memory. :) So that is an issue I've been investigating.
My first idea was System.Windows.Forms.Timer
My first idea was to change this timer to the System.Windows.Forms.Timer, and it worked well in application, but that application is able to run in GUI and WinService modes. But there are so many complains over interned to do not use Forms.Timer for non UI stuff. Also if you put Forms.Timer into your console application it will simply not work.
Why System.Timers.Timer is good toy?
System.Timers.Timer is just wrapper over System.Threading.Timer, but what is very interesting is that it provides us with more developer-friendly abilities like enabling and disabling it.
My final decision which fixes issue is to disable timer when we are diving into our operation and enable on exit. In my app timer executes every 30 seconds so this could not be a problem. Fix looks like:
And it looks that we don't need lock there, but I left it there just to be sure is case if SomeOperation will be called from dozen of other threads.
MAKE DECISION ON TIMER BASING ON THIS TABLE (from msdn article)
I hope my story is useful and when you will be searching like "C# Timer Threads issues" or "Allocation of threads when using timer" you will find my article and it will help you.
System.Threading.Timer vs. System.Windows.Forms.Timer
In few words what are differences between Threading and Forms timers just to start with something.
System.Threading.Timer executes some method on periodic bases. But what is interesting is that execution of method is performed in separate thread taken from ThreadPool. In other words it calls QueueUserWorkItem somewhere internally for your method at specified intervals.
System.Windows.Forms.Timer ensure as that execution of our method will be in the same thread where we've created timer.
What if operation takes longer than period?
Let's now think what will happen if the operation we set for execution takes longer than interval.
When I have following code:
internal class LearningThreadingTimer
{
private System.Threading.Timer timer;
public void Run()
{
timer = new Timer(SomeOperation, null, 0, 1000);
}
private void SomeOperation(object state)
{
Thread.Sleep(500);
Console.WriteLine("a");
}
}
my application behaves well - prints "a" twice a second. I took a look for number of threads in Task Manager and it stays constantly (7 threads).
Let now change following line: Thread.Sleep(500) to Thread.Sleep(8000). What will happen now? Just think before continue to read.
I'm almost completely sure that you predicted printing "a" every second after 8 seconds have passed. As you already guessed each of the "a" printings are scheduled in separate threads allocated from ThreadPool. So... amount of threads is constantly increasing... (Every 1.125 seconds :) )
Issue I've been investigating
Some mister X also figured out that Console.WriteLine("a") is critical and should run in one thread, at least because he is not sure how much does it take to execute Thread.Sleep(500). To ensure it will run in one thread he decided to have lock, like in code below:
internal class LearningThreadingTimer
{
private System.Threading.Timer timer;
private object locker = new object();
public void Run()
{
timer = new Timer(SomeOperation, null, 0, 1000);
}
private void SomeOperation(object state)
{
lock (locker)
{
Thread.Sleep(8000);
Console.WriteLine("a");
}
}
}
Yes, this code ensures that section under lock is executed in one thread. And you know this code works well unless your execution takes few hours and you will be out of threads and out of memory. :) So that is an issue I've been investigating.
My first idea was System.Windows.Forms.Timer
My first idea was to change this timer to the System.Windows.Forms.Timer, and it worked well in application, but that application is able to run in GUI and WinService modes. But there are so many complains over interned to do not use Forms.Timer for non UI stuff. Also if you put Forms.Timer into your console application it will simply not work.
Why System.Timers.Timer is good toy?
System.Timers.Timer is just wrapper over System.Threading.Timer, but what is very interesting is that it provides us with more developer-friendly abilities like enabling and disabling it.
My final decision which fixes issue is to disable timer when we are diving into our operation and enable on exit. In my app timer executes every 30 seconds so this could not be a problem. Fix looks like:
internal class LearningTimersTimer
{
private System.Timers.Timer timer;
private object locker = new object();
public void Run()
{
timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Elapsed += SomeOperation;
timer.Start();
}
public void SomeOperation(object sender, EventArgs e)
{
timer.Enabled = false;
timer.Enabled = false;
lock (locker)
{
Thread.Sleep(8000);
Console.WriteLine("a");
}
timer.Enabled = true;
timer.Enabled = true;
}
}
And it looks that we don't need lock there, but I left it there just to be sure is case if SomeOperation will be called from dozen of other threads.
MAKE DECISION ON TIMER BASING ON THIS TABLE (from msdn article)
| System.Windows.Forms | System.Timers | System.Threading | |
|---|---|---|---|
| Timer event runs on what thread? | UI thread | UI or worker thread | Worker thread |
| Instances are thread safe? | No | Yes | No |
| Familiar/intuitive object model? | Yes | Yes | No |
| Requires Windows Forms? | Yes | No | No |
| Metronome-quality beat? | No | Yes* | Yes* |
| Timer event supports state object? | No | No | Yes |
| Initial timer event can be scheduled? | No | No | Yes |
| Class supports inheritance? | Yes | Yes | No |
| * Depending on the availability of system resources (for example, worker threads | |||
I hope my story is useful and when you will be searching like "C# Timer Threads issues" or "Allocation of threads when using timer" you will find my article and it will help you.
Labels:
.NET,
C#,
Concurrency,
Performance
| Reactions: |
Subscribe to:
Posts (Atom)







