Click here to Skip to main content
15,890,123 members
Home / Discussions / C#
   

C#

 
AnswerRe: Writing to ListBox from multiple threads Pin
Henry Minute7-Apr-09 1:15
Henry Minute7-Apr-09 1:15 
GeneralRe: Writing to ListBox from multiple threads Pin
Nicholas Butler7-Apr-09 1:23
sitebuilderNicholas Butler7-Apr-09 1:23 
GeneralRe: Writing to ListBox from multiple threads Pin
Henry Minute7-Apr-09 1:35
Henry Minute7-Apr-09 1:35 
GeneralRe: Writing to ListBox from multiple threads Pin
Bruce Coward7-Apr-09 1:24
Bruce Coward7-Apr-09 1:24 
GeneralRe: Writing to ListBox from multiple threads Pin
Henry Minute7-Apr-09 1:35
Henry Minute7-Apr-09 1:35 
GeneralRe: Writing to ListBox from multiple threads Pin
Bruce Coward7-Apr-09 1:40
Bruce Coward7-Apr-09 1:40 
AnswerRe: Writing to ListBox from multiple threads Pin
DaveyM697-Apr-09 2:22
professionalDaveyM697-Apr-09 2:22 
AnswerRe: Writing to ListBox from multiple threads Pin
Luc Pattyn7-Apr-09 4:05
sitebuilderLuc Pattyn7-Apr-09 4:05 
Hi Bruce,

the code shown is perfect. However the problem is elsewhere. You have to help those threads to find and use your log method. Especially for logging purposes I tend to do that with static events, like so
(incomplete and unchecked code):

class Form1 : Form {
    public Form() {
        ...
        Class2.Logger+=log;    // one statement per "log client" class
    }

    public void log(string s) {...}
}


and the "log clients" look like this:

class Class2 {
    public static event Action<string> Logger;
    private void log(string s) {
        if (Logger!=null) Logger(s);
    }
}


BTW: the event does not have to be static, but it allows for doing Logger+=log before the actual client objects have been created.

PS: I always like to have a log(Exception) method too; I either solve that by doing the above stuff twice, or by adding the following code in every log client class:

private void log(Exception exc) {
    foreach(string s in exc.ToString().Split('\r','\n')) if (s.Length!=0) log(s);
}



Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


GeneralRe: Writing to ListBox from multiple threads - OT Pin
DaveyM697-Apr-09 5:14
professionalDaveyM697-Apr-09 5:14 
GeneralRe: Writing to ListBox from multiple threads - OT Pin
Luc Pattyn7-Apr-09 5:31
sitebuilderLuc Pattyn7-Apr-09 5:31 
Questionproblem when writing to eventlog Pin
prasadbuddhika6-Apr-09 23:56
prasadbuddhika6-Apr-09 23:56 
QuestionCreating a Collection of User Defined classes in a namespace and Invoking each class dynamicly Pin
grealish6-Apr-09 23:52
grealish6-Apr-09 23:52 
Questionan installation problem of excel addin Pin
abhiram_nayan6-Apr-09 23:48
abhiram_nayan6-Apr-09 23:48 
AnswerRe: an installation problem of excel addin Pin
abhiram_nayan7-Apr-09 20:16
abhiram_nayan7-Apr-09 20:16 
QuestionSession level connection object Pin
abhishek agrawal19836-Apr-09 23:07
abhishek agrawal19836-Apr-09 23:07 
Questionviewing all fonts in "windows/fonts" Pin
tai-fun6-Apr-09 22:42
tai-fun6-Apr-09 22:42 
AnswerRe: viewing all fonts in "windows/fonts" Pin
Henry Minute7-Apr-09 1:42
Henry Minute7-Apr-09 1:42 
QuestionHow to do unit testing to non-function? [modified] Pin
BabyOreo6-Apr-09 22:42
BabyOreo6-Apr-09 22:42 
QuestionSearch through list of objects. Pin
xkrja6-Apr-09 22:36
xkrja6-Apr-09 22:36 
AnswerRe: Search through list of objects. Pin
Christian Graus7-Apr-09 0:38
protectorChristian Graus7-Apr-09 0:38 
AnswerRe: Search through list of objects. Pin
MumbleB7-Apr-09 3:45
MumbleB7-Apr-09 3:45 
GeneralRe: Search through list of objects. Pin
xkrja7-Apr-09 4:44
xkrja7-Apr-09 4:44 
QuestionCompress and extract a folder Pin
yesu prakash6-Apr-09 21:37
yesu prakash6-Apr-09 21:37 
AnswerRe: Compress and extract a folder Pin
Christian Graus6-Apr-09 21:39
protectorChristian Graus6-Apr-09 21:39 
GeneralRe: Compress and extract a folder Pin
Vikram A Punathambekar6-Apr-09 22:11
Vikram A Punathambekar6-Apr-09 22:11 

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.