Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#ASP.NETGeneral
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,
Posted 2-Oct-12 19:06pm
Edited 2-Oct-12 19:57pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Anusha Sridhar at 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 at 3-Oct-12 2:37am
Then put your code inside <code>try{}...catch(){}</code> block. If any IO exception comes then show the message to user that <i>"File is used by another application, try again later"</i>.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

hi Anusha,

You cannot do this unless the other entity which has the file open allows for shared reads.
for more ref:[^]
Sarrrva at 3-Oct-12 2:14am
Another one thread, Regards sarva
Anusha Sridhar at 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 at 3-Oct-12 2:31am
can you show me some sample? regards sarva
Sarrrva at 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
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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..

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

  Print Answers RSS
0 Dnyaneshwar@Pune 994
1 Sergey Alexandrovich Kryukov 826
2 OriginalGriff 448
3 Tadit Dash 390
4 sanket saxena 323
0 Sergey Alexandrovich Kryukov 11,800
1 OriginalGriff 7,225
2 Peter Leow 5,009
3 Abhinav S 3,893
4 Maciej Los 3,575

Advertise | Privacy | Mobile
Web01 | 2.8.140421.2 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid