Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Canceling out of a delegate or IComparable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
superjodash@gmail.com - 12 Mar 2008 01:24 GMT
Hi,

I am sorting a large List<FileInfo> using Sort(new MyComparable()). In
other areas, I'm using an anonymous delegate on a List<>.FindAll(...).

My question is this: How do you cancel out of these scenarios? For
instance, Sort calls the following

public class MyComparable() : IComparer<FileInfo> {
 public virtual int Compare(FileInfo x, FileInfo y) {
   // i want the cancel here
 }
}

The problem is that the Sort method calls this function for each item.
Without throwing an exception, I can't seem to figure out how to stop
the sorting process. Granted, this is probably not a good idea since
it acts directly on the array. However, I still would like to know if
anyone knows how to do it.

Thanks,
John
Jon Skeet [C# MVP] - 12 Mar 2008 01:34 GMT
> I am sorting a large List<FileInfo> using Sort(new MyComparable()). In
> other areas, I'm using an anonymous delegate on a List<>.FindAll(...).
[quoted text clipped - 13 lines]
> it acts directly on the array. However, I still would like to know if
> anyone knows how to do it.

You can't, basically.

You could write your own CancellableSort routine, which used a
different comparison delegate (or IComparer<T> equivalent) but included
a flag allowing the delegate to cancel - but that's the only option
aside from exceptions.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Ignacio Machin ( .NET/ C# MVP ) - 12 Mar 2008 12:52 GMT
Hi,

> Hi,
>
[quoted text clipped - 3 lines]
> My question is this: How do you cancel out of these scenarios? For
> instance, Sort calls the following

You cannot, basically the List class is doing a loop (for FindAll) in the
collection and ntil it gets to the end it cannot be sure if FindAll will
really find all :)

I'm afraid than an exception is your only alternative, a dirty solution I
might add :(

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.