none
multithreading problem RRS feed

  • Question

  • hi every bod i m new in threading and i have this trouble: i have a C++ function that return to me card number of my users, i want add this function to another thread of my UI  for checking any card checkout on my device, currently i m using a timer with 100 ms Interval and i haven't any problem but when i want use another thread in background my form doesn't response and nothing work so whats the my problem this is my code for create thread:

    //loopTimer.Enabled = true;
          Thread loopThread = new Thread(delegate()
          {
            while (true)
            {
              try
              {
                if (new Nurture.ReserveService.ReserveService().semiHandshake().Equals("ok"))
                { }
              }
              catch
              {
                DoInLostConnection();
                return;
              }
              currCardNum = "";
              String cardNum = null;
              cardNum = ReadCardNum();
              if (cardNum != null && cardNum.Trim().Length > 0)
              {
                //loopTimer.Enabled = false;
                currCardNum = cardNum.Trim();
                picBoxWaiting.Visible = true;
                timerShowPanel.Enabled = true;
                return;
              }
              Thread.Sleep(10);
            }
          });<br/>
    <br/>
          loopThread.IsBackground = true;<br/>
          loopThread.Start();
    
    so whats wrong thanks a lot.

     

    Friday, October 29, 2010 7:52 AM

Answers

  • Hi Danics,

    If on desktop, backgroundworker is the best option, since it is not available on device, we have to use control.Invoke or control.beginInvoke to do it.

     Here is a sample:
    http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

     

    If you can't make it work using control.Invoke, please provide detailed sample code, which can help us find the issue.

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by Danics Tuesday, December 7, 2010 12:06 PM
    Tuesday, November 2, 2010 6:09 AM
    Moderator

All replies

  • Not really sure what the problem is but from your description is it possible that you are updating a UI Component from a thread that did not create your UI Component.

    In .Net you cannot update a UI Component from a non-UI thread.  So if you are doing that in your background thread you need to use InvokeRequired and either BeginInvoke or Invoke depending on your design.

     

    Friday, October 29, 2010 4:37 PM
  • i m using BeginInvoke too but my form dont load until i m using a card for breaking while so whats the best option for me i want create a thread in background for reading RFID card and run a event or somthing like that to change my UI, thanks a lot.
    Saturday, October 30, 2010 6:53 PM
  • Hi Danics,

    If on desktop, backgroundworker is the best option, since it is not available on device, we have to use control.Invoke or control.beginInvoke to do it.

     Here is a sample:
    http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

     

    If you can't make it work using control.Invoke, please provide detailed sample code, which can help us find the issue.

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by Danics Tuesday, December 7, 2010 12:06 PM
    Tuesday, November 2, 2010 6:09 AM
    Moderator
  • i m using this code for my invoke without any change
    Wednesday, November 3, 2010 6:10 PM