简体   繁体   中英

Forked thread in C# .net App dies inexplicably and without throwing any errors

Firstly, I am sorry if this is quite a vague question, it is quite difficult to explain and quite bizarre and it also does not seem like a particularly specific problem. but I will do my best to explain here:

I have a C# .net application which forks a new thread to do most of it's work - with the primary thread just handling a GUI front end.

syncThread = new Thread(new ThreadStart(syncClass.sync));

The sync thread loops continuously...

public void sync()
    {

        while (true)
        {
           do stuff

And at the end, has a sleep to stop it hogging CPU all of the time...

System.Threading.Thread.Sleep(sync_interval);

To fill in some background, what it does in "do stuff" is connect to Sage using Sages API, and connect to a SOAP service and basically pull data from Sage and push to the SOAP. This works fine HOWEVER...

What appears to be happening is that at random intervals the thread just stops executing - as though it has just "gone away". I have logged to screen and to a file to try and find a particular cause for this but it stops at different parts of it's process almost every time.

I have tried catching errors and logging these to screen and to file as well as logging code execution progress to file - but when this happens - it simply does not throw an error (or if it does, I am not able to catch it). So I am completely stuck.

To add to the bizarreness - this does not seem to happen on every system it is installed on but does happen on more than just the odd one.

Without any caught errors to fix, and no set area of code to investigate for the problem (because as I say - it is not consistent) I am completely stumped as to how to proceed.

So - are there any general threading issues that anyone is aware of that could cause this behaviour? Is there a better way for me to go about the threading in the first place, or can anyone offer any further advice on how to find and trap the error so that I can fix it?

Please help :)

If you are talking to Sage 50 via the SDO it's a COM interface and it runs in a Single Threaded Apartment (STA), I am 99% sure that your app too will have to run in STA mode - our app runs as as a STA and if I recall we only thread the UI elements.

Try sticking [STAThread] in your Main() function and see if that helps - Let me know how you get on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM