Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Graphic User Interface freezes after 2 mins (http://www.go4expert.com/forums/graphic-user-interface-freezes-2-mins-t30100/)

firstoption 14May2014 20:05

Graphic User Interface freezes after 2 mins
 
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.

shabbir 15May2014 09:44

Re: Graphic User Interface freezes after 2 mins
 
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.


All times are GMT +5.5. The time now is 18:03.