问题
I'm making a desktop application in C# which contains a moving News Bar labels. I'm using a timer to move these labels but the problem is that when i make the interval of this timer low (1-10 for example) the application takes very high percentage of CPU Usage, And when i make it higher(200 -500 ) the movement of the labels becomes intermittent or not smooth movement even that the user may not be able to read the news in Comfortable way.
((More Information)) it is Windows form application. the way i move the labels is as follows : the news items from RSS feeds are represented in a group of linklabels. All these linklabels are added to a flowlayout container. The timer moves the whole flowlayout container. I found this way according to my knowledge the best way to making the news bar. If you have better idea or solution please help
回答1:
What does the timer interval represent? If it's milliseconds, then you can divide the number of updates per second that you want into one thousand and get the timer rate.
You could also use Sleep(100) or so, but it may just be that you're trying to do too much in your update. Maybe you can do "important changes" much less frequently, like only every 100 updates or so, or put those on their own timer, and do as little as possible to update the scrolling much more frequently.
It's not surprising that your application takes a lot of CPU when you have it set to do its update 100 or 1000 times per second. :)
回答2:
I suspect the problem is that you're using the timer to move the ticker as well as populate the data?
If you want to use a timer to scroll the view, that should be fine. Your code needs to be extremely light (just update the vertical or horizontal positions and return). A better approach, however, would be to use something like a "game loop" to achieve whatever update frequency you're after (within each iteration, time how long it takes to move the view, then Sleep for the number of milliseconds remaining to hit your target frequency.)
Update the data from a separate timer/thread.
回答3:
Look : Drag One Label and One Timer Set Timer Interval = 100 then :
private void timer1_Tick(object sender, EventArgs e)
{
label1.Location = new Point(label1.Location.X + 5, label1.Location.Y);
if (label1.Location.X > this.Width)
{
label1.Location = new Point(0 - label1.Width, label1.Location.Y);
label1.Text = "Your Message Here ";
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
回答4:
You can create a custom marquee label which uses a timer and redraw itself and animates the text from left to right or top to down, like this horizontal marquee label or this vertical marquee label.
But since you have asked about something without a timer, a good option is using a WebBrowser
control showing a marquee tag, easy and flexible.
You can set any content for that, setup behavior, direction, speed, appearance, width, height and fully customize it.
Example
this.webBrowser1.DocumentText = @"
<marquee>
<span style='color:#f00;'>Breaking news: </span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</marquee>
";
来源:https://stackoverflow.com/questions/2933155/how-to-make-moving-news-bar-in-windows-forms-application-without-timer