Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...
My task is to generate Word documents. By selecting the appropriate options, we can generate a document. I have the base documents in the server, and if I click generate, then the document will be generated and we can save it on our local drive.

If I am using the portal now and, at the same time, if some one is using it in their machine at the same time, then the document is not getting generated. Its automatically getting posted back.

I want to show a progress bar or something like that, so that the person waits until the document is completely generated.

Is this possible using threads?
Please Help with codes,

Thanks in advance,
Anusha
Posted
Updated 2-Oct-12 19:57pm
v2

C#
public void UpdateTOC(string file1, string file2)
        {
            using (var mainDoc = WordprocessingDocument.Open(file1, false))
            using (var resultDoc = WordprocessingDocument.Create(file2, WordprocessingDocumentType.Document))
            {
                // copy parts from source document to new document
                foreach (var part in mainDoc.Parts) resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId);
                // perform replacements in resultDoc.MainDocumentPart   // ... }
            }

        }


This is the code I am using.. File1 is the base document which will be in the server and file2 will be the document that will be generated.. both the documents have unique names..
 
Share this answer
 
hi Anusha,


You cannot do this unless the other entity which has the file open allows for shared reads.

for more ref:

http://stackoverflow.com/questions/897796/how-do-i-open-an-already-opened-file-with-a-net-streamreader[^]

regards
sarva
 
Share this answer
 
v2
Comments
Sarrrva 3-Oct-12 2:14am    
Another one thread,

http://bytes.com/topic/c-sharp/answers/236171-reading-file-already-open-another-process

Regards
sarva
Anusha Sridhar 3-Oct-12 2:17am    
Hi Sarva,
Thanks for the link..
But I already tried the FileShare method and its not working..
Is it possible to do with theread.. Any idea about that?
Thanku. :)
Sarrrva 3-Oct-12 2:31am    
can you show me some sample?
regards
sarva
Sarrrva 3-Oct-12 3:15am    
you can try with thread mechanism,

Process.Start() & Process.WaitForExit()& Process.End,
Monitor.TryEnter, lock, using, and Deadlock
regards
sarva
If file is being used by other process then you need to kill the process first before using the file. You need to see How to kill a process[^].

For some process, you'll require admin privileges to kill it. You'll have to take care of that also.

--Amit
 
Share this answer
 
Comments
Anusha Sridhar 3-Oct-12 2:24am    
Hi Amit.. I don want to kill the other process..I want to wait till the other process completes. Because many people will be accessing the portal to generate the documents. I think killing wont be a good idea..
Thabks for the response. :)
_Amy 3-Oct-12 2:37am    
Then put your code inside try{}...catch(){} block. If any IO exception comes then show the message to user that "File is used by another application, try again later".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900