Friday, April 24, 2009

Cross-thread operation not valid: Control 'Ctrl' accessed from a thread other than the thread it was created on

I though I would have to redesign part of the application, after all, this seems like a critical error. But if you come across this error, check out this link:

http://www.vcskicks.com/cross-thread.php

Delegates can fix it. The code below will help you (part of this code is from the website above).

 
private delegate void AddListBoxItemDelegate(object item);
... then copy this code inside your method...
if (this.InvokeRequired)
{
AddListBoxItemDelegateaddItemDel = new AddListBoxItemDelegate
(AddListBoxItem); //delegate
object[] parameters = { text }; //parameters
this.Invoke(addItemDel, parameters); //call
}

else

{
//code you want to execute
}

Saturday, January 3, 2009

Disabling controls during a postback

Have you ever wondered how to disable the controls in the UI when doing a postback? This is a way to give feedback to the user saying: "wait for the page to load/respond". There are other ways to give this kind of feedback, for example, by using a busy indicator. The article "Managing the User Experience in AJAX" shows how to handle the first situation:

msdn.microsoft.com/en-us/magazine/cc163319.aspx

Thursday, September 18, 2008

Google Maps API and Microsoft Virtual Earth SDK

After thinking about what would be worth to learn, I decided to choose the Google Maps API and Microsoft Virtual Earth SDK. I just got started, you can find my "Hello, World" example here. I will keep on posting examples (more interesting than this one, I promess!)

Tuesday, September 9, 2008

Algorithm to buffer a polygon

I came across the need to buffer a polygon. Of course, ESRI provides tool in ArcMap to achieve this, but I needed an algorithm to use in an application rather than a tool. I could not find the algorithm, but I found the code to achieve this. Still, it should be fairly easy to figure out the algorithm by reading the code. You can find by clicking here. It's in VB, so I had to convert it to C++.

In case the link is broken, I am including the source code:

Private Type XY
p_X As Double
p_Y As Double
End Type
Dim MyPoly() As XY
Dim MyPoly2() As XY
Dim MyMidLines() As XY
Dim MyLine() As XY

Private Sub OUTLINE_IT()
'MyPoly is a matrix from 0 to N, containing the X and Y values of the
'points of the original polygon
'MyPoly2 is the Output Poly

Dim I As Integer
Dim PI As Integer
Dim NI As Integer '
Dim p_1 As XY
Dim p_2 As XY
Dim t1 As Double
Dim t2 As Double
Dim MidP As XY
Dim MySign As Integer

ReDim MyMidLines(UBound(MyPoly))
ReDim MyPoly2(UBound(MyPoly))
ReDim MyLine(UBound(MyPoly))
Dim MyD As Long
MyD = Val(txtSize.Text)
MySign = Sgn(MyD)
Picture1.Cls
If MySign <> 0 Then
For I = 0 To UBound(MyPoly)
PI = ((I - 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
NI = ((I + 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
Picture1.Line (MyPoly(I).p_X, MyPoly(I).p_Y)-(MyPoly(NI).p_X, MyPoly(NI).p_Y), vbBlack
MyLine(I).p_X = MyPoly(NI).p_X - MyPoly(I).p_X
MyLine(I).p_Y = MyPoly(NI).p_Y - MyPoly(I).p_Y
t1 = Sqr(((MyPoly(PI).p_X - MyPoly(I).p_X) ^ 2 + (MyPoly(PI).p_Y - MyPoly(I).p_Y) ^ 2) / _
((MyPoly(NI).p_X - MyPoly(I).p_X) ^ 2 + (MyPoly(NI).p_Y - MyPoly(I).p_Y) ^ 2))
MidP.p_X = (MyPoly(PI).p_X + (MyPoly(NI).p_X - MyPoly(I).p_X) * t1 + MyPoly(I).p_X) / 2
MidP.p_Y = (MyPoly(PI).p_Y + (MyPoly(NI).p_Y - MyPoly(I).p_Y) * t1 + MyPoly(I).p_Y) / 2

MyMidLines(I).p_X = (MidP.p_X - MyPoly(I).p_X)
MyMidLines(I).p_Y = (MidP.p_Y - MyPoly(I).p_Y)

Next I

t1 = MySign * Sqr((MyD ^ 2) / (MyMidLines(0).p_X ^ 2 + MyMidLines(0).p_Y ^ 2))

MyPoly2(0).p_X = t1 * MyMidLines(0).p_X + MyPoly(0).p_X
MyPoly2(0).p_Y = t1 * MyMidLines(0).p_Y + MyPoly(0).p_Y

For I = 0 To UBound(MyPoly)
PI = ((I - 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
NI = ((I + 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
t2 = (MyLine(I).p_Y * MyPoly2(I).p_X - MyLine(I).p_X * MyPoly2(I).p_Y + MyLine(I).p_X * MyPoly(NI).p_Y - _
MyLine(I).p_Y * MyPoly(NI).p_X) / (MyLine(I).p_Y * MyMidLines(NI).p_X - MyLine(I).p_X * MyMidLines(NI).p_Y)
MyPoly2(NI).p_X = MyMidLines(NI).p_X * t2 + MyPoly(NI).p_X
MyPoly2(NI).p_Y = MyMidLines(NI).p_Y * t2 + MyPoly(NI).p_Y
Picture1.Line (MyPoly2(I).p_X, MyPoly2(I).p_Y)-(MyPoly2(NI).p_X, MyPoly2(NI).p_Y), vbBlack
Next I
End If

End Sub

Wednesday, August 27, 2008

Error showing "Run-Time Check Failure #0"

After compiling the source code of OSG 2.6.0 and MANUALLY copying the binaries to another directory, I got the error message:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

This error pops up when I run an OSG application in VS2005 in Debug mode. This error looks a little scary, but after looking for information about this error, I found http://www.gamedev.net/community/forums/topic.asp?topic_id=308819 which pointed me in the right direction. It was because I copied the dll's and include directory (*dll, *.h), but not the library files (*.lib) to a new directory. I guess that is why there is an INSTALL project in the OSG solution that copies the files automatically :)

Friday, June 27, 2008

Bug, Error or Defect?

A few days ago I came across this article:
www.sei.cmu.edu/news-at-sei/columns/watts_new/1999/March/watts-mar99.htm

It got me thinking... I usually use the word "bug" to referr to any unexpected behavior in the software. However, after reading this article, I started to say "defect" instead of "bug".

It also made me remember a Software Quality Assurance course I took during my Master's degree. In the course book the differences between defect, error, fault and some other words I don't really remember was explained. Back then -and still- I thought that there was no sense in using different words for different situations. In my opinion, problems during compilation, testing, deployment... all can be called defects. Using a word for each situation makes it confusing.

Finally, it is interesting how the name of the software changes our habits... for example, I used to say "bug" because I am used to using BugNET as a defect tracking system. Or when I say "googled" a word or "photoshop" an image.

Wednesday, June 18, 2008

VS2005 and wchar_t

While upgrading a VS2003 solution to VS2005, I received the following error:

Error 29 error LNK2028: unresolved token (0A001D22) "public: void __thiscall osgText::TextBase::setText(unsigned short const *)" (?setText@TextBase@osgText@@$$FQAEXPBG@Z) referenced in function "private: class osg::Node * __thiscall XXX::YY::ZZZ::FFF::WWWW(class XXX::YY::ZZZ::FFF::CCC &,class osg::Vec4f const &)" (?KKKKK@TTTTT@CCCCC@YY@XXX@@$$FAAEPAVNode@osg@@AAVGGGGG@234@ABVVec4f@6@@Z) RRRRRR.obj

My first thought was to make sure I had the correct version of the OSG libraries. They were OK, so then I googled some keywords of the error message and came across something interesting. VS has an option in Tools->Options that is "Treat wchar_t as built in type". My default value was false, so I set it to "Yes" and rebuilt the libraries. It worked!