stuff i do at work

(maybe i’ll make less mistakes if i remember some)

Archive for July, 2008

while(currentThread == backgroundThread)

Posted by noah on July 17, 2008

Since Thread.Abort is so bad, the normal alternative is to do:

volatile bool runBackgroundThread;

then

while(runBackgroundThread) {…}

In my code, there is a start and stop function which would set runBackgroundThread to different values, then create a new thread in the start function.

I was worried about the scenario where the user stops and starts really quickly, which would mean that I would have to wait until the 1st thread dies before starting the 2nd.  Then I have to consider what happens if the background thread takes too long on it’s last iteration, and then use some sort of blocking.  Otherwise it’s possible to have 2 background threads running… one being a zombie thread.

So what I did was:

public static void Heartbeat()
{
Thread currentThread = heartbeatThread;
while (currentThread == heartbeatThread)
{ … }
}

Where heartbeatThread is defined in the class.  I think this should work fine except for the odd case when the new thread is assigned to the same pointer as the old thread.  Is that possible?

I think I have to think about this more.

edit: I think a better way is to actually pass the thread pointer in to the function which will run in the thread instead. This avoids the possiblity of the thread being assigned to between initialization and the thread running and acquiring the pointer to its own thread.

example:

backgroundThread = new Thread(Heartbeat);
backgroundThread .IsBackground = true;
backgroundThread .Start(backgroundThread );

public static void Heartbeat(Object currentThread)
{
while ((Thread)currentThread == heartbeatThread)
{ … }
}

edit 2: use Thread.GetHashCode() instead

Posted in Uncategorized | Leave a Comment »

Run any function as an Action delegate

Posted by noah on July 17, 2008

It was really neat when I found out about the the Action built-in delegate.  Instead of defining the same delegates all over the place, I can use the Action delegate as long as I don’t have any return values.

Now what do you do if you want to pass a function as a parameter into another function that takes an Action delegate?

Just anonymize it by wrapping the function in an anonymous function.

FunctionAcceptingActionDelegate(delegate { functionWhichReturnsABool(); });

Posted in Uncategorized | Leave a Comment »

InvalidCastException when dynamically creating an object instance

Posted by noah on July 10, 2008

This one was driving me crazy.  Using the factory pattern, I created an instance of a class, which implemented a certain interface.  But when I tried to cast the object into the interface it used, I got an invalidCastException.  Here’s the line of code:

IClassType t = (IClassType)Activator.CreateInstance(ClassType);  // Where ClassType implements IClassType

What happened was ClassType was referencing IClassType in a different copy of an assembly than the one the current assembly was referencing.  All I had to do was change the assembly reference property to copyLocal=false.

The post that figured this out for me: http://www.yoda.arachsys.com/csharp/plugin.html

Posted in Uncategorized | Leave a Comment »