views:

34

answers:

1

Hi,

i have to do some message exchange with a 3rd party (in a website). When the client posts a page, i start the message exchange. When that doesn't succeed for some reason, i report this to the client by rendering the page with a message. On the background, in a separate thread, i start a process to send abort messages to the 3rd party. I can't do this while the user is waiting for the page to come back, because it might take a few minutes.

But in a test project, the test ends when the message to the 3rd party is sent, and after the new thread is started. But it seems that the new thread also ends, when the test is done.

Is that normal behaviour?

I do start the thread in a new class with a reference to 2 objects from the class which tries to send the message in the first place, may that be a problem?

EDIT: it keeps running when the whole process is started in IIS

+1  A: 

I guess the mstest executable shuts itself down when all tests have finished. Try, in your unit test, to wait for the end of the abort conversation.

Timores
it's in a separate thread, started in a object somewhere, can i wait for that?
Michel
If you don't have the thread instance, you can use an Event that is signaled by the thread and waited on by the unit test.
Timores
Can you give me a code snipet, because i'm not sure where to start?I have no direct reference to the object which started the new thread from my test project: i start a service which starts a connector which starts a thread.
Michel
Use: var evt = new System.Threading.EventWaitHandle(false, EventResetMode.AutoReset, "A unique name") on both unit test and the thread that starts the abort message sending process.Then, evt.WaitOne() in the test and evt.Set() in the thread.
Timores
doesn't that interfere with the normal, non-test behaviour? When i run the code from the normal flow, i have only one 'var evt....' and a evt.Set() in the abortservice. I'm not realy familiar with the eventwaithandle, so i was wondering if this situation (the set without the waitone) could somehow cause an exception?
Michel
By the way, it works like a charm!
Michel
No, creating an event and setting it, without anyone waiting for it will not cause an exception. But, of course, it's too bad to do this for production code, where it's not needed. Maybe you could wrap this in #if DEBUG.
Timores