|
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.
|
|
|
|
|
And your problem is ... ?
All I see is one or more TextBoxes that are bound to a "second" BindingSource (the "lookup" table) and a custom component that is used to "position" that BindingSource via a CurrencyManager / BindingManager.
You could even use a relation between your primary and "lookup" tables and bind to that, dispensing with the "custom component" altogether.
Are you binding to Typed DataSets or Entities / Entity Framework, or is everything just custom code / properties and no backing collection types? If the latter, that may be why you are finding things so difficult; you're not using the features that are available in the Framework.
|
|
|
|
|
Hi, I have a word file which contains numbers of shapes. Each shape contain some textboxes. I am copying that file to another location and assigning some values to those textboxes of those shapes. Now I have to assign the values to those textboxes then I have to ungroup those shape first then only I can assign values those text boxes. Now the problem is that when I ungroup those shapes, they change their locations in the document. I do not understand what is the problem. Here is the code
//copy the sourc file to another location
System.IO.File.Copy(Convert.ToString(source), Convert.ToString(destination));
Microsoft.Office.Interop.Word.Document obDoc = new Microsoft.Office.Interop.Word.Document();
object unknown = Type.Missing;
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;
obDoc = varWord.Documents.Add(ref source, ref unknown, ref unknown, ref visible);
obDoc.Activate();
//hiding a shape--working fine
obj = "shape1";
varWord.ActiveDocument.Shapes.get_Item(ref obj).Select(ref unknown);
varWord.ActiveDocument.Shapes.get_Item(ref obj).Visible = MsoTriState.msoFalse;
//ungroup the shape
obj = "Shape2";
varWord.ActiveDocument.Shapes.get_Item(ref obj).Select(ref unknown);
varWord.ActiveDocument.Shapes.get_Item(ref obj).Ungroup();
obj = "txtbox1";
varWord.ActiveDocument.Shapes.get_Item(ref obj).Select(ref unknown);
varWord.Selection.TypeText(txtbox1value);
//show the word file
varWord.Visible = true;
varWord = null;
Pankaj
|
|
|
|
|
Got the solution
I have used following code before ungrouping the shape
System.Threading.Thread.Sleep(1000);
Actually when c# code run its ungroup the shape first, then render the file, that's why shape is showing in the header of the page, not in the proper position. Now using the sleep method it renders the shape in proper place.
Pankaj
|
|
|
|
|
ha ha ... I think the process takes some time to ungroup. So sleeping the Thread cures the problem.
|
|
|
|
|
May be you are right. I have also posted the code for ungrouping. But there are no problem in ungrouping. Do you have any solution without using threading ?
Pankaj
|
|
|
|
|
Is there a way to discover which is the actual exception, without using the catch block?
For example:
Dispose() will be called if there is an exception or not.
I want to know if this Dispose is happening naturally, at the end of the block or by an exception that forced the end of the block.
My idea is simple test something like:
Exception.GetCurrentException() != null
But I don't know if there is somewhere to look for this.
|
|
|
|
|
Paulo Zemek wrote: Is there a way to discover which is the actual exception, without using the catch block?
This is the second question on this subject that makes no sense. The try/catch construct is there for a reason, so make use of it and stop your programs from causing havoc.
|
|
|
|
|