Click here to Skip to main content
15,314,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All!

I got weird problem which might be threading related. I got 3. party
component which feeds my singleton dataclass properties. My dataclass offers public propeties and some public methods to send messages into 3. party component. In the end I use instance of my dataclass in multiple windows as showing those properties or sending messages to component.

Problem comes when quit my program and dispose my dataclass. At the disposing I have to call 3. party component to stop its thread channel.Stopthread(). which
SOMETIMES throws indexoutof range exception. But when I but"just
click ok") just before channel.Stopthread that error never happens (what is that trick!?).

I did also simple wpf app where I use directly 3. party component and I was able to spam channel.StartThread / channel.StopThread as long I wanted without errors..

Also odd thing is that when I but channel.Stopthread inside try/catch
statement it never catches that error.

UPDATE: Here is some code at uninitialize

private void Uninitialize()
//UnSubcribe from message events which feeds my properties
channel.MessageReceived -= new EventHandler<MessageEventArgs>(MessageReceived);


public void StopThread()
if (this.thread != null)
   this.threadStop = true;


internal void SetEvent(params Object[] args)
           lock (

And that is pretty much I'm able dig in about 3. party component. As you might noticed this is empedded device related application and I cannot debug this on development computer. My tracelog tells that programs crashes at channel.StopThread call.

I know that this is trivial to you also but any advice is would be great because im totally novice what comes to threading.

Hope to hear your thougths :)

Updated 3-Jul-11 22:59pm
It can easily happen, but you need to create some code sample demonstrating the problem. For goodness sake, don't post all your real code.
Also, you can easily set a break point in your code and find out where the problem is. Alternatively, provide exception log. I'm sure the problem is easy to fix.
paleGegg0 4-Jul-11 4:59am
Updated :)

1 solution

IndexOutOfRangeException is not one that should directly be caused by stopping a thread. (If something got disposed unexpectedly because of the shutdown you might get ObjectDisposed or NullReference exceptions.) Do you have the stack trace for the actual exception? (If not, put more logging into the channel class.) This should be something you can find by normal debugging techniques.

A try/catch will not catch exceptions which are thrown in a different thread, so that proves that the exception is in the channel thread not the main thread – you probably guessed that already. As you are posting an event to the event queue I would look at your dequeue code.
paleGegg0 4-Jul-11 8:25am
Thanks a lot to directing me to solution, there was a bug in channel thread (handling queue/dequeue) just like you expected! Took me 9 hours to find that one..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900