Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have
C#
t.Enabled = true;
            while (t.Enabled == true)
            {
                scroll(telep.Handle, (speedslider.Value * 2));
            }

where t is my timer. scroll(telep.Handle, (speedslider.Value * 2)); is short for a very very long and boring function, that pretty much scrolls the scrollbar by the value of a track bar multiplied by two (2).
Using while (t.Enabled == true) is not very good because if I try to interact with the form while it is scrolling, the form becomes unresponsive for a sec, then is scrolled all the way to the bottom.

I was thinking of using a timer for scrolling 2 pixels every, lets say for e.g., 30 milliseconds, but I do not know much about timers.

If someone could throw me into the correct direction, or even completely tell me how to do this, that would be great.

thanks to all who answer.:thumbsup:
Posted
Updated 20-Feb-11 22:52pm
v2
Comments
Albin Abel 21-Feb-11 3:43am    
why don't you delegate the scroll function to a separate thread? Handle the timer's tick event for executing at intervels
vlad781 21-Feb-11 4:46am    
here is how the scroll works, i made an article about it:
http://www.codeproject.com/KB/cs/Automatic_Scroll_Effect.aspx
Dalek Dave 21-Feb-11 4:52am    
Edited for Readability and Grammar.
vlad781 21-Feb-11 4:58am    
um, thanks?

Hi You can use very well Dylan's solution. That should work

Alternatively (However not recommended for a UI thred)......
Use the Threading timer as shown in the example below...it won't make your form unresponsive, because the timer will be in a different thread.

C#
public partial class Form1 : Form
 {
     private static int Count = 0;
     public Form1()
     {
         InitializeComponent();
     }

     private void Form1_Load(object sender, EventArgs e)
     {
         System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(MyIntervalFunction));
         timer.Change(0, 2000);
     }
     private void MyIntervalFunction(object obj){
         Count++;
         if (label1.InvokeRequired == true)
         {
             this.Invoke((MethodInvoker)delegate
             {
                 label1.Text = Count.ToString();
             });
         }
         else
         {
             label1.Text = Count.ToString();
         }
     }
 }


The count will be added to the label, still your form will be responsive.This is an example and you can use the MyIntervalFunction for your requirement
 
Share this answer
 
v2
Comments
Dalek Dave 21-Feb-11 4:52am    
Good answer.
vlad781 21-Feb-11 4:56am    
hmmm, this seems legit. but i do not understand it, because well, i just dont know much about timers. do you think you can explain in further detail?
Albin Abel 21-Feb-11 5:49am    
System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(MyIntervalFunction));
the above call instantiate the timer. It is not the same as timer control...where I am pass the a delegate reference of a function. This function will be executed in a different thread than the main UI thread. SO the UI thread become still responsive.

timer.Change(0, 2000); // this start the timer execute the MyIntervalFunction at 2 sec interval, because i given 2000 milliseconds as argument in the Change method call.

if (label1.InvokeRequired == true)
{
this.Invoke((MethodInvoker)delegate
{
label1.Text = Count.ToString();
});
}
else
{
label1.Text = Count.ToString();
}

This is the standard way of handling controls when working with multiple thread. The controls can't be updated cross thread. So you have to check is invoke required. If so use a method invoker to invoke the control operations.

Replace the label with your scroll function. If any control is accessed in the scroll then that has to be handled through method invoker

Is it clear?

here is the documentation for method invoker

http://msdn.microsoft.com/en-us/library/system.windows.forms.methodinvoker.aspx

here is the documentation of timer

http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
vlad781 21-Feb-11 11:32am    
ok, thanks a lot. so i would replace the part with the label with my
scroll(telep.handle, (scrollspeed.Value * 2));
method?
vlad781 21-Feb-11 11:34am    
ok, isee now.
Set the timers interval property, then create an event handler for the timers tick function.

Done!

C#
private Timer timer = new Timer();

public void Init()
{
    timer.Interval = 5000; // every 5 seconds
    timer.Tick += new EventHandler(timer_Tick);
    timer.Enabled = true;
}
void timer_Tick(object sender, EventArgs e)
{
    Console.WriteLine("This event will happen every 5 seconds");
}
 
Share this answer
 
v4
Comments
vlad781 21-Feb-11 4:30am    
Im sorry, but this simply doesn't work. First, you didn't even create the timer correctly. try:
private Timer timer = new Timer();
you forgot^ but you did name it...
and even after fixing that, i get 6 errors as opposed to the initial 9. please try the code before posting it again.
Dylan Morley 21-Feb-11 4:38am    
Was merely an example of how to control Interval + Tick (which are the important things here), not working production code!!

There you go though, fixed.
Dalek Dave 21-Feb-11 4:52am    
Good Call.
vlad781 21-Feb-11 4:53am    
i have so far:

private Timer t = new Timer();

public Form1()
{
InitializeComponent();

t.Interval = 30;
t.Tick += new EventHandler(t_Tick);
t.Enabled = true;
}
void t_Tick(object sender, EventArgs e)
{
Console.WriteLine("This event will happen every .3 seconds");
}
which is good... but now i need to know how to use it to be able to use my scroll function everytime there is a 'tick'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900