473,320 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Sharing Data in DLLs

Hi,

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.

Any help is appreciated.

Thanks

Rajesh
http://www.slcltd.com
Aug 7 '06 #1
6 3024
What kind of error ?
Rajesh wrote:
Hi,

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.

Any help is appreciated.

Thanks

Rajesh
http://www.slcltd.com
Aug 7 '06 #2
It give "File in use".

Basically I am getting multiple instance of the DLL and the singleton
pattern is not working.

Regards
Rajesh
"Tasos Vogiatzoglou" wrote:
What kind of error ?
Rajesh wrote:
Hi,

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.

Any help is appreciated.

Thanks

Rajesh
http://www.slcltd.com

Aug 7 '06 #3
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
Aug 7 '06 #4
Hi,

You should have the file open the less possible time, so you should
open/close it in each call.
Even more, it's possible that more than one process will try to write to it
at the same time. so you will still have a problem.

Possible solutions:
1- Use event log instead of a file.
2- Use a dedicated thread to write down the events, this thread will use a
sync queue to store the log entries that need to be written. it will fire
from time to time and try to write the entries to the file. The entries will
be generated by the UI thread (or any other worker thread)

Hope this help,

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Rajesh" <Ra****@discussions.microsoft.comwrote in message
news:3A**********************************@microsof t.com...
Hi,

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.

Any help is appreciated.

Thanks

Rajesh
http://www.slcltd.com

Aug 7 '06 #5
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
Aug 7 '06 #6
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...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
Aug 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: chemdawg | last post by:
I"m Having a problem Sharing data between access Microsoft® Excel. I need to populate the databases in a flash by linking and updating the excel sheets.
1
by: phil | last post by:
Hi, I want to share some variables among several instances of a DLL, with MinGW compiler. What do I have to make ?
2
by: Red Green | last post by:
I am trying to switch to C# from Delphi and I am having some trouble finding some information. What I need to do should be simple but I have been through a dozen books and done web searches that...
2
by: Mervin Williams | last post by:
I am using Infragistics UltraWebTab (a tab folder control for ASP.NET). My tab folder control will include five tab pages with a separate web form on each, and these web forms will share data. ...
4
by: radiax | last post by:
Iam trying to find a simple solution for sharing data between windows applications( apart of using file system or remoting or MMF) . I tried using class library by making data members "shared" but...
3
by: Dax | last post by:
//////////////// Inside the APPLICATION/ game //how do i get the same instance running in the //dll into my app? i.e the pointer ferrari //does not work ! it creates a new instance in the ...
2
by: =?Utf-8?B?RWl0YW4=?= | last post by:
Hello, My application, winform, need to share data between multiple dialog boxes? What would be the best way to do it? Thanks, EitanB
9
by: Gilles Ganault | last post by:
Hello Some data are common to all user sessions, and to improve performance/save resources, I'd like to share those data between all sessions, so that each user doesn't have to hit MySQL for the...
9
by: kirk | last post by:
I have program.cs, my "main" form and then a "settings" form. My "main" form existed for awhile and I had constants, instantiations, properties, etc within it created. I went to create my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.