如何在不退出vb.net中的功能的情况下从功能发送状态消息 [英] How to send status messages from a function without exiting such function in vb.net

查看:96
本文介绍了如何在不退出vb.net中的功能的情况下从功能发送状态消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我目前正在尝试编写一个可以解决魔方的应用程序.我写了各种类,其中有类Cube.其中有一个函数(名称为IsValid),用于检查用户绘制的多维数据集是否有效并且可以解决.我在此函数中有很多阶段可以分析多维数据集的颜色,块,方向和其他类似内容,并且我希望该函数在不退出该函数的情况下发送某种类型的消息.如果多维数据集有问题,该函数将引发一个异常,告诉您多维数据集到底有什么问题,但是我希望能够在该多维数据集之前发送许多消息,例如:正在检查正确的颜色..." 已检查"正在检查正确的工件方向..."这样的角落重复了两种颜色",这样我就可以在过程中进行某种形式的登录了.关于如何的任何想法?我没有显示任何代码,因为到目前为止没有任何错误,直到现在,我只是不知道该怎么做.

P.S.当我编写Throw new时,在系统异常下似乎有一个名为SystemInformation的对象.我尝试进行研究,但我想它只是用来处理操作系统信息.

Hi,
I''m currently trying to write an application that will solve the rubik''s cube. I have written various classes amongst which I have the class Cube. In it there is a function (by the name IsValid) to check whether the cube the user drew is valid and can be solved. I have many stages in this function to analyze the cube''s colors, pieces, orientation and other stuff like that and I would like the function to send some type of message WITHOUT exiting the function. If something''s wrong with the cube the function throws an exception telling what exactly is wrong with cube, but I would like to be able to send many messages previous to that one, for example: "Checking for correct colors..." "Checked" "Checking for correct piece orientation..." "Such and such corner has two colors repeated" so that I could have some sort of log on the process. Any Ideas on how to that? I don''t show any code because there''s nothing wrong with it up ''till now, I just don''t know how to do that.

P.S. when I write Throw new, under System exception there seems to be an object called SystemInformation. I tried researching that but I guess it''s just used to handle the operating system information.

推荐答案

这是我最初在此问题上发表评论的解决方案.


为什么不看一下制作自己的自定义事件和事件args的原因,您可以异步调用它们,然后使您可以使用其他代码向用户显示消息.此外,您可以将多个处理程序附加到一个事件上,这样您就可以让一个处理程序显示一条消息,而另一个处理程序编写一个日志,或者更好地将这些处理程序组合到一个处理程序中(但是您的代码可能需要我描述的分隔符-取决于您自己:) ).

看一下MSDN事件和代表: http://msdn.microsoft. com/en-us/library/aa903294(v = vs.71).aspx [ http://msdn.microsoft.com/en -us/library/dh37dwxs(v = vs.71).aspx [
This is the solution I originally posted as a comment on this question.


Why not have a look at making your own custom events and event args - you can invoke them asynchronously which will then allow you to have other code that displays a message to the user. Furthermore, you can attach multiple handlers to one event so you could have one handler that displays a message and another that writes a log or better combine those into one handler (but your code may require the separation I describe - up to you really :) ).

Take a look at MSDN Events and Delegates : http://msdn.microsoft.com/en-us/library/aa903294(v=vs.71).aspx[^]
and for a full example: http://msdn.microsoft.com/en-us/library/dh37dwxs(v=vs.71).aspx[^]


Hope this helps,
Ed


如果这是交互式的,那是一回事.您不需要太多,Edward的解决方案将是一个完整的答案.

但是,如果您正在开发一种实际上可以解决问题的算法,并且希望将步骤可视化(或者即使不是,但是解决方案需要花费一些明显的时间(很有可能)),那么问题也不在于您需要发送通知不退出该功能".问题看起来将完全不同:您将需要运行解决方案的是一个单独的线程.然后,向UI线程发出通知的问题看起来并不那么琐碎.正确的机制是UI线程委托 invocation 之一.

您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview扫描仪和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
If this is interactive, this is one thing. You don''t need much, and the solution by Edward would be a complete answer.

However, if you are developing an algorithm actually solving the problem, and if you want to visualize the step (or even if not but if the solution takes some noticeable time which is very likely), the problem is not that you need to send notification "without exiting such function". The problem will look completely different: you will need to run the solution is a separate thread. And then the problem of giving notification to the UI thread looks not so trivial. The right mechanism is the one of UI thread delegate invocation.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于如何在不退出vb.net中的功能的情况下从功能发送状态消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆