Click here to Skip to main content
15,888,610 members
Home / Discussions / C#
   

C#

 
QuestionFile Manipulation Pin
LCI19-Nov-07 6:40
LCI19-Nov-07 6:40 
AnswerRe: File Manipulation Pin
pmarfleet19-Nov-07 8:39
pmarfleet19-Nov-07 8:39 
GeneralRe: File Manipulation Pin
LCI19-Nov-07 16:43
LCI19-Nov-07 16:43 
Questionlogin page problem Pin
rocky81119-Nov-07 6:22
rocky81119-Nov-07 6:22 
AnswerRe: login page problem Pin
Anthony Mushrow19-Nov-07 6:33
professionalAnthony Mushrow19-Nov-07 6:33 
GeneralRe: login page problem Pin
rocky81119-Nov-07 6:34
rocky81119-Nov-07 6:34 
QuestionGeneral Questions using Attach Process [modified] Pin
Khoramdin19-Nov-07 6:18
Khoramdin19-Nov-07 6:18 
QuestionSome issue with thread synchronization & events Pin
Karma Komae19-Nov-07 5:44
Karma Komae19-Nov-07 5:44 
Hello,

Well, i'm messing with C# for one month now (sort of new to me), and i've studied a bit how thread works in .NET 2.0, but i'm facing an annoying problem i can't fix ! Here it is :

Let's assume i've got the main thread in my windows Form, and two others i'm trying to manage within this Form. A big point of interest to me is to know for sure a thread has ended, so it seems kinda easy at first. Thing is, there are some stuff that i can't make it work, so i'm here to ask for your help Big Grin | :-D !

The main thread is rather big with some processing, and handles the two others.

One of the others is a small one, looping to wait for data input. It looks like this :

public void dataReadThread(){
    try{
        while(true){
            if(eventAbortRead.WaitOne(5,false))break;
            dataReceived = receiveData();
            if(dataReceived)eventProceedNext.Set();
        }
    }
    catch (...) {}
    eventReadThreadHasExited.Set();
}


So it's supposed to allways try to read data, and send an event when it get a response. If the thread is asked to abort, the break statement leaves the while and then fires the event eventReadThreadHasExited.

Well, thing is that works without any issues because the function receiveData is quite simple.

Now the processing thread is rather similar in it's architecture, but a bit more complex !

private void processInstructionThread(){
    while(!stop){
        if(eventAbortProcess.WaitOne(5,false))break;
        stop = processLine();  //return true when the whole processing is done
        if(FLAG_WaitAnswer){
            WaitHandle[] wh = new WaitHandle[2] {eventProceedNext, eventAbortProcess}; //To avoid being blocked
            if(WaitHandle.WaitAny(wh) == 1) //i.e. abort
                break;
        }
        //do some work
    }
    eventProcessThreadHasExited();
}


So there's no huge difference between these two threads, except the processLine function is really complex and does a lot of processing.

Now on the main thread, my thread are started and processing, and everything goes fine ... until i try to abort the threads ...

To do so, it seems quite easy, but to me it's not (maybe my code is totally wrong, but looked rather suitable to me). Here is what i'm trying to do and where i get the issues on the main thread :

protected override void OnClose(EventArgs e){
    this.eventAbortRead.Set();
    if( ! eventReadThreadHasFinished.WaitOne(5000, false) ){
         //display some error message
    }
    this.eventAbortProcess.Set();
    if( ! eventProcessThreadHasFinished.WaitOne(5000, false) ){
         //display some error message
    }
    //Has to be sure both threads ended before processing the remaining code
}


The thing is that for some unknow reason, it sometimes works and sometimes doesn't. Most of the time the read abortion is doing fine, but the Process abortion is impossible ! It looks like doing a WaitOne() in the main thread pauses or interrupts the other threads at some time !

I've also tried to code the waiting with FLAGs (with a while(!FLAG) Thread.Sleep(0); ) and it seems to do the same error Frown | :-( .

I've also tried to code these thread using backgroundWorker but no success Frown | :-(

Well, seems a bit weird to me, and i think i miss something enomous, but can't figure out what !

So if any of you has any idea, i'd be glad to test it !




Best Regards,

Mamat,

AnswerRe: Some issue with thread synchronization & events Pin
Judah Gabriel Himango19-Nov-07 9:31
sponsorJudah Gabriel Himango19-Nov-07 9:31 
GeneralRe: Some issue with thread synchronization & events Pin
Karma Komae19-Nov-07 21:56
Karma Komae19-Nov-07 21:56 
QuestionDataBound DataGridView Relational DB Problem Pin
shirazamod19-Nov-07 5:03
shirazamod19-Nov-07 5:03 
AnswerRe: DataBound DataGridView Relational DB Problem Pin
pmarfleet19-Nov-07 9:04
pmarfleet19-Nov-07 9:04 
GeneralRe: DataBound DataGridView Relational DB Problem Pin
shirazamod19-Nov-07 9:45
shirazamod19-Nov-07 9:45 
GeneralRe: DataBound DataGridView Relational DB Problem Pin
pmarfleet19-Nov-07 9:50
pmarfleet19-Nov-07 9:50 
QuestionEnumerators and locks. Pin
jmhamm19-Nov-07 4:40
jmhamm19-Nov-07 4:40 
AnswerRe: Enumerators and locks. Pin
Bekjong19-Nov-07 5:00
Bekjong19-Nov-07 5:00 
GeneralRe: Enumerators and locks. Pin
jmhamm19-Nov-07 5:08
jmhamm19-Nov-07 5:08 
AnswerRe: Enumerators and locks. Pin
Daniel Grunwald19-Nov-07 11:42
Daniel Grunwald19-Nov-07 11:42 
QuestionFindControl on Fields on an aspx screen. Pin
imnotso#19-Nov-07 4:33
imnotso#19-Nov-07 4:33 
AnswerRe: FindControl on Fields on an aspx screen. Pin
Anthony Mushrow19-Nov-07 4:42
professionalAnthony Mushrow19-Nov-07 4:42 
GeneralRe: FindControl on Fields on an aspx screen. Pin
imnotso#19-Nov-07 4:58
imnotso#19-Nov-07 4:58 
AnswerRe: FindControl on Fields on an aspx screen. Pin
martin_hughes19-Nov-07 5:27
martin_hughes19-Nov-07 5:27 
GeneralRe: FindControl on Fields on an aspx screen. Pin
imnotso#19-Nov-07 5:36
imnotso#19-Nov-07 5:36 
GeneralRe: FindControl on Fields on an aspx screen. Pin
Anthony Mushrow19-Nov-07 5:42
professionalAnthony Mushrow19-Nov-07 5:42 
GeneralRe: FindControl on Fields on an aspx screen. Pin
imnotso#19-Nov-07 5:55
imnotso#19-Nov-07 5:55 

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.