Click here to Skip to main content
15,914,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 Images (Image1, Image2, Image3),

Using a timer control with interval value of 5000, i.e 5second, I
want to accomplish the following:

- Initially, only Image1 is visible to the user middle of the form, i.e the 2 images
are set to be invisible.
- When load the form appear and image1
appears exact middle of the form
- After 5 seconds, the image1 disappear and image 2 appears right side.
- After 5 seconds, the image2 disappear and image3 appears left side
- After 5 seconds, the image3 disappear and the image1 appear

I am setting timer control's Enable property to "True" in Design
time.
I am setting timer control's Interval property value to 5000 in
Design Time.

I assume that I do all the coding but not working only displaying two images


Can anyone help me code what I want to accomplish?
Posted
Comments
Shahin Khorshidnia 12-Mar-12 8:33am    
Please improve the question and write the code.
It's need to see the code. especially in the timer.Tick event

The procedure Shahin showed would work correctly if called from the event handler of the event System.Windows.Forms.Timer.Tick. Let's say, this is a method called SwitchImageVisibility. Now, a problem with this timer is that it is incredibly inaccurate. If it's fine for your purpose, use it, but I warn it: the errors could be so bad that a user may clearly see that a timer behavior is not periodic.

This lack of accuracy can be unbelievably bad. The lack of periodic behavior can be visible with unarmed eye and very irritating. I would recommend to use this timer in rare cases when accuracy does not matter, and this is not your case. (One case when such a random delay is expected: flash screen or something like that, when the event is handled only once.)

If you create a separate thread with periodic behavior and a delay using System.Threading.Thread.Sleep(5000), it would be much better. Another approach would be using much more accurate timers, System.Threading.Timer or System.Timers.Timer. Please see:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx[^].

But overall, using a thread instead of timer is safer and simpler.

The only benefit of System.Windows.Forms.Timer is that you can call UI methods/properties from its Tick handler, unlike all other cases.

You cannot call anything related to UI from non-UI thread (and other timers will work from a different thread, too). Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

So in your case, if you want to call SwitchImageVisibility in a method defined in a form or a control class, but in a non-UI thread, it will look like this:

C#
this.Invoke(new Action(() => { SwitchImageVisibility(); }), null);


See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 12-Mar-12 10:46am    
+5Perfect as always
Sergey Alexandrovich Kryukov 12-Mar-12 10:56am    
Thank you very much, Shahin.
--SA
If you use WinForm, then In Timer.Tick event:

C#
if(Image1.Visible == true)
{
   Image1.Visible = false;
   Image2.Visible = true;
   return;
}
if(Image2.Visible == true)
{
   Image2.Visible = false;
   Image3.Visible = true;
   return;
}
if(Image3.Visible == true)
{
   Image3.Visible = false;
   Image1.Visible = true;
   return;
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 12-Mar-12 10:31am    
Basically correct, but not everything is so easy. First, you need to write Timer.Tick (C# is case-sensitive). But more importantly, this timer is so inaccurate that it cannot guarantee smooth look or periodic behavior, which can be easily seen by an unarmed eye. I voted 4.

I explain how to solve this problem better (and still easily, major difference is one more line and either a different timer type or, better yet, a thread) -- please see.
Shahin Khorshidnia 12-Mar-12 10:45am    
Thanks SA
siri2012 14-Mar-12 0:56am    
Thanks a lot Friend
Shahin Khorshidnia 14-Mar-12 16:22pm    
You're welcome.

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