1
votes

I've developed a webpart that presents a strange behaviour. When I load the page where is installed sometimes it works correctly and sometimes it crashes, but I can't find any pattern (seems random to me).

When I debugged it I saw that have two threads of execution. (The cursor of debug always passes two times over every function in separated threads). How can it be?

I want that only be one thread, and I also suspect that this could be the source of the random crash. Any idea where start to find this error?

EDIT:

Added the two screenshots with the debug

Added two screenshots with the debug

enter image description here

1
Debug cursor going through a function twice does not mean there are two threads of execution. Can you explain a bit more on what you mean by two threads of execution? Are you loading the webpart twice on the same page? Are you wiring up the event handlers twice?Ken
I've added two screenshots of the debug, showing the two threads (I think there are). The webpart is added once in one page (I created from zero a new clean page, and after added the webpart. How more could be?Dr. No

1 Answers

-1
votes

That is the problem with threads and shared variables, if you don't synchronize you never know when things crash. First off you have to find why you get more threads accessing your code, If you can not "remove" the behavior you will have to synchronize the access to all the variables that are shared in you object using locks or some other synchronizing mechanisms. (Be relay careful that you do not get any deadlocks doing that).

The point here is. If you are not able to make the code run in a single tread, you have to synchronize the access to the variables in the object so things will not crash.