以前は問題なく動作していたコードが動かなくなってしまいました。問題はランタイムの更新か何かのようなものでしょうか?
フォーム ダイアログを表示して値を取得するサブスレッドを起動します。この値は、静的クラスの静的変数によってメイン スレッドに返されます。そのステートメントでは、スレッドがスタックし、ビジュアル スタジオは何が起こっているかについて何も言いません。 ..値を返す別の方法はありますか(場合によっては、複数の文字列が返されます)
このようにする必要があるのは、特にソフトウェアがアドオン プログラムを機能させる方法のためです。
コード例:
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 */
}
}
最後に、この質問のこの回答を使用して解決しました。そのユーザーに信用を与える方法はありますか? そうでない場合は、サブスレッドが静的変数へのアクセスでスタックする理由を説明できる人に、アンサーまたは賛成票を投じます...