1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Graphic User Interface freezes after 2 mins

Discussion in 'C#' started by firstoption, May 14, 2014.

  1. firstoption

    firstoption New Member

    Joined:
    Apr 1, 2014
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Good day to all,
    Please i need your support on how to solve this Problem.I developed a graphic user Interface with 37 trackbars.the trackbars are used to control Led brightness.The application works very well except that after 2mins it hangs.So i have to re-load it before it can be used again.The lines of code below is for two of the 37 trackbar.
    Code:
    private Timer _scrollingTimer = null;
    
    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        if (_scrollingTimer == null)
        {
            // Will tick every 500ms (change as required)
            _scrollingTimer = new Timer() 
            {
                    Enabled = false,
                    Interval = 500,
                    Tag = (sender as TrackBar).Value
            };
            _scrollingTimer.Tick += (s, ea) =>
            {
                // check to see if the value has changed since we last ticked
                if (trackBar1.Value == (int)_scrollingTimer.Tag)
                {
                    // scrolling has stopped so we are good to go ahead and do stuff
                    _scrollingTimer.Stop();
                   myFtdiDevice.write(dataTowrite,dataTowrite.Length,ref  numByteswritten);
    
        int  percent =(int )(((double)trackbar1.Value/(double)reackbar1.Maximum) * 100);
    
        label2.Text = (percent.ToString()) + "%";
    
        data[0] = Convert.ToByte(percent);
    
        data[1] = 0x6A;
    
        myFtdiDevice.write(data,2,ref  numByteswritten);
    
                 
                    _scrollingTimer.Dispose();
                    _scrollingTimer = null;
                }
                else
                {
                    // record the last value seen
                    _scrollingTimer.Tag = trackBar1.Value;
                }
            };
            _scrollingTimer.Start();
        }
    }
    private void trackBar2_Scroll(object sender, EventArgs e)
    {
        if (_scrollingTimer == null)
        {
            // Will tick every 500ms (change as required)
            _scrollingTimer = new Timer() 
            {
                    Enabled = false,
                    Interval = 500,
                    Tag = (sender as TrackBar).Value
            };
            _scrollingTimer.Tick += (s, ea) =>
            {
                // check to see if the value has changed since we last ticked
                if (trackBar2.Value == (int)_scrollingTimer.Tag)
                {
                    // scrolling has stopped so we are good to go ahead and do stuff
                    _scrollingTimer.Stop();
                   myFtdiDevice.write(dataTowrite,dataTowrite.Length,ref  numByteswritten);
    
        int  percent =(int )(((double)trackbar2.Value/(double)reackbar2.Maximum) * 100);
    
        label3.Text = (percent.ToString()) + "%";
    
        data[0] = Convert.ToByte(percent);
    
        data[1] = 0x6A;
    
        myFtdiDevice.write(data,2,ref  numByteswritten);
    
                 
                    _scrollingTimer.Dispose();
                    _scrollingTimer = null;
                }
                else
                {
                    // record the last value seen
                    _scrollingTimer.Tag = trackBar2.Value;
                }
            };
            _scrollingTimer.Start();
        }
    }
    
    Is there any way i can adjust these code in order to prevent freezing.Thank you all for the usual Support.Best regards.
    Firstoption.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    You should do the operations in non UI thread and notify the UI thread to update things. Doing everything in the main UI thread can block things up. All calculations and other things can be moved to non UI thread.
     

Share This Page