Re: Sharing Data in DLLs



Rajesh a écrit :
Hi Mathieu,

Can you give me littel more insight that how do I use it. I will again try to explain what I am aiming to achive -

I have three programs
1. FileWatcherService1
2. ProcessWatcherService1
3. MyUI
The two services will always run simultaneously and MyUI may run at any time.

And I have a DLL "LogManager" which writes to a log file "C:\abc.log". The LogManager class is created using the singleton pattern.

Now I want to write my debug, error and information messages through the DLL to C:\abc.log file. As stated by you, I am able to write to abc.log from multiple threads from the same process. But I am not able to write from multiple services / programs running on my machine.

I am not sure how to implement Threading.Mutex. Someone have suggested me using the Shared Data segements in the DLL.

Can you give some reference about the implementation of Threading.Mutex.

Regards Rajesh Thareja


"Mathieu Cartoixa" wrote:

Tasos Vogiatzoglou a écrit :
Rajesh wrote:
We are tyring to build a DLL which will write the log data to a text file.

Multiple executables should use this dll to write data to same text file. We
are using a synchronized method (using TextWriter.Synchronized) for writing
data to the text file. The class in the DLL (LogManagement) is implemented
using the Singleton pattern.

My problem is that when I try to write data from more that one executables
(and services) it gives me error and does not allow write to the file.
Hi,

TextWriter.Synchronized() creates an wrapper that can only guarantee that two threads in the same process will not conflict while writing your data to the text file. It cannot guarantee that two threads in different processes will not conflict either.
To prevent conflicts across processes, you will have to implement the synchronization yourself, using the System.Threading.Mutex class for instance.
Also, make sure that the method you use to create/open the text file does not lock it to other processes...

Mathieu


Hi,

A good starting point in learning how to use mutexes could be this page : http://msdn.microsoft.com/library/en-us/cpguide/html/cpconmutex.asp?frame=true

I am sorry I do not have much time to write proper, compiling, fully tested code, but something like this would do :

using System;
using System.IO;
using System.Threading;

public class LogManager
{
private static string _LogFileName=@"C:\abc.log";

// Better use a GUID here, so that the mutex name will not conflict with another...
private Mutex _Mutex=new Mutex(false, "66db34d1-1528-4083-b0c8-825d00f1aa7c");

/* Insert constructor, singleton (...) code here... */


public void Log(string logMessage)
{
_Mutex.WaitOne();

// Guarantee : only one thread of one single process at a time in this section

using (StreamWriter sw=File.Append
Text(_LogFileName))
{
sw.WriteLine(logMessage);
sw.Close();
}

_Mutex.ReleaseHandle();
}
}

Note that the threads calling the method Log() will wait until they can get the mutex, which may not be suitable in every situation...
Also, as suggested by Ignacio, consider using the event log...

Mathieu
.



Relevant Pages

  • Re: IIS permissions error sporadically occurs
    ... I may have found the answer Roger. ... So I believe I did in fact have a name conflict. ... > I am not so sure abont there being no error in their dll. ... > to intercept that and recycle that app. ...
    (microsoft.public.windows.server.security)
  • Re: Usign old DLL in VC .NET App
    ... You can #undef the macros that conflict. ... Ronald Laeremans ... I want to call a function, in DLL, on a button click in main form ...
    (microsoft.public.dotnet.languages.vc)
  • Re: IIS permissions error sporadically occurs
    ... before removing an old dll it does not ... "Rob" wrote in message ... > I may have found the answer Roger. ... > So I believe I did in fact have a name conflict. ...
    (microsoft.public.windows.server.security)
  • Re: Access Violation Errors while using free
    ... DLL, right? ... Microsoft MVP, MCSD ... "Rajesh" wrote in message ... > But i'm facing runtime access violation errors that is especially from the ...
    (microsoft.public.win32.programmer.ole)
  • Re: Base Address Proprty
    ... VB6 defaulted to a fixed address, which required the DLL to be ... there will be no conflict. ... > We are writing a set of Windows Services using C#.NET that will run on the> same server. ... > when to set the Base Address or what its practical effect would be. ...
    (microsoft.public.dotnet.languages.csharp)