Updating UI from NFC-Tag-Reader event handler is very slow












2















I have an ACR122U NFC reader where I read MiFare UltraLight NFC tags. I downloaded a framework for my reader. However when the event handler is called, the update of the GUI is very slow.



I have not much experience in threaded programming and event handling. Maybe the solution for my question is very easy.



However it only updates 'label by label' in the UpdateInterface()-method and one all by once. I can see each label disappearing. So the updating of the UI is very slow.



I guess this has something to do with the (hardware-)events called by the reader-classes in background.



The program is generally working only it is very slow.



private void StartMonitor()
{
//Function called when starting the Windows Forms Application
IMonitorFactory monitorfactory = MonitorFactory.Instance;
monitor = monitorfactory.Create(SCardScope.System);
monitor.Start(ReaderNames[0]);
monitor.StatusChanged += Monitor_StatusChanged;
}

private void Monitor_StatusChanged(object sender, StatusChangeEventArgs e)
{
strPassportNo = "";
strPassportNo = lPassportNumberNo.Text;

if (e.NewState.ToString().ToLower().Contains("empty".ToLower()) == true)
{
//Interface should be deleted all by once now as no NFC tag is on the reader.
UpdateInterface();
}
}

private void UpdateInterface()
{
if (InvokeRequired)
{
this.BeginInvoke(new Action(() =>
{
lPassportNumberNo.Text = "";
lPassengerName.Text = "";
lPax.Text = "";
lTableNo.Text = "";
lRoomNo.Text = "";
pbTables.Hide();
pbPasspic.Hide();
this.BackgroundImage = BackgroundWelcome;
}));
}
else
{
lPassportNumberNo.Text = "";
lPassengerName.Text = "";
lPax.Text = "";
lTableNo.Text = "";
lRoomNo.Text = "";
pbTables.Hide();
pbPasspic.Hide();
this.BackgroundImage = BackgroundWelcome;
}
}


The expected output should be that the performance of the UI is a lot faster than it is actually. Or at least the labels with data in it should appear and disappear at once.










share|improve this question

























  • Sounds like you want to batch up the NFC updates coming from the monitor.StatusChanged event. I can't see where you are updating the UI though. You need to do those updates on the UI thread using the beginupdate method.

    – Aron
    Jan 19 at 17:21
















2















I have an ACR122U NFC reader where I read MiFare UltraLight NFC tags. I downloaded a framework for my reader. However when the event handler is called, the update of the GUI is very slow.



I have not much experience in threaded programming and event handling. Maybe the solution for my question is very easy.



However it only updates 'label by label' in the UpdateInterface()-method and one all by once. I can see each label disappearing. So the updating of the UI is very slow.



I guess this has something to do with the (hardware-)events called by the reader-classes in background.



The program is generally working only it is very slow.



private void StartMonitor()
{
//Function called when starting the Windows Forms Application
IMonitorFactory monitorfactory = MonitorFactory.Instance;
monitor = monitorfactory.Create(SCardScope.System);
monitor.Start(ReaderNames[0]);
monitor.StatusChanged += Monitor_StatusChanged;
}

private void Monitor_StatusChanged(object sender, StatusChangeEventArgs e)
{
strPassportNo = "";
strPassportNo = lPassportNumberNo.Text;

if (e.NewState.ToString().ToLower().Contains("empty".ToLower()) == true)
{
//Interface should be deleted all by once now as no NFC tag is on the reader.
UpdateInterface();
}
}

private void UpdateInterface()
{
if (InvokeRequired)
{
this.BeginInvoke(new Action(() =>
{
lPassportNumberNo.Text = "";
lPassengerName.Text = "";
lPax.Text = "";
lTableNo.Text = "";
lRoomNo.Text = "";
pbTables.Hide();
pbPasspic.Hide();
this.BackgroundImage = BackgroundWelcome;
}));
}
else
{
lPassportNumberNo.Text = "";
lPassengerName.Text = "";
lPax.Text = "";
lTableNo.Text = "";
lRoomNo.Text = "";
pbTables.Hide();
pbPasspic.Hide();
this.BackgroundImage = BackgroundWelcome;
}
}


The expected output should be that the performance of the UI is a lot faster than it is actually. Or at least the labels with data in it should appear and disappear at once.










share|improve this question

























  • Sounds like you want to batch up the NFC updates coming from the monitor.StatusChanged event. I can't see where you are updating the UI though. You need to do those updates on the UI thread using the beginupdate method.

    – Aron
    Jan 19 at 17:21














2












2








2








I have an ACR122U NFC reader where I read MiFare UltraLight NFC tags. I downloaded a framework for my reader. However when the event handler is called, the update of the GUI is very slow.



I have not much experience in threaded programming and event handling. Maybe the solution for my question is very easy.



However it only updates 'label by label' in the UpdateInterface()-method and one all by once. I can see each label disappearing. So the updating of the UI is very slow.



I guess this has something to do with the (hardware-)events called by the reader-classes in background.



The program is generally working only it is very slow.



private void StartMonitor()
{
//Function called when starting the Windows Forms Application
IMonitorFactory monitorfactory = MonitorFactory.Instance;
monitor = monitorfactory.Create(SCardScope.System);
monitor.Start(ReaderNames[0]);
monitor.StatusChanged += Monitor_StatusChanged;
}

private void Monitor_StatusChanged(object sender, StatusChangeEventArgs e)
{
strPassportNo = "";
strPassportNo = lPassportNumberNo.Text;

if (e.NewState.ToString().ToLower().Contains("empty".ToLower()) == true)
{
//Interface should be deleted all by once now as no NFC tag is on the reader.
UpdateInterface();
}
}

private void UpdateInterface()
{
if (InvokeRequired)
{
this.BeginInvoke(new Action(() =>
{
lPassportNumberNo.Text = "";
lPassengerName.Text = "";
lPax.Text = "";
lTableNo.Text = "";
lRoomNo.Text = "";
pbTables.Hide();
pbPasspic.Hide();
this.BackgroundImage = BackgroundWelcome;
}));
}
else
{
lPassportNumberNo.Text = "";
lPassengerName.Text = "";
lPax.Text = "";
lTableNo.Text = "";
lRoomNo.Text = "";
pbTables.Hide();
pbPasspic.Hide();
this.BackgroundImage = BackgroundWelcome;
}
}


The expected output should be that the performance of the UI is a lot faster than it is actually. Or at least the labels with data in it should appear and disappear at once.










share|improve this question
















I have an ACR122U NFC reader where I read MiFare UltraLight NFC tags. I downloaded a framework for my reader. However when the event handler is called, the update of the GUI is very slow.



I have not much experience in threaded programming and event handling. Maybe the solution for my question is very easy.



However it only updates 'label by label' in the UpdateInterface()-method and one all by once. I can see each label disappearing. So the updating of the UI is very slow.



I guess this has something to do with the (hardware-)events called by the reader-classes in background.



The program is generally working only it is very slow.



private void StartMonitor()
{
//Function called when starting the Windows Forms Application
IMonitorFactory monitorfactory = MonitorFactory.Instance;
monitor = monitorfactory.Create(SCardScope.System);
monitor.Start(ReaderNames[0]);
monitor.StatusChanged += Monitor_StatusChanged;
}

private void Monitor_StatusChanged(object sender, StatusChangeEventArgs e)
{
strPassportNo = "";
strPassportNo = lPassportNumberNo.Text;

if (e.NewState.ToString().ToLower().Contains("empty".ToLower()) == true)
{
//Interface should be deleted all by once now as no NFC tag is on the reader.
UpdateInterface();
}
}

private void UpdateInterface()
{
if (InvokeRequired)
{
this.BeginInvoke(new Action(() =>
{
lPassportNumberNo.Text = "";
lPassengerName.Text = "";
lPax.Text = "";
lTableNo.Text = "";
lRoomNo.Text = "";
pbTables.Hide();
pbPasspic.Hide();
this.BackgroundImage = BackgroundWelcome;
}));
}
else
{
lPassportNumberNo.Text = "";
lPassengerName.Text = "";
lPax.Text = "";
lTableNo.Text = "";
lRoomNo.Text = "";
pbTables.Hide();
pbPasspic.Hide();
this.BackgroundImage = BackgroundWelcome;
}
}


The expected output should be that the performance of the UI is a lot faster than it is actually. Or at least the labels with data in it should appear and disappear at once.







c# winforms nfc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 21 at 16:13









LarsTech

69.9k12105158




69.9k12105158










asked Jan 19 at 16:27









Benjamin BlumBenjamin Blum

112




112













  • Sounds like you want to batch up the NFC updates coming from the monitor.StatusChanged event. I can't see where you are updating the UI though. You need to do those updates on the UI thread using the beginupdate method.

    – Aron
    Jan 19 at 17:21



















  • Sounds like you want to batch up the NFC updates coming from the monitor.StatusChanged event. I can't see where you are updating the UI though. You need to do those updates on the UI thread using the beginupdate method.

    – Aron
    Jan 19 at 17:21

















Sounds like you want to batch up the NFC updates coming from the monitor.StatusChanged event. I can't see where you are updating the UI though. You need to do those updates on the UI thread using the beginupdate method.

– Aron
Jan 19 at 17:21





Sounds like you want to batch up the NFC updates coming from the monitor.StatusChanged event. I can't see where you are updating the UI though. You need to do those updates on the UI thread using the beginupdate method.

– Aron
Jan 19 at 17:21












2 Answers
2






active

oldest

votes


















0














Not sure what is this actually: this.BackgroundImage = BackgroundWelcome; but is it really necessary to assign such static data every time?



Also i dont think that frequency of your update event is too high - information on the screen should be human-readable so it's definitely only one update of all data required after the entire tag is readed. If there are many read events for every single tag - then store all of them into temporary list and update the form at the read end (entire tag).
When i say "tag" i mean the entire mifare card content.






share|improve this answer































    0














    The UI-Update was in my case to just clear the labels (see UpdateInterface()). However I found the "error". It was the ".jpg" file als BackgroundImage I loaded on every refresh. Although it was "just" 1280x1024, it was too much for the frontend to update.



    When I cleared the Images, the refresh was immediately.



    My solution now is:



    this.BackgroundImage = ((System.Drawing.Image(resources.GetObject("$this.BackgroundImage")));
    this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
    this.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true);


    in the Form1.Designer.cs



    and Suspending and Resuming the Layout with the following class (which I found somewhere on the Internet)



    public static class ControlHelper
    {
    #region Redraw Suspend/Resume
    [DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
    private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
    private const int WM_SETREDRAW = 0xB;

    public static void SuspendDrawing(this Control target)
    {
    SendMessage(target.Handle, WM_SETREDRAW, 0, 0);
    }

    public static void ResumeDrawing(this Control target) { ResumeDrawing(target, true); }
    public static void ResumeDrawing(this Control target, bool redraw)
    {
    SendMessage(target.Handle, WM_SETREDRAW, 1, 0);

    if (redraw)
    {
    target.Refresh();
    }
    }
    #endregion
    }


    Also I changed the ".jpg" files to ".bmp" files. Now the Interface is smooth and fast.



    And @Jesting:




    Not sure what is this actually: this.BackgroundImage = BackgroundWelcome; but is it really necessary to assign such static data every time?




    No, I could have found a better solution. However as the application is very small and I only have four background images, I found it easier to "hard code" it.



    Finally, the tag(-reader) was not really the problem.



    And for updating the UI, I use this quite easy function:



    private void UpdateInterface(PassportDatabaseReader.Passport _passport)
    {
    if (InvokeRequired)
    {
    this.BeginInvoke(new Action<PassportDatabaseReader.Passport>(UpdateInterface), new object { _passport });
    return;
    }
    else
    {
    ControlHelper.SuspendDrawing(this);
    new Thread(() => ChangeBackground(BackgroundPassport)).Start();
    lPassportNumberNo.Text = _passport.PassportNo;
    strPassportNo = _passport.PassportNo;
    lPassengerName.Text = _passport.Name.Replace('$','n');
    lPax.Text = _passport.Pax;
    lTableNo.Text = _passport.TableNo;
    lRoomNo.Text = _passport.RoomNo;
    pbTables.Hide();
    try
    {
    pbPasspic.Load("..\..\Pics\" + _passport.PassportNo + ".png");
    pbPasspic.Show();
    pbTables.ImageLocation = "..\..\Tischordnung_" + _passport.TableNo + ".png";
    pbTables.Show();
    }
    catch (Exception)
    {
    throw;
    }
    ControlHelper.ResumeDrawing(this);
    }
    }


    The application is a "wedding passport" reader which shows the name, the guests room number, table number as well as the "passport" picture on screen.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54269111%2fupdating-ui-from-nfc-tag-reader-event-handler-is-very-slow%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Not sure what is this actually: this.BackgroundImage = BackgroundWelcome; but is it really necessary to assign such static data every time?



      Also i dont think that frequency of your update event is too high - information on the screen should be human-readable so it's definitely only one update of all data required after the entire tag is readed. If there are many read events for every single tag - then store all of them into temporary list and update the form at the read end (entire tag).
      When i say "tag" i mean the entire mifare card content.






      share|improve this answer




























        0














        Not sure what is this actually: this.BackgroundImage = BackgroundWelcome; but is it really necessary to assign such static data every time?



        Also i dont think that frequency of your update event is too high - information on the screen should be human-readable so it's definitely only one update of all data required after the entire tag is readed. If there are many read events for every single tag - then store all of them into temporary list and update the form at the read end (entire tag).
        When i say "tag" i mean the entire mifare card content.






        share|improve this answer


























          0












          0








          0







          Not sure what is this actually: this.BackgroundImage = BackgroundWelcome; but is it really necessary to assign such static data every time?



          Also i dont think that frequency of your update event is too high - information on the screen should be human-readable so it's definitely only one update of all data required after the entire tag is readed. If there are many read events for every single tag - then store all of them into temporary list and update the form at the read end (entire tag).
          When i say "tag" i mean the entire mifare card content.






          share|improve this answer













          Not sure what is this actually: this.BackgroundImage = BackgroundWelcome; but is it really necessary to assign such static data every time?



          Also i dont think that frequency of your update event is too high - information on the screen should be human-readable so it's definitely only one update of all data required after the entire tag is readed. If there are many read events for every single tag - then store all of them into temporary list and update the form at the read end (entire tag).
          When i say "tag" i mean the entire mifare card content.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 17:52









          JestingJesting

          1




          1

























              0














              The UI-Update was in my case to just clear the labels (see UpdateInterface()). However I found the "error". It was the ".jpg" file als BackgroundImage I loaded on every refresh. Although it was "just" 1280x1024, it was too much for the frontend to update.



              When I cleared the Images, the refresh was immediately.



              My solution now is:



              this.BackgroundImage = ((System.Drawing.Image(resources.GetObject("$this.BackgroundImage")));
              this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
              this.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true);


              in the Form1.Designer.cs



              and Suspending and Resuming the Layout with the following class (which I found somewhere on the Internet)



              public static class ControlHelper
              {
              #region Redraw Suspend/Resume
              [DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
              private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
              private const int WM_SETREDRAW = 0xB;

              public static void SuspendDrawing(this Control target)
              {
              SendMessage(target.Handle, WM_SETREDRAW, 0, 0);
              }

              public static void ResumeDrawing(this Control target) { ResumeDrawing(target, true); }
              public static void ResumeDrawing(this Control target, bool redraw)
              {
              SendMessage(target.Handle, WM_SETREDRAW, 1, 0);

              if (redraw)
              {
              target.Refresh();
              }
              }
              #endregion
              }


              Also I changed the ".jpg" files to ".bmp" files. Now the Interface is smooth and fast.



              And @Jesting:




              Not sure what is this actually: this.BackgroundImage = BackgroundWelcome; but is it really necessary to assign such static data every time?




              No, I could have found a better solution. However as the application is very small and I only have four background images, I found it easier to "hard code" it.



              Finally, the tag(-reader) was not really the problem.



              And for updating the UI, I use this quite easy function:



              private void UpdateInterface(PassportDatabaseReader.Passport _passport)
              {
              if (InvokeRequired)
              {
              this.BeginInvoke(new Action<PassportDatabaseReader.Passport>(UpdateInterface), new object { _passport });
              return;
              }
              else
              {
              ControlHelper.SuspendDrawing(this);
              new Thread(() => ChangeBackground(BackgroundPassport)).Start();
              lPassportNumberNo.Text = _passport.PassportNo;
              strPassportNo = _passport.PassportNo;
              lPassengerName.Text = _passport.Name.Replace('$','n');
              lPax.Text = _passport.Pax;
              lTableNo.Text = _passport.TableNo;
              lRoomNo.Text = _passport.RoomNo;
              pbTables.Hide();
              try
              {
              pbPasspic.Load("..\..\Pics\" + _passport.PassportNo + ".png");
              pbPasspic.Show();
              pbTables.ImageLocation = "..\..\Tischordnung_" + _passport.TableNo + ".png";
              pbTables.Show();
              }
              catch (Exception)
              {
              throw;
              }
              ControlHelper.ResumeDrawing(this);
              }
              }


              The application is a "wedding passport" reader which shows the name, the guests room number, table number as well as the "passport" picture on screen.






              share|improve this answer




























                0














                The UI-Update was in my case to just clear the labels (see UpdateInterface()). However I found the "error". It was the ".jpg" file als BackgroundImage I loaded on every refresh. Although it was "just" 1280x1024, it was too much for the frontend to update.



                When I cleared the Images, the refresh was immediately.



                My solution now is:



                this.BackgroundImage = ((System.Drawing.Image(resources.GetObject("$this.BackgroundImage")));
                this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
                this.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true);


                in the Form1.Designer.cs



                and Suspending and Resuming the Layout with the following class (which I found somewhere on the Internet)



                public static class ControlHelper
                {
                #region Redraw Suspend/Resume
                [DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
                private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
                private const int WM_SETREDRAW = 0xB;

                public static void SuspendDrawing(this Control target)
                {
                SendMessage(target.Handle, WM_SETREDRAW, 0, 0);
                }

                public static void ResumeDrawing(this Control target) { ResumeDrawing(target, true); }
                public static void ResumeDrawing(this Control target, bool redraw)
                {
                SendMessage(target.Handle, WM_SETREDRAW, 1, 0);

                if (redraw)
                {
                target.Refresh();
                }
                }
                #endregion
                }


                Also I changed the ".jpg" files to ".bmp" files. Now the Interface is smooth and fast.



                And @Jesting:




                Not sure what is this actually: this.BackgroundImage = BackgroundWelcome; but is it really necessary to assign such static data every time?




                No, I could have found a better solution. However as the application is very small and I only have four background images, I found it easier to "hard code" it.



                Finally, the tag(-reader) was not really the problem.



                And for updating the UI, I use this quite easy function:



                private void UpdateInterface(PassportDatabaseReader.Passport _passport)
                {
                if (InvokeRequired)
                {
                this.BeginInvoke(new Action<PassportDatabaseReader.Passport>(UpdateInterface), new object { _passport });
                return;
                }
                else
                {
                ControlHelper.SuspendDrawing(this);
                new Thread(() => ChangeBackground(BackgroundPassport)).Start();
                lPassportNumberNo.Text = _passport.PassportNo;
                strPassportNo = _passport.PassportNo;
                lPassengerName.Text = _passport.Name.Replace('$','n');
                lPax.Text = _passport.Pax;
                lTableNo.Text = _passport.TableNo;
                lRoomNo.Text = _passport.RoomNo;
                pbTables.Hide();
                try
                {
                pbPasspic.Load("..\..\Pics\" + _passport.PassportNo + ".png");
                pbPasspic.Show();
                pbTables.ImageLocation = "..\..\Tischordnung_" + _passport.TableNo + ".png";
                pbTables.Show();
                }
                catch (Exception)
                {
                throw;
                }
                ControlHelper.ResumeDrawing(this);
                }
                }


                The application is a "wedding passport" reader which shows the name, the guests room number, table number as well as the "passport" picture on screen.






                share|improve this answer


























                  0












                  0








                  0







                  The UI-Update was in my case to just clear the labels (see UpdateInterface()). However I found the "error". It was the ".jpg" file als BackgroundImage I loaded on every refresh. Although it was "just" 1280x1024, it was too much for the frontend to update.



                  When I cleared the Images, the refresh was immediately.



                  My solution now is:



                  this.BackgroundImage = ((System.Drawing.Image(resources.GetObject("$this.BackgroundImage")));
                  this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
                  this.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true);


                  in the Form1.Designer.cs



                  and Suspending and Resuming the Layout with the following class (which I found somewhere on the Internet)



                  public static class ControlHelper
                  {
                  #region Redraw Suspend/Resume
                  [DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
                  private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
                  private const int WM_SETREDRAW = 0xB;

                  public static void SuspendDrawing(this Control target)
                  {
                  SendMessage(target.Handle, WM_SETREDRAW, 0, 0);
                  }

                  public static void ResumeDrawing(this Control target) { ResumeDrawing(target, true); }
                  public static void ResumeDrawing(this Control target, bool redraw)
                  {
                  SendMessage(target.Handle, WM_SETREDRAW, 1, 0);

                  if (redraw)
                  {
                  target.Refresh();
                  }
                  }
                  #endregion
                  }


                  Also I changed the ".jpg" files to ".bmp" files. Now the Interface is smooth and fast.



                  And @Jesting:




                  Not sure what is this actually: this.BackgroundImage = BackgroundWelcome; but is it really necessary to assign such static data every time?




                  No, I could have found a better solution. However as the application is very small and I only have four background images, I found it easier to "hard code" it.



                  Finally, the tag(-reader) was not really the problem.



                  And for updating the UI, I use this quite easy function:



                  private void UpdateInterface(PassportDatabaseReader.Passport _passport)
                  {
                  if (InvokeRequired)
                  {
                  this.BeginInvoke(new Action<PassportDatabaseReader.Passport>(UpdateInterface), new object { _passport });
                  return;
                  }
                  else
                  {
                  ControlHelper.SuspendDrawing(this);
                  new Thread(() => ChangeBackground(BackgroundPassport)).Start();
                  lPassportNumberNo.Text = _passport.PassportNo;
                  strPassportNo = _passport.PassportNo;
                  lPassengerName.Text = _passport.Name.Replace('$','n');
                  lPax.Text = _passport.Pax;
                  lTableNo.Text = _passport.TableNo;
                  lRoomNo.Text = _passport.RoomNo;
                  pbTables.Hide();
                  try
                  {
                  pbPasspic.Load("..\..\Pics\" + _passport.PassportNo + ".png");
                  pbPasspic.Show();
                  pbTables.ImageLocation = "..\..\Tischordnung_" + _passport.TableNo + ".png";
                  pbTables.Show();
                  }
                  catch (Exception)
                  {
                  throw;
                  }
                  ControlHelper.ResumeDrawing(this);
                  }
                  }


                  The application is a "wedding passport" reader which shows the name, the guests room number, table number as well as the "passport" picture on screen.






                  share|improve this answer













                  The UI-Update was in my case to just clear the labels (see UpdateInterface()). However I found the "error". It was the ".jpg" file als BackgroundImage I loaded on every refresh. Although it was "just" 1280x1024, it was too much for the frontend to update.



                  When I cleared the Images, the refresh was immediately.



                  My solution now is:



                  this.BackgroundImage = ((System.Drawing.Image(resources.GetObject("$this.BackgroundImage")));
                  this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
                  this.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true);


                  in the Form1.Designer.cs



                  and Suspending and Resuming the Layout with the following class (which I found somewhere on the Internet)



                  public static class ControlHelper
                  {
                  #region Redraw Suspend/Resume
                  [DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
                  private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
                  private const int WM_SETREDRAW = 0xB;

                  public static void SuspendDrawing(this Control target)
                  {
                  SendMessage(target.Handle, WM_SETREDRAW, 0, 0);
                  }

                  public static void ResumeDrawing(this Control target) { ResumeDrawing(target, true); }
                  public static void ResumeDrawing(this Control target, bool redraw)
                  {
                  SendMessage(target.Handle, WM_SETREDRAW, 1, 0);

                  if (redraw)
                  {
                  target.Refresh();
                  }
                  }
                  #endregion
                  }


                  Also I changed the ".jpg" files to ".bmp" files. Now the Interface is smooth and fast.



                  And @Jesting:




                  Not sure what is this actually: this.BackgroundImage = BackgroundWelcome; but is it really necessary to assign such static data every time?




                  No, I could have found a better solution. However as the application is very small and I only have four background images, I found it easier to "hard code" it.



                  Finally, the tag(-reader) was not really the problem.



                  And for updating the UI, I use this quite easy function:



                  private void UpdateInterface(PassportDatabaseReader.Passport _passport)
                  {
                  if (InvokeRequired)
                  {
                  this.BeginInvoke(new Action<PassportDatabaseReader.Passport>(UpdateInterface), new object { _passport });
                  return;
                  }
                  else
                  {
                  ControlHelper.SuspendDrawing(this);
                  new Thread(() => ChangeBackground(BackgroundPassport)).Start();
                  lPassportNumberNo.Text = _passport.PassportNo;
                  strPassportNo = _passport.PassportNo;
                  lPassengerName.Text = _passport.Name.Replace('$','n');
                  lPax.Text = _passport.Pax;
                  lTableNo.Text = _passport.TableNo;
                  lRoomNo.Text = _passport.RoomNo;
                  pbTables.Hide();
                  try
                  {
                  pbPasspic.Load("..\..\Pics\" + _passport.PassportNo + ".png");
                  pbPasspic.Show();
                  pbTables.ImageLocation = "..\..\Tischordnung_" + _passport.TableNo + ".png";
                  pbTables.Show();
                  }
                  catch (Exception)
                  {
                  throw;
                  }
                  ControlHelper.ResumeDrawing(this);
                  }
                  }


                  The application is a "wedding passport" reader which shows the name, the guests room number, table number as well as the "passport" picture on screen.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 19 at 19:17









                  Benjamin BlumBenjamin Blum

                  112




                  112






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54269111%2fupdating-ui-from-nfc-tag-reader-event-handler-is-very-slow%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Homophylophilia

                      Updating UILabel text programmatically using a function

                      Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage