1

We have a piece of code that previously worked fine and now it gets stuck, the problem seems to be runtime updates or something like it maybe?

We launch a subthread that shows a Form Dialog and gets a value, this value is returned to the main thread by a static variable in an static class and in that statement the thread gets stuck and visual studio doesn't say anything about what's happening... is there another way to return the value (in some cases there are more than one string to return)

We need to do this way specifically because of how the software we do the add-on programs for work.

Example code:

public static Cons
{
   public static string inputvalue;
}
public static Funs
{
   public static string GetValueString()
   { 
       Thread threadx = new Thread(GetValueStringx);

       threadx.SetApartmentState(ApartmentState.STA);
       threadx.Start();

       if (threadx.Join(new TimeSpan(0, 3, 0)))
       {
            return ComprobarLicencia(Cons.inputvalue);
       }

       /*because the subthread is stuck the wait time (3mins) is always 
         reached and the code continues here, not getting the input value :/    */
       try
       {
           threadx.Abort();
       }
       catch
       {
       }
       return "";
   }
   public static string GetValueStringx()
   {
       WindowWrapper window = new WindowWrapper(Fun.GetForegroundWindow());
       FormInput formlicencia = new FormLicencia();
       formlicencia.ShowDialog(window);
       Cons.inputvalue = formlicencia.inputvalue; //thread gets stuck here
       /*even if i comment all the above lines and i put
         directly Cons.inputvalue="valx"; it gets stuck too :s */
   }
}

In the end I solved it using this response in this question. Is there some way to give credit to that user? If not i would give the anwser or upvotes to someone that can explain why the subthread gets stuck accesing the static variable...

Community
  • 1
  • 1
VSP
  • 2,367
  • 8
  • 38
  • 59
  • Why are you launching a form on another thread? Also, is this WinForms, WPF etc? – MoonKnight Nov 30 '12 at 09:57
  • @Killercam Becouse in this case the application is an Addon for SAP B1, it uses its own UI API Forms but in some cases (like this one) we need to show a WinForm and the only way to be sure the form is shown in the foreground and as a modal dialog is this one. But remember that our problem in this case is the thread being stuck when setting the static variable value to anything Cons.inputvalue="valx"; ;) – VSP Nov 30 '12 at 10:32
  • Are you sure that it is getting stuck on the value accessor? When you step through in the debugger are you saying no exception gets thrown? Note, that if this is you _actual_ code structure any unhandled exception will not be caught and could cause a hang... – MoonKnight Nov 30 '12 at 10:39
  • I don't know how you compile this, there are numerous errors like `public static string GetValueStringx()`, it should be `public static void GetValueStringx()`. Also you don't have class specifiers for your static classes. – Nikola Davidovic Nov 30 '12 at 11:20
  • @NikolaD-Nick Sorry it is striped down code to give some context but the problem is a generic one that could be reproduced with some simple lines of code... – VSP Nov 30 '12 at 13:23
  • @killercam it doesnt throw error the debugger sits at that line Cons.inputvalue, if i wrap it around try catch it doesnt catch any errors as its stuck at the line on code, i click continue -> wait for x secs/mins -> click break all -> debug point stays at that line... – VSP Nov 30 '12 at 13:24
  • 1
    Well I tried your code it's working perfectly. I couldn't reproduce the problem. – Nikola Davidovic Nov 30 '12 at 13:26
  • 1
    I'm guessing you shrunk the example enough to not repro the problem. I wonder two things though: what is in the `Cons.inputValue` setter, and what happens if you don't set the apartment state? – tallseth Nov 30 '12 at 13:49
  • @tallseth Happens the same setting or not the apartment state, inputValue has no setter is a direct static variable. Maybe the context its too big to exemplify it here but with the published exe the program hangs at the same point so the problem is not IDE related – VSP Dec 03 '12 at 08:02

0 Answers0