Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
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.
Posted
Comments
Ed Nutting 22-Feb-12 16:17pm    
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 displays a log or better combine those into one handler (but your code may require the separation).

Hope this helps,
Ed

P.s. 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
wizardzz 22-Feb-12 16:35pm    
I completely agree Edward, but can't vote a comment up unfortunately. Invisible +5 for you!
Ed Nutting 22-Feb-12 16:46pm    
Thank you wizardzz :) I shall post it as a solution then - just so others can find it more easily - I wasn't sure if it was actually the correct solution so I posted as a comment to start with :p
Sergey Alexandrovich Kryukov 22-Feb-12 16:59pm    
Yes, but I found your solution is not sufficient. Please see my answer.
I credited your comment though...
--SA
Sergey Alexandrovich Kryukov 22-Feb-12 16:58pm    
Not quite. It's only good in some simpler cases. In this case, it looks like threading is needed, and then the right mechanism is the UI thread delegate invocation.

Please see my answer.
--SA

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
 
Share this answer
 
Comments
Vic91 22-Feb-12 17:24pm    
Thanks man I already started looking into this stuff. This is all new to me unfortunately so I will have to do some studying.
Ed Nutting 22-Feb-12 17:26pm    
Glad I could help :)
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
 
Share this answer
 
Comments
Ed Nutting 22-Feb-12 17:09pm    
Yes but... If the OP has used threading already (which he might well have done but we don't know but for the sake of argument assume he has or will do then...) Setting up his own events and calling them asynchronously should solve both the threading and non-UI thread problems since (so far as I have experienced) if you call an event asynchronously from one thread, it is handled by the thread that added the handler in the first place. So long as OP adds any handlers to his update events on the UI thread then the event fired will be handled on the UI thread. This then solves your issue of UI update not being possible due cross-thread operations being blocked. Furthermore, it is simple than using threads. An alternative solution (but one I consider not to be elegant or efficient but is worth mentioning as all solutions are so that they may be combined) is to have a timer that polls a progress status object (from the UI thread) at an interval and then updates the UI accordingly. While polling isn't very elegant nor in many cases efficient, it is simpler to program/use to learn the basics of threading and then progress to our more complex solutions. Your solution is correct and good though and is more general purpose/fits more cases so I credit you accordingly; My 5+
Sergey Alexandrovich Kryukov 22-Feb-12 17:18pm    
Thank you. What you say makes sense, but i think using threads is much simpler solution in most cases. I really think that all kinds of async APIs is a legacy from the times where threads were not commonplace, no more. Threads are sequential, so the logic is way more straightforward. In some selected points of the code the logic is not sequential (otherwise we would not use threads at all), but this is a distinctly separate part of logic, the cross-thread logic. And then you use inter-thread communications (and UI thread invocation is only one them), which is also done explicitly and more flexible and straightforward.

I would say, the real nature of applications is multithreaded and not asynchronous, just because asynchronous behavior can also be expressed in terms of threading, and not visa versa. Threading is easier in development and support, too. Threading debugging is more straightforward, by the same reasons.

--SA
Ed Nutting 22-Feb-12 17:25pm    
Yes I agree with all you have said and yes your solution is better for more cases. I still feel that for the OP to learn to use events would be good too though since there are a lot of other cases where they can be useful (otherwise why would all the .Net Controls use them...) Anyway, a good answer and as I said, my 5+ :)
Sergey Alexandrovich Kryukov 22-Feb-12 17:35pm    
Thank you again. We all need to learn those things, of course. In particular, the events should be learned well regardless of the field of development. I mean, those special features and limitations (actually important as the fool-proof features) of events compared to "regular" delegates, which are even more important to understand (and often misunderstood; see, for example, in my work: http://www.codeproject.com/Articles/203453/Dynamic-Method-Dispatcher#a4.1).
--SA
Vic91 22-Feb-12 18:23pm    
Thanks for your time guys, really. I've been reading your comments and honestly It's kinda hard for me to understand everything perfectly (specially since English is not my native language) but I think I do. I haven't a strong knowledge of all the tools in .NET programming yet, since most of what I do know I've learned myself. I had worked with threads a bit when trying to write a socket based chat app, but I had never used events and I've been studying them for the last hour and I think it works perfectly for what I'm trying to do. Nonetheless I'll look into threading as well, for future reference. :) Again, thank you all!.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900