Click here to Skip to main content
15,907,874 members
Home / Discussions / C#
   

C#

 
AnswerRe: JavaScript <-> C# and WebBrowser: How to serialize JS-Objects to C#-Classes and Backward? Pin
Gerry Schmitz28-Nov-09 4:24
mveGerry Schmitz28-Nov-09 4:24 
GeneralRe: JavaScript <-> C# and WebBrowser: How to serialize JS-Objects to C#-Classes and Backward? Pin
softwarejaeger29-Nov-09 1:48
softwarejaeger29-Nov-09 1:48 
GeneralRe: JavaScript <-> C# and WebBrowser: How to serialize JS-Objects to C#-Classes and Backward? Pin
Gerry Schmitz29-Nov-09 4:44
mveGerry Schmitz29-Nov-09 4:44 
Question[Resolved]Calling to a thread [modified] Pin
Trollslayer27-Nov-09 9:35
mentorTrollslayer27-Nov-09 9:35 
AnswerRe: Calling to a thread Pin
Garth J Lancaster27-Nov-09 10:33
professionalGarth J Lancaster27-Nov-09 10:33 
AnswerRe: Calling to a thread Pin
Luc Pattyn27-Nov-09 10:35
sitebuilderLuc Pattyn27-Nov-09 10:35 
General[More info] Re: Calling to a thread Pin
Trollslayer27-Nov-09 11:12
mentorTrollslayer27-Nov-09 11:12 
GeneralRe: [More info] Re: Calling to a thread Pin
Luc Pattyn27-Nov-09 12:41
sitebuilderLuc Pattyn27-Nov-09 12:41 
Hi Elaine,

no the situation isn't clear yet. I have no idea how long a test job lasts, and whether you want to change parameters in the middle of a test, etc.

I'll try to provide a general answer:

1. if jobs (units of work that will be executed sequentially) don't need parameter changes while running, either use a new BGW for each job, or create a queue (with a lock) where the main thread produces jobs and the BGW executes (i.e. consumes) jobs. With a queue, the BGW would have a main loop which dequeues a job and blocks when none available.
BTW: a queue can be implemented in many ways, e.g. by using the System.Collections.Queue class, the System.Collections.Generic.Queue<T> class, or something you build yourself probably on top of a List<T>
suggestion: carefully read the MSDN remarks on the queue you choose (thread safety!).

2. if a running job needs a simple parameter change, just change it (assuming no synchronization is necessary, i.e. the parameter is allowed to change at all times). If the parameter cannot be read in one CPU instruction, or if it should not be changed at random times, or if there are several parameters that need to be consistent, I would probably recommend an extra class:
2a. as a minimum a little Parameters class, so the producer can create a new instance, insert the new set of values, then have the consumer update its Parameters reference in a cooperative way (e.g. at the top of its execution loop).
2b. a more structured approach would take a Job class that hides the BGW, and provides properties for all parameters, so it also can hide your consistency stuff.

Example in pseudo-code (not quite safe):
class Job {
    Parms initialParms;
    Parms runningParms;

    Job(Parms parms) {
        initialParms=parms;
    }
    Run() {
        runningParms=initialParms;
        runPart1();
        runningParms=initialParms;
        runPart2();
        runningParms=initialParms;
        runPart3();
    }
    ParmType1 Parm1 {set {initialParms.Parm1=value;}}
    ParmType2 Parm2 {set {initialParms.Parm2=value;}}
}

struct Parms {
    ParmType1 Parm1;
    ParmType2 Parm2;
}


The example isn't safe: you could be modifying Parm1 while the BGW is executing runningParms=initialParms; resulting in inconsistencies. So you need to provide some safety measure. A possible way would be:

class Job {
    ManualResetEvent MREproducer;
    ManualResetEvent MREconsumer;
    Parms initialParms;
    Parms runningParms;

    Job(Parms parms) {
        initialParms=parms;
        MREproducer=new ManualResetEvent(true);
        MREconsumer=new ManualResetEvent(false);
    }

    Run() {
        updateParms();
        runPart1();
        updateParms();
        runPart2();
        updateParms();
        runPart3();
    }

    updateParms() {
        MREproducer.WaitOne();
        runningParms=initialParms;
    }


    ParmType1 Parm1 {set {MREconsumer.WaitOne(); initialParms.Parm1=value;}}
    ParmType2 Parm2 {set {MREconsumer.WaitOne(); initialParms.Parm2=value;}}
    BeginParmChanges() { Clear(MREproducer); Set(MREconsumer);}
    EndParmChanges()   { Clear(MREconsumer); Set(MREproducer);}
}

struct Parms {
    ParmType1 Parm1;
    ParmType2 Parm2;
}


remark: to hide the BGW inside Job, create it in the constructor, and turn Run() into a DoWork() handler.

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


GeneralRe: [More info] Re: Calling to a thread Pin
Trollslayer27-Nov-09 13:00
mentorTrollslayer27-Nov-09 13:00 
GeneralRe: [More info] Re: Calling to a thread Pin
Daniel Grunwald27-Nov-09 14:57
Daniel Grunwald27-Nov-09 14:57 
GeneralRe: [More info] Re: Calling to a thread Pin
Trollslayer27-Nov-09 23:25
mentorTrollslayer27-Nov-09 23:25 
QuestionDistance calculator Pin
Chazzysb27-Nov-09 8:57
Chazzysb27-Nov-09 8:57 
AnswerRe: Distance calculator Pin
Roger Wright27-Nov-09 20:23
professionalRoger Wright27-Nov-09 20:23 
QuestionChild form in C# Pin
VB Overlord27-Nov-09 7:45
VB Overlord27-Nov-09 7:45 
AnswerRe: Child form in C# Pin
Luc Pattyn27-Nov-09 9:07
sitebuilderLuc Pattyn27-Nov-09 9:07 
GeneralRe: Child form in C# Pin
VB Overlord27-Nov-09 9:19
VB Overlord27-Nov-09 9:19 
GeneralRe: Child form in C# Pin
VB Overlord27-Nov-09 9:29
VB Overlord27-Nov-09 9:29 
QuestionArray from JavaScript Pin
Hans Ruck27-Nov-09 3:01
Hans Ruck27-Nov-09 3:01 
AnswerRe: Array from JavaScript Pin
Shameel27-Nov-09 3:10
professionalShameel27-Nov-09 3:10 
GeneralRe: Array from JavaScript Pin
Hans Ruck27-Nov-09 3:14
Hans Ruck27-Nov-09 3:14 
GeneralRe: Array from JavaScript Pin
Shameel27-Nov-09 3:22
professionalShameel27-Nov-09 3:22 
GeneralRe: Array from JavaScript Pin
dojohansen27-Nov-09 4:40
dojohansen27-Nov-09 4:40 
GeneralRe: Array from JavaScript Pin
dojohansen27-Nov-09 4:38
dojohansen27-Nov-09 4:38 
QuestionAbout dateTimePicker problem [modified] Pin
miss YY27-Nov-09 2:24
miss YY27-Nov-09 2:24 
AnswerRe: About dateTimePicker problem Pin
Rob Philpott27-Nov-09 2:36
Rob Philpott27-Nov-09 2:36 

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.