0

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 :)

Mayb2Moro
  • 3
  • 1
  • 5
  • Have you tried a global `try ... catch(Exception ex)` around your `while` loop? Does your code ("do stuff") contains a `return` statement somewhere? (first things that come to my mind) – ppetrov May 16 '13 at 10:12
  • IF you are able to reproduce the error you could add logging to see what is going on. Did you check the eventlog? – Emond May 16 '13 at 12:44
  • Erno - I must admit - I haven't checked the eventlog. I have added logging in the application though around all areas of the code and this has enlightened me to nothing. ppetrov - I have added try-catches within the while loop (including encapsulating all of the code in the while loop), but not outside and around the while loop itself. I can try that, but it doesn't feel like that is going to catch anything I haven't already caught :-S – Mayb2Moro May 16 '13 at 13:41

1 Answers1

0

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.

Adam McCrory
  • 391
  • 3
  • 7
  • [STAThread] is already in the Main function - however - is it worth doing this before I start up the thread - do you think it would help: syncThread.SetApartmentState(ApartmentState.STA); – Mayb2Moro May 16 '13 at 17:53
  • Weird thing is that it mostly works.. this threading stuff goes over my head a little bit to be honest, but I can understand that if the threading model is wrong then it might do some weird things (which it is!) – Mayb2Moro May 16 '13 at 17:54