|
Thanks for your reply
I see what you mean about the AppSetttingsReader ... I had originally called it once in the constructor but changed it after some advice from a friend. We both figured that because it was a null reference it should be cleaned up ... nevertheless it is better form to only get it once - I agree.
And .. I get what you mean about the OnStop ... Thanks
|
|
|
|
|
I implemented the suggestions given and the memory still grew. In the end the only way to stop the memory growth was to take out the logging altogether! So I had a service sit there and when the timer fired go Timer.Stop then Timer.Start - doing nothing else at all.
Out of curiosity I changed the LogFile so it was no longer static and provided Open, Write & Close methods. In the constructor I grab the log file name from the config. On service start I call "Open" and on service Stop - "Close" and write to the log file at various points using Write. I don't like this idea for a multitude of reasons - but I did it just for testing purposes. I also removed any extraneous strings & DateTimes so my logFile.Write just writes the message to the file - no pretty DateTime stamp or anything.
I've also tried a couple of variations of the actual write ... one that uses File.AppendAllText, one that uses a FileStream & StreamWriter and one that uses TextWriter with StreamWriter. Making sure I close and dispose appropriately. The File.AppendAllText was what I used in my static LogFile ... it seems to be the worst offender for memory usage. I've now got TextWriter along with the Open on start and Close on Stop. That seems like a pants way of achieving what I want to do.
Now the memory consumption has slowed - but it still increases. Am I delusional in thinking that I can get this service to hover around a consistent memory usage? I left my original code (including calls to the database etc) running overnight and it got to 52Mb before I killed it. So in the hours between say 4pm yesterday afternoon and 7:00 this morning it grew from 8Mb to 50Mb. Not happy Jan
My code needs a nappy and I have no idea where to go now I'm not comfortable installing this on a server for it to run 24 hours a day until it kills the server ... not sure I'd be too popular if I did that
|
|
|
|
|
hello..
this program is based of C# and WPF
i draw Rectangle as mouse
but this program get stain of Rectangle
i want to disply without leaving any trace of Rectangle..
help me..
im sorry short english..
Screen Capture Image DownLoad
////////////////source///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
namespace rectTest
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
System.Drawing.Rectangle myRec;
System.Windows.Point firstPoint;
bool flg = false;
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
flg = true;
firstPoint = e.GetPosition(mainwin);
myRec = new System.Drawing.Rectangle((int)firstPoint.X, (int)firstPoint.Y, 0, 0);
}
private void mainwin_MouseMove(object sender, MouseEventArgs e)
{
if (flg == true)
{
System.Windows.Point currentPoint;
currentPoint = e.GetPosition(mainwin);
double width = currentPoint.X - firstPoint.X;
double height = currentPoint.Y - firstPoint.Y;
if (width > 0 && height > 0)
{
Graphics g = Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle);
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
g.DrawRectangle(myPen, myRec);
myRec.Width = (int)width;
myRec.Height = (int)height;
InvalidateVisual();
}
}
}
private void mainwin_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
flg = false;
}
}
}
modified on Tuesday, June 1, 2010 3:37 AM
|
|
|
|
|
How about erasing the old rectangle before drawing a new one?
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
System.Drawing.Pen whitePen = new System.Drawing.Pen(System.Drawing.Color.White, 5);
g.DrawRectangle(whitePen, myRec);
myRec.Width = (int)width;
myRec.Height = (int)height;
g.DrawRectangle(myPen, myRec);
InvalidateVisual();
I have no smart signature yet...
|
|
|
|
|
if exist background image, don't this mothod
this mothod is fill the white color...
are you other mothod ??....
|
|
|
|
|
Of course there are other methods.
For example you could:
1) get the image residing in the place you drawed your first rectangle
2) put the image over the first rectangle
3) get the image residing in the place you want to draw the second rectangle
4) draw the second rectangle
5) and so on
Basicly, instead of filling with white the place where was the old rectangle, you could fill it with the old image data.
Or, if you are really lazy, you could paint the background image over the whole control in order to remove the old rectangle and maintain the background.
PS: be carefull of performance issues, drawing an image at every 20 ms it is not a good way to waste cpu time.
I have no smart signature yet...
|
|
|
|
|
Why on earth are you using GDI+ to draw your rectangle? If your application uses WPF, as you state, why not just create a WPF Rectangle object, and let WPF take care of the drawing?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
how do working GDI+ . i don't know..
you see. mothod.. .
please..!!
|
|
|
|
|
You see this line?
Graphics g = Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle); That says you're using GDI+. Here's a sample rectangle implementation that uses native WPF features instead:
private System.Windows.Shapes.Rectangle GetRectangle(double left, double top)
{
System.Windows.Shapes.Rectangle myRect = new System.Windows.Shapes.Rectangle();
myRect.Stroke = System.Windows.Media.Brushes.Black;
myRect.Fill = System.Windows.Media.Brushes.SkyBlue;
myRect.Height = 0;
myRect.Width = 0;
canvas.Children.Add(myRect);
canvas.SetLeft(myRect, left);
canvas.SetTop(myRect, top);
return myRect;
}
private System.Windows.Shapes.Rectangle myRec;
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
flg = true;
firstPoint = e.GetPosition(mainwin);
myRec = GetRectangle(firstPoint.X, firstPoint.y);
}
private void mainwin_MouseMove(object sender, MouseEventArgs e)
{
if (!flg) return;
System.Windows.Point currentPoint = e.GetPosition(mainwin);
double width = currentPoint.X - firstPoint.X;
double height = currentPoint.Y - firstPoint.Y;
if (width <= 0 || height <= 0) return;
myRec.Width = width;
myRec.Height = height;
} Please note that I've just knocked this up in Notepad, so it might require a bit of tweaking.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I have custom control that I drag from the ToolBox onto a form.
Some of the custom control properties are set in the control's Properties Window after the control
has been dropped onto the form.
These control properties eg BindingSource and DataGridView depend on a BindingSource and DataGridView attached to the form. (Either before or after this custom control)
As these properties are referenced (but not available) in the custom control's OnPaint method, a Null Reference Exception is raised in design mode as soon as the control is placed on the form and tries to paint itself.
What would be a friendlier way to remind the developer (me) that these properties are required to be set?
Should I just do a Try/Catch and not paint the complete control or maybe paint a blank control with text
saying what other properties are required.
I am the only one probably using this control so it is no real big deal, just a nuisance and doesn't seem to be the thing to do!
Interested in how it would/should normally be handled?
|
|
|
|
|
I would not go for a full paint of a custom Control in design view if that entails complex things such as database accesses. So either check DesignMode, or (my preference) check the relevant objects to avoid null references. Which basically means: paint what you've got. Chances are you want the same behavior at run-time anyway. And if you've got nothing, then maybe paint an appropriate placeholder.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
I think that your control must be able to function even if it's not binded to data. Make it display something on it to inform you that there is no data binded (like a big red text "NO DATA BINDED" ). This sould be easy if you say that you do the painting.
I have no smart signature yet...
|
|
|
|
|
Sry for the wierd title. I made a simple program to dump data from a serial COM port. My question is how to wire the device to the serial female port so that the computer can recive the data. I am using a parallax Ping))) sensor that is already programmed to a basic stamp microcontroller. If i run the signal wire to the serial port pin 2, will I need to attach serial port pin 5 (signal ground) to the ground on the microcontroller in orderto recive a signal in the com port? The ping has an onboard A/D converter so the data is digital. P.S. If analog "data" is sent into the serial port raw, will it be converted by to computer or will bad things happen (i.e. nothing).
|
|
|
|
|
The subject line was quite OK. However this is not a C# question, so you're in the wrong forum.
yes, you must connect the GND pin of the PC serial port to the GND potential of your peripheral.
no, you can't feed analog signals into your PC's serial port.
yes, you can cause permanent damage when treating your serial port pins badly.
since you feel the need to ask, don't try this on your motherboard's serial port, as repairing it would be expensive; use either a plug-in serial card or a USB-to-RS232C cable instead.
PS: there are several CodeProject articles that discuss the use of serial ports.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
|
Hello,
I have a project for school and I need it finished a in a few days.
What the project does is reads data from a txt file. Each row of the txt file contains a timestamp hh:mm:ss,mmm (example: 00:05:33,141).
Whenever the system time is equal to one of the records, I need to send data to a tcp client.
What I did is created a thread with a while(true) in it and verified if a row contains the current time, than send the data.
That is not good because it's not efficient.
I would need something like a timestamp event (not using Timer).
Any suggestions/code samples? I would appreciate some help here a lot. Unfortunately I am not good with writing custom events
So for example I would need an event that will occur on a given time. Example:
event(double time,DoThis) . If time is equal to timestamp, than execute function DoThis.
In this way I would avoid a while(true) that will eat all processor.
Also, System.Threading.Seep is not an alternative because I need this to be precise.
Can someone help?
Thanks!
|
|
|
|
|
here are a few things for starters:
1.
you need to get your jobs sorted chronologically, so if your file isn't, sort it first.
2.
Windows timing has limited accuracy. You may want to read Timer surprises, and how to avoid them[^] to get the picture.
3.
if you want both good performance and good accuracy (1 second or better), you'll need something clever, probably a combination of some of these: a general timer, a multi-media timer, a stopwatch, a busy loop.
4.
if consecutive events are close together, you'll need to take extra care.
Now study, and work on your project.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Hello,
Thanks for the reply. I don't want to be rude, but it does not help to much.
1. It's sorted chronologically
2. I am already using multimedia timer, and I don't need 1000000 millisecond precission. The time is like : hh:mm:ss,mmm (first 3 digits from milliseconds).
3. This is what I don't know how to do. As I said, I tried in a while(true) loop, and it worked perfectly, but that's not efficient. That's why I was thinking of a custom event. The windows forms timer is similar to what I want, but it's not good. The windows forms timer receives an interval and you call start. Instead of this, I would need something like SetFinalTime(time,WhatToDo). When time equals the system time, than execute the function WhatToDo . That would be my idea, but I don't know how to implement it.
Believe me,I did my reading
4. They are close, yes, but I will figure that out on the way. First I need point 3 solved ... the event (or any other suggestion).
Thanks!
Vlad
|
|
|
|
|
So you have the necessary tools in your tool box. Switch on the brain cells.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
I really tried, believe me, but I have no ideas how to do it. SImply no ideas. If I had ideas, I wouldn't have posted here
|
|
|
|
|
If (as you say to Luc) the data is sorted in date time order then why not work out the number of seconds between now and the next message being due, and set up a timer for that interval. Start the timer as a single-shot, and when it fires...
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
It's true that while running through a loop constantly is not very efficient it is going to be the most accurate for you. What you really need to decide is how accurate you really need this to be.
You say that when you match up with one of the times you have you will send data to a TCP client, sending data over the network (unless the destination is actually your own machine) will have a small delay of some milliseconds to actually get sent and then it will be a little longer before it arrives at where it needs to be. That being the case you may be able to set up a standard System.Timers.Timer and use a reasonably small Tick (say 50 milliseconds or something).
All you'd need to do then is know the current time and check to see if there are any times in your list that you have gone past since the last time you checked. In this case there would be a maximum delay of 50ms since the time in your list but of course you could lower it for a little more accuracy (Keeping in mind that at the best of times the standard timer will only get called every 20ms) or indeed make the gap larger which would save some of your precious CPU time.
There's no way you're going be able to set up any kind of event that will be fired accurately down to the millisecond, even with a while loop and a performance counter it's easily possible that you may skip past a time (and therefore miss the event or fire it 'late'). So like I said you need to decide how much delay you can tolerate if the answer is 'None' then you will have to stick with your while loop because that is as accurate as it's going to get
My current favourite quote is: Punch them in the face, see what happens!
-SK Genius
|
|
|
|
|
I think I have a decent solution to your problem which would give the accuracy you want without killing the CPU:
class bleh
{
List<DateTime> times;
DateTime next;
bool quit = false;
void main()
{
while(!quit)
{
bool resting = false;
next = ...;
do
{
resting = false;
TimeSpan diff = next - DateTime.Now;
if(diff > someVal)
{
resting = true;
sleep;
}
}
while(resting);
while( true )
{
if(DateTime.Now >= next)
{
fire event;
break;
}
}
}
}
}
You'd want to be running this in a separate thread so it doesn't interfere with your main application and you'll want to replace DateTime.Now and whatnot with queries to your multimedia timer and you may also have to add some stuff for events that are very close together.
Basically if there is a large enough gap in time before the next event you should be able to afford to put the thread to sleep and when you get close to the event you will have to go into the while loop to make sure that the event is fired as close to the specified time as possible. What amount of time you use to switch between sleeping and looping is entirely up to you and I imagine will involve some experimentation.
Well, I hope that gives you some idea on what you could do to improve performance, without (hopefully) sacrificing too much accuracy.
My current favourite quote is: Punch them in the face, see what happens!
-SK Genius
|
|
|
|
|
example ebout style button
|
|
|
|
|
question
See how annoying that is?
Do you have a question?
Read the instructions at the top of the page "how to get an answer to your question".
Then edit your post to comply. Then, maybe, you will get help.
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|