CameTooFar

A Nerd's False Positive.

Cross-Thread operation not valid in WinForm


One problem while working with Threads in C# is that a background thread cannot ‘talk’ to the any visual elements that exists in the main GUI thread, because talking to objects across thread boundaries is forbidden. The reasons are obvious; because a thread has its own process space, the address of an object in one thread is not useful as an address in another thread. As a result, trying to access that object in another space results in a program crash with an unhandled exception. However, C# provides a mechanism for working with cross-thread process issue.

The BeginInvoke method of the MethodInvoker delegate is used within a thread prcoess to call another process method. These methods are thread-safe and can be used in a multi-threaded environment.

Last night, when a Thread tried to access the Text property of a Label control. I got the following error:

Cross-Thread operation not valid: Control ‘label3’ accessed from a thread other than the thread that it was created on.

Add the below quoted codes to perform a cross-thread operation.

   1: if (label3.InvokeRequired)
   2: {
   3:     label3.Invoke(new MethodInvoker(delegate{label3.Text = "sample text";});
   4: }


Hope this helped.

Thanks Winking smile

Ajax Error - Question marks on sending Foreign Language


Yup! So that’s the problem when you switch to Foreign Languages! This post is related to my previous post – A Forgotten Story – Ajax in ASP.NET using JavaScript.

Last day, I’s trying to send Chinese characters over the wire through Ajax. Surprisingly, I received every Chinese characters at the server-side as questions marks.

A mock-up looks like:

encoding

After spending few hours with the academic basics, the problem was pin-pointed.

The request sending from the client-side was not encoded, which was the reason for this weird behavior. I slightly modified the Ajax code to encode the value from the TextBox and the problem was solved!

You can use either of the below JavaScript function to achieve it:

  1. encodeURI

    This function is used to encode URI. It encodes special characters (except / , ? : @ & = + $ #)
  2. encodeURIComponent

    This function encodes URI component, including the characters excluded by encodeURI

And, a modified code-segment looks like

decode

Hope there is no question-marks in your Request.

Happy Encoding!

Silverlight Error - The type or namespace name 'Label' does not exist in the namespace 'System.Windows.Controls' (are you missing an assembly reference?)


Recently, I’s adding an existing Silverlight Project to a newly created Silverlight project. Of course, the xaml contains controls in its Design, including Label control.

Soon after adding the xaml’s, I hit F5, boom! – Compiler time error:

“The type or namespace name 'Label' does not exist in the namespace 'System.Windows.Controls' (are you missing an assembly reference?)”

What? My new Silverlight project doesn’t have a Label control?

Well, my newly created project was missing an assembly reference & can fix it by adding a reference of the assembly:

System.Windows.Controls.Data.Input.dll

Yup! It’s fixed.