1

Below is the method to start a thread in compact framework 3.5

public ScanEntry(string scanId)
{
   InitializeComponent();
    _scanId = scanId;
    //reader = deviceFactory.Create();
    //reader.YMEvent += new ScanEventHandler(reader_Reading);
    //reader.Enable();
 }


private void CasesEntry_Load(object sender, EventArgs e)
{
      caseCounterLabel.Text = cases.Count.ToString();
      scanIdValueLabel.Text = _scanId;
}



internal void menuItemNewScan_Click(object sender, EventArgs e)
{
       System.Threading.ThreadStart threadDelegate = new System.Threading.ThreadStart(ScanEvents);
       System.Threading.Thread newThread = new System.Threading.Thread(threadDelegate);
       newThread.Start();
}

which calls the below method on thread

private void ScanEvents()
{

  try
  {

     //some other codes     
      if (scanIdValueLabel.InvokeRequired)
     {
          scanIdValueLabel.Invoke((Action)(() => scanIdValueLabel.Text = "value"));
      }  


     attributeNode = docEventFile.CreateNode(XmlNodeType.Element, "Attribute", string.Empty);
     XMLUtils.CreateAttribute(docEventFile, attributeNode, "name", "SCANID");
     XMLUtils.CreateAttribute(docEventFile, attributeNode, "value", scanIdValueLabel.Text);
     attributeSetNode.AppendChild(attributeNode);
     //some other codes
  }
  catch(Execption e)
  {
     Message.Show(e.Message);
  }
}

Errors:

TextAlign = 'scanIdValueLabel.TextAlign' threw an exception of type 'System.NotSupportedException' 
base {System.SystemException} = {"Control.Invoke must be used to interact with controls created on a separate thread."}

In Line

XMLUtils.CreateAttribute(docEventFile, attributeNode, "value", scanIdValueLabel.Text);

I am getting Control.Invoke must be used to interact with controls created on a separate thread at this line

XMLUtils.CreateAttribute(docEventFile, attributeNode, "value", scanIdValueLabel.Text);

I have googled and tried with that solutions but not worked for me.Can any one help me in doing this.

Thanks

Royal
  • 752
  • 2
  • 14
  • 29
  • 2
    You cannot access the `scanIdValueLabel.Text` from another thread. – S.Spieker Jul 07 '14 at 12:50
  • What is behind Message.Show, is this one interacting with the controls on your form? – Complexity Jul 07 '14 at 12:53
  • @S.Spieker Can you explain me some what clear. – Royal Jul 07 '14 at 12:53
  • @Complexity Message.Show i just placed for debug to know the exact error.But i tried without that and not worked. – Royal Jul 07 '14 at 12:54
  • It's a general rule that you should and cannot access forms properties from another thread than the thread creating the controls... – Complexity Jul 07 '14 at 12:55
  • possible duplicate of [Thread Control.Invoke](http://stackoverflow.com/questions/1423446/thread-control-invoke) – Daniel Kelley Jul 07 '14 at 12:57
  • @Complexity Ok,Can you tell me in my case how to handle a thread – Royal Jul 07 '14 at 12:58
  • See my answer below. Does that help you in achieving what you want? – Complexity Jul 07 '14 at 13:00
  • possible duplicate of [Best Way to Invoke Any Cross-Threaded Code?](http://stackoverflow.com/questions/711408/best-way-to-invoke-any-cross-threaded-code) – Complexity Jul 07 '14 at 13:03
  • @S.Spieker You can access the Text property, or any other property of a UI control from a background thread, as long as you don't try to modify it. Only when you write to it, invoking on Dispatcher thread is required. – Darek Jul 07 '14 at 13:04
  • @Royal Can you show us what is happening in XMLUtils.CreateAttribute? – Darek Jul 07 '14 at 13:05
  • According to some comments om what I have replied, when you're on you're seperate thread, do you try to modify any of your controls? – Complexity Jul 07 '14 at 13:09
  • @Complexity I am not modify any controls,however i am processing the values of controls in ScanEvents method. – Royal Jul 08 '14 at 05:57

1 Answers1

9

When you're dealing with Winforms, WPF, Silverlight there's the following sentence which is very important:

The UI elements can only be accessed by the UI thread. WinForms, WPF, Silverlight doesn't allow access to controls from multiple threads.

However, there is a solution which can be found here:

Update: I've created a sample application to make some things clear:

enter image description here

I've created a form first with a button and a label on it. The label is not visible because it doesn't contain text, but it's right underneath the button.

Scenario 1: Updating without threads:

private void btnStartThread_Click(object sender, EventArgs e)
{
    lblMessage.Text = "Button has been clicked.";
}

Off course this is not a problem. It's some standard code:

Scenario 2: Updating with threads:

private void btnStartThread_Click(object sender, EventArgs e)
{
    System.Threading.ThreadStart threadDelegate = new System.Threading.ThreadStart(ScanEvents);
    System.Threading.Thread newThread = new System.Threading.Thread(threadDelegate);
    newThread.Start();
}

private void ScanEvents()
{
    lblMessage.Text = "Exected in another thread.";
}

This will fail because I'm MODIFYING the controls on my form from another thread:

enter image description here

Now, I will modify the code so that I'm changing the label with an action through an invoke on the label.

private void ScanEvents()
{
    if (lblMessage.InvokeRequired)
    {
        lblMessage.Invoke((Action)(() => lblMessage.Text = "This text was placed from within a thread."));
    }
}

This will make the text change.

enter image description here

So, I hope that it helps. If not, please shout :-)

Community
  • 1
  • 1
Complexity
  • 5,682
  • 6
  • 41
  • 84
  • If you believe this is a duplicate you should flag it as such rather than simply linking to an existing question. – Daniel Kelley Jul 07 '14 at 13:02
  • Done. Sorry but I didn't knew it. – Complexity Jul 07 '14 at 13:04
  • @Complexity You can access UI elements from a background thread as long as you don't attempt to modify any of their properties. READ ONLY is allowed. – Darek Jul 07 '14 at 13:06
  • Thanks for pointing this out, but looking at the original question, it's not clear if one of the properties is modified. – Complexity Jul 07 '14 at 13:08
  • @Complexity can you tell me how to use InvokeRequired in my application.Since the code what i given in question is for creating xml attributes and i added the InvokeRequired above the 4 line in question in ScanEvents method.Still i am getting the same exception what you mentioned in answer.Do i want to add the InvokeRequired to the place where i am passing the input to textbox control.Please clarify – Royal Jul 08 '14 at 09:13
  • Can you update your question with what you have right now, that will make things easier. Also place the code where you're padding the input to the textbox control. Then I'll probably be able to help you out. – Complexity Jul 08 '14 at 09:26
  • @Complexity I edited the question with updates.Please help me to do theading. – Royal Jul 08 '14 at 11:22
  • And where do you have the error because I don't see it? – Complexity Jul 08 '14 at 11:23
  • I am not able to take screen shot of error,but i updated the error and line in question.Please see Errors and In line in question. – Royal Jul 08 '14 at 11:40
  • Let's move to the chat, so avoid a very long post. – Complexity Jul 08 '14 at 11:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56947/discussion-between-complexity-and-royal). – Complexity Jul 08 '14 at 11:42
  • The value of scanIdValueLabel is not changed until a submit is completed.Without using thread i debugged and noticed that a 6 digit number(ranchid) is passing to that.What could be the cause on using thread. – Royal Jul 08 '14 at 13:07
  • Well, you're saying that the value is changed when a submit is completed. From which thread is the submit called? If it's not from the UI thread, you will need InvokeRequired, try adding an InvokeRequired for changing the TextAlign property. – Complexity Jul 08 '14 at 13:09