|
Great! Was there a question in there, or did you just want to announce to the world what you're doing?
|
|
|
|
|
My Bad! Engrossed in this thing I cant think straight!
I want to find out which one is better. Thanks in advance for your suggestions!
|
|
|
|
|
The one that meets all of your specifications.
"Better" is a subjective term. The only way you're going to know is to test each one against what you want to do.
|
|
|
|
|
Hi
Is there a way of writing and reading a csv file from C# at the same time?
I mean I have two separated projects; one keeps writing data into a csv file and the other one is to read the data from the same csv that is also being written.
When I tried this, it always says the file can't be accessed because it's being used by another process.
Is there anyway of getting around this?
Many thanks indeed.
|
|
|
|
|
Tony4966 wrote: Is there anyway of getting around this?
The easiest way that comes to mind is to replace the file with a desktop database. You can read/write into the database from multiple points, and export the parts to CSV if you need them.
I are Troll
|
|
|
|
|
The program that writes the file needs to allow other processes to read it; this is done when the stream is opened.
But, really, this is unlikely to be a good design. Do you really want the file? Or are you only trying to have inter-process communication? If you don't actually need the file; then find a better way.
|
|
|
|
|
You are right. This is probably not a good design but I just want to get this working as quickly as possible and it is just a short term solution.
So please can you be a little bit more specific about how exactly I let the program that writes into the file allow other programs to read the file at the same time?
A quick example of would be
string path = @"C:\Documents and Settings\";
string filename = "tester.csv";
TextWriter writer = new StreamWriter(path + filename, false);
for (int j = 0; j < 10; j++)
writer.WriteLine("{0}, {1}", j,2 * j);
Many thanks indeed.
|
|
|
|
|
You can create a FileStream that allows read access using this[^] constructor:
Stream stream = new FileStream(path + filename, FileAccess.OpenOrCreate, FileAccess.Write, FileShare.Read);
TextWriter writer = new StreamWriter(stream); Also it is a better pratice to dispose of the stream, enclosing them with the using keyword.
|
|
|
|
|
Consider using a message queue. It is robust and flexible, performs well, supports asynchronous processing (if desired) and doesn't take a lot of code so should be as quick to get up and running as messing with the files.
|
|
|
|
|
a service with a queue seems like an awesome idea for production code; you could add logging and keep adding all sorts of features as the requirements of the other applications change.
I personally have used a MemoryMapped file to exchange data between two applications because I hate using the HDD for temporary data.
EDIT: for an example of a memorymapped file: http://msdn.microsoft.com/en-us/library/dd997372%28VS.100%29.aspx[^]
modified on Monday, November 23, 2009 6:45 PM
|
|
|
|
|
I suppose a memory-mapped file is faster if there is a very large number of interactions. And I don't think message queues are installed by default on non-server editions of Windows, at least not XP. If performance is paramount there's probably nothing that can beat shared memory!
On the other hand message queues are extremely flexible. It's straightforward to share one between any number of applications (either as producers or consumers), it supports distributed transactions, the apps and the queue itself can be on the same or different computers, running the same or different Windows versions, there is built-in support for preserving a message log, they can be easily used synchronously or asynchronously, and .NET has all the wrappers needed to make it easy to work with them.
Wikipedia (usually good for getting an overview)
http://en.wikipedia.org/wiki/Microsoft_Message_Queuing
MSDN (usually good for reference, rather variable IMO on describing "the big picture")
http://msdn.microsoft.com/en-us/library/ms978425.aspx
[EDIT: changed the MSDN link to one about .NET and MSMQ; originally linked to Win32 doc.]
|
|
|
|
|
Hi,
You will have to drop down to the FileStream level to open the files in the correct manner. The writer should open the file with FileAccess.Write and FileShare.ReadWrite. The reader will obviously use FileAccess.Read but perhaps not so obviously must also specify FileShare.ReadWrite, otherwise the open will fail with exception that you have already seen.
The writer must flush the stream after each write operation to ensure that the latest data is available to the reader.
The writing code will be something along these lines:
FileStream fsw = new FileStream("filename", FileMode.Open, FileAccess.Write, FileShare.ReadWrite, 8, FileOptions.WriteThrough);
....
....
Byte[] buff = Encoding.Default.GetBytes("Data to write");
fsw.Write(buff, 0, buff.Length);
fsw.Flush();
The reader should open the file thus:
fsr = new FileStream("filename", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
That's the basics of it, assuming that there is only one writer. If that's not the case then you will need to look into locking the file.
Alan.
|
|
|
|
|
Thanks so much for so many excellent replies. I appreicate all of them
I finally 'have to' give up this 'bad' design because I can get it working but it's very messy and probably not worthwhile spending too much time on it.
Just a quick note; I get writer part working like this, which is enssentially the same as suggested.
string path = @"C:\Documents and Settings\";
string filename = "testernew1.txt";
var fi = File.Open(path + filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
var sw = new StreamWriter(fi);
for (int j = 10; j < 0; j--)
sw.Write(j);
sw.Flush();
fi.Flush();
sw.Close();
fi.Close();
Apparently, the reader part is very simliar.
I am not familiar with message queque so I am going to look into it; also someone told me that it also be done by using remote charting which I assume is more complicated than message quque?
Anyway, it would be great if someone can show some examples of either using message queue or remote charting.
Thanks again for all the replies!
|
|
|
|
|
I concur with Alan. That is the way that has worked for me many times already.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
If you don't need the file, but want to work with the "stream" behavior, use NamedPipes.
See the classes NamedPipeServerStream and NamedPipeClientStream. They are easy to use and are created for interprocess communication.
|
|
|
|
|
Hi Friends, I have an issue regarding components but I could not find out through google or somewhere else.
I'm creating a component extended from a TextBox with extra bindable properties, but to get it perfect, some of these bindable properties would have to be custom designed or runtime designed.
Imagine that besides the bindable property "Text" I created a new one named Key... but to get my component working I need to bind extra properties and they have to be custom. there comes the question.
Is it possible to create bindable properties during runtime or design time for a custom component?
[Bindable(true)]
public int Key { set; get; }
|
|
|
|
|
All properties are bindable, however the usual problem lies in two-way binding, that is getting the textbox properties to update automatically when the object it is bound to changes.
Take as look at the INotifyPropertyChanged[^] interface. To get the binding working you will probably have to implement this interface on your Textbox.
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
www.JacksonSoft.co.uk
|
|
|
|
|
I understand it, but I have more than one custom bindable attribute defined in the component. I had problems to get the values updated, but I could fix it using Reflection, it's now fine. I just keep with this other problem, I need to design custom bindable atributtes in runtime..
That's an example of what I need:
string[] bindableProperties = {"Text", "EditValue", "Key" }
foreach (string BindablePropertyName in bindableProperties)
{
// command to create custom bindable property named BindablePropertyName.ToString() < --------- (My Needs!)
Object.DataBindings.Add(Binding(BindablePropertyName, Object, "PROPERTY_NAME", true));
}
|
|
|
|
|
You can create new types at run-time but you can't modify an existing one.
In any case, what's this for? What would bind to the properties? Obviously there can be no design-time support or anything like that for properties that don't exist at design-time and aren't known until run-time. And if it's all happening dynamically at run-time and uses reflection, why even use properties? Why not just a name-value collection?
|
|
|
|
|
let's use the following relational example.
Database Structure:
table one: Employee(EmployeeID, Name, ContactID)
table two: Contact(ContactID, ContactName, Email)
Given the information, an Employee can have ONE Contact. My Component has 2 custom bindable properties, KeyID and Text... when the user types the name of the Contact, it binds the Text (ContactName), and the ContactID in the KeyID property as soon as it is validated.
That's fine... the data at the primary table will be filled and if I try to insert the data on the database, it will work.
Now imagine that I want to see more than one information, not only the contact Name, but I want the component to fill other information Like Email, so.. when i type the name and bind it's key, i also want to show the contact email in another field.... then I need another bindable property linked to the ContactEmail.
Form Design presentation:
EmployeeID (textfield), Name (textField)
ContactID and ContactName (customComponent databind (Text and KeyID))
Email = ? (what is going to fill the email data when I type and select the ContactName on my Component?)
I'll have both textfield and CustomComponent binding to the same field (Email) but one will be used ONLY as presentation. Actually
i need a code example of how to create a bindable property, all the others stuffs I can fix
Any doubts about it?
Thanks!
|
|
|
|
|
I still don't understand what this has to do with run-time or why you need a bindable property. Are you going to dynamically generate the UI based on the data model? If so, why not just create another text box for the email field and bind the text property of that text box? Or will the data model itself be user-defineable and thus change at run-time?
I also think putting storage logic into a UI component is a bad idea. It will tightly couple the UI to the storage medium, and worse, make it rather harder to maintain the system long term. What if you need to add a different UI layer for portable devices? Or a web service? Or if you want to have a native Windows Forms interface and an asp.net web app interface? What if you decide to add import/export capabilities so you can create Employee or other records from files?
A far better solution in my opinion is to use an object-relational mapper such as NHibernate. Then you'll have a class representing the Employee and use that as the data source for your UI. If you really need to do these things dynamically, you can still do it this way - it's a lot more work obviously, but you could generate both the classes and the NHibernate mappings.
If you want an "employee" UI control a simple user control would suffice, and it could take an Employee instance as data source.
|
|
|
|
|
I'll try to explain again...
Imagine I have many textboxes on a windows form and they are all binded to their corresponding fields at the bindingsource. If I fill information on the textboxes, than add another record and so on.. there will be lots of records with different data... that's nothing more then a simple windows forms with databinding. Fine.
Now imagine that I have a form with different information and I need to get data from an extra table to fill the corresponding foreign key value while i'm inserting a new record. I would have to do some search engine or use a dropdown whose datasource would select data from the extra table. Alright....
I made a component, and when the guy start typing something, it brings data from the corresponding extra table and when the Enter Key is pressed, it validates the information and updates the foreign key.
However, I have 2 bindable properties, one for the foreign key field and another one for the search value.. or "Text". I wanna customize it, to create different properties during runtime, and not to be stuck only in 2 bindable properties, so the component can bring more information and update (set value) more then 2 fields on the Binding Source.
I understand that I could create like 5 fixed parameters to be instantly linked to the binding field whenever I need more than 2 binds... but that would be sucks, that's the reason I'm asking help to create runtime bindable parameters...
Sorry mates! God please send me a message
|
|
|
|
|
What are you going to do with this "extra information" you want to "bind" to? Display it? How are you going to dispay it? Dynamically created Textboxes? If that's the case, each "new" TextBox would consitute a "new" Binding (and you would not be "stuck" with only 2 Bindings, as you put it).
You could even "hide" these dynamically created TextBoxes or whatever, if it buys you anything.
And besides ".Text", you can also bind to a Control's .Tag property, which can be stuffed with just about anything.
|
|
|
|
|
Yes, it's a textbox only to display information of a binding field, but what the component does is retrieve data from another datasource and sets the information to the binding field.
Therefore, I would have a textbox control binding the field "Total" (display only) and a custom dynamically created property in the component, binding to the same binding field "Total", the only difference is that the component is going to set the information of the Total Field and as BOTH are linked to the SAME bindingfield, the commom textbox will be updated too for display only.
|
|
|
|
|
I still don't get it.
Can't your component just set the Total property, to which the TextBox is bound?
Why does it need to have another property and bind this to the Total property? To do so dynamically it would have to know about (discover) the Total property anyway, so rather than create another property that also represents the total, and then bind this to the "real" Total property, and then set this other property so that the read-write binding causes the Total property to change... would it not be a lot easier to simply assign the Total property directly?
It may very well be that I haven't understood what you're trying to do. But if all you want to do is to automatically load the data from a foreign key into another data source, I cannot see how bindable properties comes into the picture at all. You just need to know what the foreign key value is and to what table it refers, and you could hold this information in any number of simple data structures, regardless of how it is obtained (mapping files, dynamically discovering relations defined in the database, attributes on the entity types, or whatever).
I will simply repeat this: Writing a bunch or relational logic and putting into a user interface component is not a good idea. It is no less work yet has many drawbacks compared to properly separating UI from business logic from data access. The problem you are trying to address, that of having to spend a lot of time on mundane data access code that to a large extent follows automatically from the data model (in most cases, the entities in the domain model largely correspond to the tables in the database), is one that many large and well organized groups of people have tried to tackle before you. Take a look at the Entity Framework, or NHibernate, or even CodeSmith templates that will generate a DAL for you.
|
|
|
|
|