Click here to Skip to main content
15,917,565 members
Home / Discussions / C#
   

C#

 
GeneralRe: Begin printing at row x column y Pin
led mike15-Oct-07 4:52
led mike15-Oct-07 4:52 
AnswerRe: Begin printing at row x column y Pin
darkelv14-Oct-07 19:04
darkelv14-Oct-07 19:04 
GeneralRe: Begin printing at row x column y Pin
Roger CS15-Oct-07 4:15
Roger CS15-Oct-07 4:15 
QuestionThread & UI Pin
Archyami12-Oct-07 17:11
Archyami12-Oct-07 17:11 
AnswerRe: Thread & UI Pin
led mike12-Oct-07 20:26
led mike12-Oct-07 20:26 
AnswerRe: Thread & UI Pin
mav.northwind12-Oct-07 20:30
mav.northwind12-Oct-07 20:30 
GeneralRe: Thread & UI Pin
Archyami12-Oct-07 22:42
Archyami12-Oct-07 22:42 
QuestionDelegate awkwardness---is there a better way? Pin
Domenic Denicola12-Oct-07 16:35
Domenic Denicola12-Oct-07 16:35 
Hi all,

I have several methods, with many different signatures, that I would like to allow the user to cancel in the middle of their operation. This wouldn't be a backgroundworker situation, however---these are methods in my library that are called synchronously from a certain thread, and need to operate synchronously (i.e., the calling thread should be waiting on them to finish), then canceled from the UI thread. Also, since these methods by default operate just by a method call, and not any other invocation, I don't want to make them all conform to the backgroundworker's dowork event handler signature.

So, is there some kind of built-in way to do this, perhaps with an attribute? How would the method check that it's time to cancel? How can I specify, preferably at compile time of course, that only methods x, y, and z can be canceled?

Otherwise, my solution is as follows:
  • If you want to call the method normally (without canceling capabilities), just call it.
  • If you want to call a method in a cancelable fashion, call object Controller.PerformOngoingOperation(method, string id, params object[] args); another thread can later call Controller.CancelOngoingOperation(string id).
    • PerformOngoingOperation checks that the method passed (more on that later) has the appropriate OngoingOperationAttribute; if so, it invokes it. It also stores in an internal dictionary a map from ids to boolean "should cancel now" variables.
    • The method in question finds its ID. This is kludgy... it involves reflections to find the calling method, check if the calling method is PerformOngoingOperation, and if so, ready the id parameter.
    • It then uses this id to performs a series of checks throughout its operation against that internal map from ids to "should cancel now"; if at any point the should-cancel bit is true, it finishes what it's doing and returns.
    • Then, PerformOngoingOperation will return the results of the method, whether canceled prematurely or not. So it acts just like invoking the method (good for users!), but can allow cancellation.
    • The should-cancel bit is set by Control.CancelOngoingOperation(string id), with consequences as above.

IF the above approach is reasonable---and it's very nice from a user's perspective, except for the upcoming detail---then I have a question. How should I pass the method to PerformOngoingOperation? Is there any way to do so without awkwardness? What I mean is that, as far as I can tell, I want to use a delegate. This necessitates that the user of my library declare a new delegate type for every method he wants to call in a cancelable fashion! And he can't even declare delegate types locally; he has to pollute his class with them. Like this:

class C
{
    delegate int annoying(int param1, bool param2, string param3);
    void Method()
    {
        string id;
        int retVal = (int)Control.PerformOngoingOperation(new annoying(Library.Object.Method), out id, param1, param2, param3);
    }
}


I suppose an alternative is requiring that the delegate be of type MethodInvoker (basically void return type, no parameters), and forcing the user to do this:

int retVal;
string id = Control.PerformOngoingOperation((MethodInvoker)(new delegate() { retVal = Library.Object.Method(param1, param2, param3); }));


But that's awkward at best.

Ideally I would like to have something like this instead:

string id;
int retVal = Control.PerformOngoingOperation(Library.Object.Method, out id, param1, param2, param3);


Is that possible?


-Domenic Denicola- [CPUA 0x1337]

“I was born human. But this was an accident of fate—a condition merely of time and place. I believe it's something we have the power to change…”

AnswerRe: Delegate awkwardness---is there a better way? Pin
PIEBALDconsult12-Oct-07 16:43
mvePIEBALDconsult12-Oct-07 16:43 
AnswerRe: Delegate awkwardness---is there a better way? Pin
S. Senthil Kumar12-Oct-07 22:45
S. Senthil Kumar12-Oct-07 22:45 
GeneralRe: Delegate awkwardness---is there a better way? Pin
Domenic Denicola13-Oct-07 7:28
Domenic Denicola13-Oct-07 7:28 
QuestionExtracting files from inside a file archive Pin
Luminare12-Oct-07 16:16
Luminare12-Oct-07 16:16 
AnswerRe: Extracting files from inside a file archive Pin
Roger CS13-Oct-07 11:52
Roger CS13-Oct-07 11:52 
GeneralRe: Extracting files from inside a file archive Pin
Luminare14-Oct-07 10:48
Luminare14-Oct-07 10:48 
QuestionSystem.IO.Exception Pin
solutionsville12-Oct-07 13:53
solutionsville12-Oct-07 13:53 
AnswerRe: System.IO.Exception Pin
Dave Kreskowiak12-Oct-07 15:35
mveDave Kreskowiak12-Oct-07 15:35 
GeneralRe: System.IO.Exception Pin
solutionsville12-Oct-07 16:02
solutionsville12-Oct-07 16:02 
GeneralRe: System.IO.Exception Pin
Dave Kreskowiak12-Oct-07 17:00
mveDave Kreskowiak12-Oct-07 17:00 
GeneralRe: System.IO.Exception Pin
solutionsville12-Oct-07 17:07
solutionsville12-Oct-07 17:07 
GeneralRe: System.IO.Exception Pin
Dave Kreskowiak13-Oct-07 3:01
mveDave Kreskowiak13-Oct-07 3:01 
AnswerRe: System.IO.Exception Pin
solutionsville13-Oct-07 9:44
solutionsville13-Oct-07 9:44 
QuestionProblem with launching an external process Pin
Togakangaroo12-Oct-07 10:44
Togakangaroo12-Oct-07 10:44 
AnswerRe: Problem with launching an external process Pin
Ennis Ray Lynch, Jr.12-Oct-07 11:28
Ennis Ray Lynch, Jr.12-Oct-07 11:28 
AnswerRe: Problem with launching an external process Pin
Daniel Grunwald12-Oct-07 23:53
Daniel Grunwald12-Oct-07 23:53 
AnswerRe: Problem with launching an external process Pin
Togakangaroo15-Oct-07 4:34
Togakangaroo15-Oct-07 4:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.