Click here to Skip to main content
15,915,600 members
Home / Discussions / C#
   

C#

 
GeneralRe: Best way to populate grid? Pin
zaboboa27-Jul-07 11:57
zaboboa27-Jul-07 11:57 
GeneralRe: Best way to populate grid? Pin
Urs Enzler28-Jul-07 4:48
Urs Enzler28-Jul-07 4:48 
QuestionHow to get a foreign key lookup value in datagridview Pin
xdavidx26-Jul-07 10:13
xdavidx26-Jul-07 10:13 
QuestionC# to Mono Pin
Sean G. Murphy26-Jul-07 10:08
Sean G. Murphy26-Jul-07 10:08 
AnswerRe: C# to Mono Pin
Dan Neely26-Jul-07 10:37
Dan Neely26-Jul-07 10:37 
AnswerRe: C# to Mono Pin
Judah Gabriel Himango26-Jul-07 10:38
sponsorJudah Gabriel Himango26-Jul-07 10:38 
GeneralRe: C# to Mono Pin
Amar Chaudhary26-Jul-07 14:41
Amar Chaudhary26-Jul-07 14:41 
QuestionTiming out a method [modified] Pin
martin_hughes26-Jul-07 8:46
martin_hughes26-Jul-07 8:46 
Hey all,

I've been puzzling this over for the last hour; basically I only want to wait a certain amount of time for a method to complete before giving up (or unless a certain condition is met).

I've tried a few things involving watcher threads and so on, and finally hit upon the idea of using an asynchronous delegate (after some nosing around MSDN):

delegate string Worker(string args);

public string DoWork(string args)
{
    Worker d = new Worker(Workhandler);
    IAsyncResult result = d.BeginInvoke(args, null, null);
    if (!result.IsCompleted)
    {
        result.AsyncWaitHandle.WaitOne(1000, false);

        //Stick the condition(s) to test for here
        if (!result.IsCompleted)
        {
            //Throw an error, return a timeout value or whatever...
            return "Timeout!!";
        }
    }

    //return the "result" to the caller
    return d.EndInvoke(result);
}

private string Workhandler(string args)
{
    //Pretend that this method is taking too long
    Thread.Sleep(2500);
    //Return the result of the method
    return args;

}


Now the above seems to work perfectly ok, but I'm the first to admit I don't know much about delegates. So my questions are:

1.) If a timeout occurs (as in the above faked up example) what happens to the Workhandler? Does this continue to run, or does the .Net Framework leap in and forcibly stop any processing?
2.) I imagine that
d.BeginInvoke
is starting a thread; is this so, and where does this thread exist in terms of the application? Is it possible to grab hold of that thread without using the delegate to control it?
3.) Am I giving myself enough rope to hang myself with here - could this go horribly, horribly wrong in subtle ways?
4.) Are there any better/more standardized/preferred ways to achieve what I'm trying to achieve?






-- modified at 14:58 Thursday 26th July, 2007


"It was the day before today.... I remember it like it was yesterday."

-Moleman


AnswerRe: Timing out a method Pin
Justin Perez26-Jul-07 8:57
Justin Perez26-Jul-07 8:57 
GeneralRe: Timing out a method Pin
martin_hughes26-Jul-07 9:15
martin_hughes26-Jul-07 9:15 
AnswerRe: Timing out a method Pin
Le centriste26-Jul-07 10:17
Le centriste26-Jul-07 10:17 
QuestionClass declaration inside loops Pin
rsaint2726-Jul-07 7:18
rsaint2726-Jul-07 7:18 
AnswerRe: Class declaration inside loops Pin
PhilDanger26-Jul-07 7:29
PhilDanger26-Jul-07 7:29 
GeneralRe: Class declaration inside loops Pin
rsaint2726-Jul-07 7:55
rsaint2726-Jul-07 7:55 
GeneralRe: Class declaration inside loops Pin
Paul Conrad26-Jul-07 8:26
professionalPaul Conrad26-Jul-07 8:26 
GeneralRe: Class declaration inside loops Pin
rsaint2726-Jul-07 10:40
rsaint2726-Jul-07 10:40 
AnswerRe: Class declaration inside loops Pin
Guffa26-Jul-07 11:05
Guffa26-Jul-07 11:05 
GeneralRe: Class declaration inside loops Pin
PhilDanger26-Jul-07 11:12
PhilDanger26-Jul-07 11:12 
QuestionDataGridView - Binding to an Object Pin
BigPMcCoy26-Jul-07 6:55
BigPMcCoy26-Jul-07 6:55 
QuestionDataGridView Checkbox not populating when binding from ObjectDataSource [modified] Pin
Tristan Rhodes26-Jul-07 6:28
Tristan Rhodes26-Jul-07 6:28 
AnswerRe: DataGridView Checkbox not populating when binding from ObjectDataSource Pin
Eliz.k26-Jul-07 21:39
Eliz.k26-Jul-07 21:39 
GeneralRe: DataGridView Checkbox not populating when binding from ObjectDataSource Pin
Tristan Rhodes26-Jul-07 23:25
Tristan Rhodes26-Jul-07 23:25 
QuestionDataSet - mapping the colums Pin
auloepid26-Jul-07 5:23
auloepid26-Jul-07 5:23 
AnswerRe: DataSet - mapping the colums Pin
Tarakeshwar Reddy26-Jul-07 5:31
professionalTarakeshwar Reddy26-Jul-07 5:31 
QuestionSerial To USB Communication Pin
Michael Fritzius26-Jul-07 4:41
professionalMichael Fritzius26-Jul-07 4:41 

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.