Le réseau pour les développeurs > Forums - Accueil > Visual C# General > Can a System.Timers.Timer update a label location?
Poser une questionPoser une question
 

QuestionCan a System.Timers.Timer update a label location?

  • mercredi 4 novembre 2009 17:19Afikim Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hello

    I'm trying to make a label that is updated uninterruptedly.

    Anytime I start another control (such as mediaplayer or OpenFileDialog), the timer continues working but the GUI stops.

    I tried Forms.Timer and Systems.Timers.Timer.  Both have the same problem.

    I will be glad to know if there is any way to keep the GUI running.

    Thanks

    Eli

Toutes les réponses

  • mercredi 4 novembre 2009 17:32Rudedog2ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    The GUI always runs.  Sometimes it waits for time-consuming stuff to finish.
    Events are usually handled when the currently executing method finishes. 
    What if the method takes its' time to finish?  Like playing a song.
    In those cases, you may wish to execute that stuff on a background thread, not the main thread.



    Mark the best replies as answers. "Fooling computers since 1971."
  • mercredi 4 novembre 2009 21:06Afikim Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hello Rudedog1

    Thanks for your answer.

    I need to put the mediaplayer in the background thread.  Anyy idea how to make it properly?

    Thanks

    Eli



  • mercredi 4 novembre 2009 21:14Loofy Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    I don't recommend using a label control for this. It will have to keep redrawing the window every second, which might look a bit choppy.

    anyways, you need to trigger its onpaint event. I don't know the equivalent in .NET, but in win32 you would have to call invalidateRect
  • mercredi 4 novembre 2009 21:15Loofy Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    see: backgroundworker controls
  • mercredi 4 novembre 2009 21:19Rudedog2ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    How are you 'updating a label'?  Post the code.

    Opening up an OpenFileDialog as you say should not make the UI freeze up on the form that launched it.
    Mark the best replies as answers. "Fooling computers since 1971."
  • jeudi 5 novembre 2009 04:36Afikim Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hi Rudedog2

    Here is my code.  I have a form with 2 BackgroundWorkers.  One is dealing with the label. The second loads a playlist and plays the clips.

    The first load is almost not making any troubles.  But, when it changes to the next clip, the label stops until after the loading.

    Thanks

    Eli


    The code:

     

    public partial class Form1 : Form

    {

     

    private int LabelLocation = 0;

     

    private int FormWidth;

     

    private IWMPPlaylist mdlPlayList;

     

     

    public Form1()

    {

    InitializeComponent();

    }

     

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

    {

     

    BackgroundWorker bw = sender as BackgroundWorker;

    System.Threading.

    Interlocked.Exchange(ref LabelLocation, 1);

     

    try

    {

     

    while (true)

    {

    LabelLocation = LabelLocation + 1;

     

    if (LabelLocation > FormWidth)

    LabelLocation = 0 - label1.Width;

    bw.ReportProgress(LabelLocation);

     

    Thread.Sleep(10);

    }

    }

     

    catch (Exception Ex)

    { }

    }

     

    private void button1_Click(object sender, EventArgs e)

    {

    backgroundWorker1.RunWorkerAsync();

     

     

    }

     

     

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

    {

    label1.Left = e.ProgressPercentage;

    }

     

     

     

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

    {

    openFileDialog1.ShowDialog();

    }

     

    private void Form1_Load(object sender, EventArgs e)

    {

    FormWidth =

    this.Width;

    }

     

    private void timer1_Tick(object sender, EventArgs e)

    {

    label1.Left = label1.Left + 1;

     

    if (label1.Left > this.Width)

    label1.Left = 10 - label1.Width;

    }

     

    private void button2_Click(object sender, EventArgs e)

    {

    backgroundWorker2.RunWorkerAsync();

     

    }

     

    private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)

    {

    mdlPlayList = axWindowsMediaPlayer1.newPlaylist(

    "MyPlayList", "");

    mdlPlayList.appendItem(axWindowsMediaPlayer1.newMedia(

    "c:\\FirstClip.avi"));

    mdlPlayList.appendItem(axWindowsMediaPlayer1.newMedia(

    "c:\\SecondClip.avi"));

    }

     

    private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

    {

    axWindowsMediaPlayer1.settings.setMode(

    "loop", true);

    axWindowsMediaPlayer1.currentPlaylist = mdlPlayList;

    }

     

    private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)

    {

    }

     

    }

  • mardi 10 novembre 2009 07:23Harry ZhuMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hi,

    I create a project according to the code in you post but could not reproduce the problem.
    I just tested with OpenFileDialog.

    It can be download here:
    http://cid-219dc49fbdfffbd1.skydrive.live.com/self.aspx/Public/OpenFileDialog.rar

    Could you please upload your project so that we can test it?

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • mardi 10 novembre 2009 08:48Afikim Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hello Harry

    Thanks for your comment.

    Unfortunately, it will not solve cases that timer interval is small (e.g. 10ms)

    Thanks

    Eli
  • mardi 10 novembre 2009 09:17Harry ZhuMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hi,

    You might want to use a usercontrol to show the label, I create a test and it works well:
    http://cid-219dc49fbdfffbd1.skydrive.live.com/self.aspx/Public/OFD.rar

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • mercredi 11 novembre 2009 02:27Harry ZhuMSFT, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hi,

    Have you tried it?

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • mercredi 11 novembre 2009 13:28Afikim Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Hello Harry

    I just opened it now.  Thanks

    unfortunately, the running label stops for a while.

    Thanks

    Eli