Graphic User Interface freezes after 2 mins

firstoption

Member
Joined
Apr 23, 2014
Messages
10
Programming Experience
Beginner
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.

C#:
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,141
Location
Sydney, Australia
Programming Experience
10+
Wouldn't it make more sense to add your Timers in the designer and then just Stop and Start them in your Scroll event handlers, rather than creating new Timers every time you scroll? Also, I haven't looked but I would think that a TrackBar would have a ValueChanged event or the like and that it would make more sense to just handle that.
 
Top Bottom