Question

Good day to all, Please I need somebody to help me have a look at my codes.I am having this error of** Object reference not set to an instance Of Object**.It appears the error is within this lines of codes

if (_scrollingTimer == null) { _scrollingTimer = new Timer() { Enabled = false, Interval = 500, Tag = (sender as TrackBar).Value };

but unfortunately I was unable to resolve this error.I would be very glad if somebody could help me out.thank you for the usual support.best regards. Firstoption. Below are the remaining part of the codes.

 byte[] data = new byte[5];
 private Timer _scrollingTimer = null;

  private void button3_Click(object sender, EventArgs e)
        {

            UInt32 numBytesWritten = 0;
            data[0] = 1;
            myFtdiDevice.Write(data, 1, ref numBytesWritten);
            data[0] = 0x6A;
            myFtdiDevice.Write(data, 1, ref numBytesWritten);


        }



  private void trackBar1_Scroll(object sender, EventArgs e)
        {

           if(!backgroundWorker1.IsBusy)
           {

               backgroundWorker1.RunWorkerAsync();
           }

        }

     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            UInt32 numBytesWritten = 1;
            string dataToWrite = "#0";

            if (_scrollingTimer  == null)
            {
                _scrollingTimer = new Timer()
                {
                    Enabled = false,
                    Interval = 500,
                    Tag = (sender as TrackBar).Value
                };
                _scrollingTimer.Tick += (s, ea) =>
                {
                    if (trackBar1.Value == (int)_scrollingTimer.Tag)
                    {
                        _scrollingTimer.Stop();
                        myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
                        int percent = (int)(((double)trackBar1.Value / (double)trackBar1.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
                    {
                        _scrollingTimer.Tag = trackBar1.Value;
                    }
                };
                _scrollingTimer.Start();
            }
        }

No correct solution

OTHER TIPS

sender is not a TrackBar. Looks like it's probably backgroundWorker1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top