Microsoft Developer Network > 포럼 홈 > Visual C# General > Can a System.Timers.Timer update a label location?
질문하기질문하기
 

질문Can a System.Timers.Timer update a label location?

  • 2009년 11월 4일 수요일 오후 5:19Afikim 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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

모든 응답

  • 2009년 11월 4일 수요일 오후 5:32Rudedog2중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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."
  • 2009년 11월 4일 수요일 오후 9:06Afikim 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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



  • 2009년 11월 4일 수요일 오후 9:14Loofy 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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
  • 2009년 11월 4일 수요일 오후 9:15Loofy 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    see: backgroundworker controls
  • 2009년 11월 4일 수요일 오후 9:19Rudedog2중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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."
  • 2009년 11월 5일 목요일 오전 4:36Afikim 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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)

    {

    }

     

    }

  • 2009년 11월 10일 화요일 오전 7:23Harry ZhuMSFT, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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.
  • 2009년 11월 10일 화요일 오전 8:48Afikim 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Hello Harry

    Thanks for your comment.

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

    Thanks

    Eli
  • 2009년 11월 10일 화요일 오전 9:17Harry ZhuMSFT, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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.
  • 2009년 11월 11일 수요일 오전 2:27Harry ZhuMSFT, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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.
  • 2009년 11월 11일 수요일 오후 1:28Afikim 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Hello Harry

    I just opened it now.  Thanks

    unfortunately, the running label stops for a while.

    Thanks

    Eli