0

Hi everyone this is my first question to stackoverflow and sorry for my English. I searched for a week and couldn't find the solution. I am working on a project which uses RFID antenna and tags . A machine reads the tags and produces tag id like bcbc 0000 or abab 1111 ... Every id points a unique product like shirt , panth etc.This program is a product counter. My form application uses this id's matches with the products and counts them. When program gets a shirt id I want to increase "shirt count label" on the form at the reading time. I wrote 2 different programs and both didn't update the label.

my codes :

System.Timers.Timer aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;
        aTimer.Start();
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

reading part is below;

 reader.Connect(SolutionConstants.ReaderHostname);

            Settings settings = reader.QueryDefaultSettings();

            settings.Report.IncludeFastId = true;

            settings.Report.IncludeAntennaPortNumber = true; // WS

            settings.Antennas.GetAntenna(1).MaxTransmitPower = true;
            settings.Antennas.GetAntenna(1).MaxRxSensitivity = true;
            settings.Antennas.GetAntenna(2).MaxTransmitPower = true;
            settings.Antennas.GetAntenna(2).MaxRxSensitivity = true;
           ...... and other settings here....


            // Apply the newly modified settings.
            reader.ApplySettings(settings);

            // Assign the TagsReported event handler.
            // This specifies which method to call
            // when tags reports are available.
            reader.TagsReported += OnTagsReported;

            // Start reading.
            reader.Start();

in OntagsReported() function i do some controls and the important part is

tagList.Add(tag.Epc.ToString()); // adds tags to tagList.

timer function ;

 private void OnTimedEvent(object source, ElapsedEventArgs e)
    {         
        for (int i = 0; i < tagList.Count; i++)
        {
            if (!usedTags.Contains(tagList[i]))
            {
                //MessageBox.Show("");
                label1.Text = "Text Updated.";
                //productCounter(tagList);
                usedTags.Add(tagList[i]);
            }
        }
    }

Everything is working . Program goes last if control. If I write there a messageBox it shows that but on the next line label does not change. Thanks for help :)

2 Answers2

1

System.Windows.Forms.Timer instead of System.Timers.Timer could help you.

System.Timers.Timer fires event in the non-UI thread, so you should not access UI controls directly in the event handlers.

Ripple
  • 1,257
  • 1
  • 9
  • 15
0

Seems like you need to marshal the call to the UI thread.

label1.BeginInvoke(new Action(() => label1.Text = "Text Updated"));
Stuart
  • 575
  • 2
  • 10