List Info

Thread: C-Sharp (C#) Group: Problem logging from different threads.




C-Sharp (C#) Group: Problem logging from different threads.
user name
2006-05-30 17:12:15
I have a service that has 6 different threads.  Each thread
has a timer
on it that elapses at about the same time (if not the same
time).  When
the timer elapses I am trying to log a message by writing to
a text
file.  The problem is that not every thread is able to log
its message.
 I thought it was because each thread is trying to use the
same
resource.  So i put in mutex.waitone() and released the
mutex at the
end of the method.  It still doesn't log all the messages. 
Is there
something that I am missing?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Problem logging from different threads.
user name
2006-06-01 17:49:02
i am not really sure what the hell mutex is but the only way
to  make
sure that the threads do not use a shared resource at the
same time is:

The complicated one:

Monitor.Enter(yourFileHandle)

//do the code to write to the file

Monitor.Exit(yourFileHandle)

The easy one:
lock(FileInfo file = new FileInfo(filePath))
{
   //your code here
}

and then there is the reader/writer lock which is a low
level
implementation, for which I have heard pretty bad output so
I cannot
comment.

Anyway, all that it means is that while one thread is doing
something
in the between monitor.enter/exit or the lock statement's
brakets no
other thread can go in and do anything. For instance:

ArrayList arrayList = new ArrayList();
lock(arrayList)
{
   arrayList.Add("Marty is cool");
   arrayList.Remove(0);
}

As soon as a thread enters this array list no other tread
can enter
that code.
Umm, and of course this is from the top of my head so head
on to the
nearest search engine and look for lock statement, and
monitor class
.net

Marty


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )