1

我们有一段以前运行良好的代码,现在它卡住了,问题似乎是运行时更新或类似的东西?

我们启动一个显示表单对话框并获取值的子线程,该值由静态类中的静态变量返回到主线程,在该语句中,线程卡住了,Visual Studio 没有说明正在发生的事情。 ..是否有另一种返回值的方法(在某些情况下有多个字符串要返回)

我们之所以需要这样做,是因为我们的软件如何执行附加程序以供工作。

示例代码:

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 */
   }
}

最后我在这个问题中使用这个回答解决了它。有什么方法可以给予该用户的信任吗?如果不是,我会将anwser或upvotes给可以解释为什么子线程被卡住访问静态变量的人......

4

0 回答 0