线程中断和ActionListener Java(Thread interruption and ActionListener Java)
我有一个函数graphics()创建我的JFrame和两个JRadioButtons并向它们添加ActionListeners。 这个图形从main()调用,图形本身调用game()。
public void game() throws Exception { jTextArea1.setLineWrap(true); jTextArea1.setWrapStyleWord(true); jTextArea1.setText("This is private information."); jRadioButton1.setVisible(true); jRadioButton2.setVisible(true); try { t.sleep(40000); repaint(); } catch (InterruptedException e) { // We've been interrupted: no more messages. return; }
显示“这是私人信息”后。 在文本区域中,我希望程序执行暂停40秒,或者直到用户按下JRadioButton,以较早者为准。 所以我添加了一个ActionListener并在其中调用了t.interrupt()。
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) { t.interrupt(); jRadioButton1.setVisible(false); jRadioButton2.setVisible(false); //System.out.println(t.interrupted()); jTextArea1.setText("Please wait..."); }
但是,即使选择了应该触发中断的JRadioButton,也不会发生这种情况,并且t.interrupted返回false。
任何帮助,将不胜感激。
I have a function graphics() that creates my JFrame and two JRadioButtons and adds ActionListeners to them. This graphics is called from main() and graphics itself calls game().
public void game() throws Exception { jTextArea1.setLineWrap(true); jTextArea1.setWrapStyleWord(true); jTextArea1.setText("This is private information."); jRadioButton1.setVisible(true); jRadioButton2.setVisible(true); try { t.sleep(40000); repaint(); } catch (InterruptedException e) { // We've been interrupted: no more messages. return; }
After displaying "This is private information." in the text Area, I want the program execution to pause for 40 seconds, or until the user presses the JRadioButton, whichever is earlier. So I added an ActionListener and called t.interrupt() inside it.
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) { t.interrupt(); jRadioButton1.setVisible(false); jRadioButton2.setVisible(false); //System.out.println(t.interrupted()); jTextArea1.setText("Please wait..."); }
However, even after choosing the JRadioButton which should trigger the interrupt, that does not happen and t.interrupted returns false.
Any help would be appreciated.
原文:https://stackoverflow.com/questions/13061054
最满意答案
由于您无法在新项目中重现它,因此在此特定项目中必定会有不同的内容,这会导致您正在描述的问题。
您可以尝试的几件事(没有特别的顺序):
检查您是否在两个项目中使用完全相同的库版本(有效的版本和不可用的版本):软件包版本和目标平台(net4,net35 ...)。
删除非工作项目的
packages
文件夹,以便强制NuGet重新下载所有软件包。从“参考”节点选择“RestSharp”时,在“Visual Studio属性”窗口中查找引用库的确切路径。 在工作项目和非工作项目引用的库之间进行二进制比较。
遗憾的是,SymbolSource上没有RestSharp的符号包,因此您无法在非工作项目中直接调试RestSharp。 如果您拥有许可证或之前未使用过试用版,则可以使用Reflector.NET VSPro 。
将非工作项目的部分移动到工作项目,直到它停止工作。
编辑:
查看
RestRequest
的源代码 ,AddObject
似乎没有使用您正在设置的JsonSerializer
。 您是否尝试过使用AddBody
?Since you can't reproduce it in a new project, there must be something different going on in this particular project, that's causing the issues, you're describing.
A couple of things you could try (in no particular order):
Check that you're using the exact same version of the library in both projects (the one that works and the one that doesn't): package version and target platform (net4, net35...).
Delete the
packages
folder of your non-working project so that NuGet will be forced to re-download all the packages.Lookup the exact path to the referenced library in Visual Studio Properties window when you have RestSharp from References node selected. Do a binary compare between the libraries referenced by the working and the non-working project.
Unfortunately there's no symbol package for RestSharp on SymbolSource, so you can't directly debug RestSharp in your non-working project. You could use Reflector.NET VSPro if you have the license or haven't used the trial before.
Move parts of your non-working project to the working one until it stops working.
EDIT:
Looking at the source code of
RestRequest
,AddObject
doesn't seem to use theJsonSerializer
you are setting. Have you tried usingAddBody
instead?
相关问答
更多-
您是否有机会在另一个线程下调用此代码? 也许是背景工作者? 如果是这样,那么可能就是不抛出异常的原因。 只需将所有代码包装在Try-Catch块中,您就能捕获所有异常。 更新: 我认为引发异常是因为你将一个数组放在没有属性名的JSON对象中,如果这样做,通过插入这一行 writer.WritePropertyName("arrayKey"); 在第89行之前,前一个异常将消失,但是你会在循环中得到一个新异常,这个新异常是由于键值对必须在一个对象{}内引起的,所以你不能将它们直接放在一个阵列。 无论如何,我 ...
-
RestSharp - 如何影响JSON序列化(命名)?(RestSharp - how to influence JSON serialization (naming)? [duplicate])[2021-12-22]
你不能或你应该将Json.NET添加到RestSharp。 在RestSharp的github repo上存在这个问题。 You cant or you should add Json.NET to RestSharp. There is a issue about this on the github repo of RestSharp. -
在上面的模型中,another_class是一个字符串字段。 我正在寻找一种方法,让我可以使用AnotherClass的对象分配another_class变量。 它会自动调用序列化方法。 但是,我已经找到了实现相同目标的方法,尽管我们自己调用这些方法。 因此,在AnotherClass类定义中,我们需要编写两个方法: to_json(*a)和self.json_create(o)如下所述: http : //www.skorks.com/2010/04/serializing-and-deserializi ...
-
你可以试试这个: AnnotationIntrospector annotationInspector = new JacksonAnnotationIntrospector(); AnnotatedClass annotatedClass = AnnotatedClass.constructWithoutSuperTypes(OID.class, new ObjectMapper().getSerializationConfig()); List
su ... -
我想,现在这是不可能的。 但是您可以为每个属性创建两个单独的序列化程序。 我知道,这是一个小解决方法,但它应该工作。 I think, this is not possible now. But you can create two separate serializers for each property. I know, this a little workaround, but it should work.
-
使用自定义序列化程序问题扩展RestSharp序列化(Extending the RestSharp serialization with a custom serializer issue)[2022-04-26]
由于您无法在新项目中重现它,因此在此特定项目中必定会有不同的内容,这会导致您正在描述的问题。 您可以尝试的几件事(没有特别的顺序): 检查您是否在两个项目中使用完全相同的库版本(有效的版本和不可用的版本):软件包版本和目标平台(net4,net35 ...)。 删除非工作项目的packages文件夹,以便强制NuGet重新下载所有软件包。 从“参考”节点选择“RestSharp”时,在“Visual Studio属性”窗口中查找引用库的确切路径。 在工作项目和非工作项目引用的库之间进行二进制比较。 遗憾的是 ... -
这个 ["student":{"name":"sam","rollNumer":1},"student":{"name":"tom","rollNumer":2}] 无效JSON(您可以使用jsonlint等在线工具自行验证 )。 查看JSON规范的详细信息: 对象的定义 : 一个对象是一组无名的名称/值对。 一个对象以{(左大括号)开始并以}(右大括号)结束。 每个名称后跟:(冒号),名称/值对由(逗号)分隔。 数组的定义 : 数组是值的有序集合。 数组以[(左括号)开始,以[]结尾(右括号)。 值由( ...
-
使用Jackson的@JsonTypeInfo和自定义序列化程序(Using Jackson's @JsonTypeInfo with a custom serializer)[2023-03-13]
我找到了解决办法,或者这可能是合适的解决方案。 无论哪种方式,它似乎都有效。 如果有更好的方法,请告诉我。 (我觉得应该有) 我定义了一个实现Map -
我设法通过这种方式注册IHttpRequest使其工作: container.Register(c => HttpContext.Current.ToRequestContext ().Get
()).ReusedWithin(Funq.ReuseScope.None); 现在,当我尝试解决它们时,我总是得到IHttpRequest对象。 此外,在我的应用程序中进行了更多的测试之后,我能够检测到,如果并发性足够高,依赖于使用ReuseScope.Request注册 ... -
我可以将自定义JsonSerializer设置为RestSharp RestClient(Can I set a custom JsonSerializer to RestSharp RestClient)[2022-07-31]
看来,自述文件中有一个错误。 https://github.com/restsharp/RestSharp/issues/886 https://github.com/restsharp/RestSharp/issues/947 这是它应该如何使用: 有一个重大变化:默认的Json Serializer不再与Json.NET兼容。 要使用Json.NET进行序列化,请从https://github.com/restsharp/RestSharp/blob/86b31f9adf049d7fb821de8279 ...