Copy to clipboard limitation - c#

As many other question about this topic, I still didn't find something that could avoid my issue.
I'd like to know what are the limitations of the clipboard in term of copy paste processing in time and restrict its copy paste functionality for specific usage and program ID.
Because, I've been developping a software for my company that takes a template in Word/Excel and PowerPoint to make auto generated reports by replacing the picture every day and pasting the new graph and the picture which are in the template.
But, I'm afraid, because it seems to exist some limitations about the copy to clipboard
CopyBitmapToClipboard
ActiveWindow.View.PasteSpecial()
When I copy a table/image into my clipboard and paste it in Word/Excel/PowerPoint it is fine, but if I have now 10 parallel reports which are generated at the same time and use the mechanism of copy paste, it will happen that it will copy it in the Word/Excel/PowerPoint that is currently being used by another report.
Taking in consideration, I have 1 document, and the copy to clipboard of a table that is huge into powerpoint, would take the best case 1 second( which doesn't ), this would mean that in 1 day, I can generate a maximum of
1 copy paste procedure = 1 second
1minute = 60 second means 60 copy paste
1hour = 60min x 60 copy paste
1day = 24hours x 3600 copy paste
Means I have a total of 86400 copy paste/reports. Which is impossible. A document will never take minimum of 1 second to be complete( for reports which are about 20 pages, powerpoint with 30 slides, excel worksheet with 6sheets ). How to avoid that the content copied into clipboard will be pasted in the wrong document, since both documents use the paste fonction.
So, I'd like to know, if it is possible to give a reference into my clipboard, to tell him, to copy only the content inside his word/excel/powerpoint and not to copy what I just copied doing CTRL+C and to do CTRL+V in the wrong document?
At the moment, we have about 50 reports daily to generate( taking in consideration that few months ago we had 2 reports to generate ), which are scheduled in interval of 5 minutes, but what will happen the day we will have 86400 daily reports to generate? The copy paste fonction will screw up all reports and paste in the wrong report. I might not be there to assist it, but I'd like to prevent that impact.
I was writing my software in C#/.NET, but I can't write inside it to catch if a current schedule/report is being generated, because I run a .bat that does the creation of the report with the program I wrote.
**Edited:
Content of my .bat is:
myprogram.exe /objectsourceprogram="" /sourcefile="my template.doc/xls/ppt" /destinationfile="my destination.doc/xls/ppt"
I'm only scheduling my batch file, the only thing that can be done is, waiting that one scheduling is being completed before starting the next one, but this will create intervales

I've dealt with auto-generated reports in Word and Excel as well. You're doing it the slow way, no wonder it's slow - .NET COM interop is quite slow as it its and then your process utilizes other threads, so now you have to take marshalling and synchronizing between threads since Office is single-threaded.
Copy/paste is a high-level, user function and you can use it as a hack but relying on it as a primary way of executing your task is a poor choice.
Better choice: Office APIs (VSTO, anyone?) - I'm pretty sure whatever you've come up with in terms of reports is reproducible through APIs. Beware - too many API calls and your functions can work slower.
Best choice: OpenXML SDK. The learning curve here will likely be steep since you're still using Copy/Paste but the payoff is that your document processing time will be reduced a couple orders of magnitude.

Related

Traversing all files in windows pc and create report due to creation date in an efficient way in C#

My client wanted me to write a simple desktop app such that :
User will select a date. Then the program will travel all files in the windows pc and then create an excel report for all files which are created before the selected date.
Actually it seems really simple application. However, my client told me he has petabytes of data which made me think the app would run for hours even days. I need a really smart solution for that. For instance despite the possibility of a crash or unexpected error etc, I am planning to divide the report into parts and create a new excel file for each 50000 records. In addition, the application should continue from where it left off. In another words, if the application is closed, the application should not start scanning all over again. What is the most logical way for the program to work efficiently and continue from where it left off? What comes to my mind is to put a tick on the folders so that the already looked folders are not looked at again. What is the best way to implement this logic in C#? Is it efficient to create a separate list for the visited folders and then first look if the folder is alreay in list? Which data structures would you use for this problem and how would you implement this?
Thanks.

Duplicate PrintJob.JobID on PrintQueueWatch .Net library

First of all, I work on windows XP, excel 2003, VS 2010, .NET 4.0 and I gather information of every print using PrintQueueWatch .Net library.
What I have seen so far is, when I print an excel active sheet with more than one copies jobdeleted event fires more than one. What I expected was that because I print one document one jobID would be made for the whole session but job deleted is fired for every single copy. So If I print one page with 3 copies I get at least 3 jobIDs. The real problem is now. When I print one page with 3 for example copies after the first copy the other 2 copies get twice in jobdeleted so I get exactly 3 unique PrintJob.jobID but 5 times(18, 19, 19, 20, 20).
My question is why job deleted is fired more than once in a print session and especially why in every copy.
The Microsoft Office applications actually do some behind the scenes stuff to take over from the standard printer libraries when handling copies. This allows them to, for example, offer page collation and copy collation options that the printer driver themselves do not offer.
They do this by (in many circumstances) creating a separate print job for each copy and these then process in the usual fashion with their own print job id, using the JOB_INFO_3 structure.
When a job is deleted but it still has linked jobs not yet deleted it raises the event but actually does not delete. Only when the whole chain is deleted does the actual delete function complete.
Unfortunately I never coded any handling of JOB_INFO_3 but should you wish so to do the code is on GitHub.

When updating a text file via code, is the entire file re-saved, or just the parts?

Okay so my overall goal is to create a UWP notes app that doesn't require the end-user to manually save each note they write; this would be done automatically for them.
So what I'm looking to do is create a C# class that will detect changes to the document the user is currently writing and constantly update the underlying text file (This will eventually be written to a row within the database, but I hear it is less efficient to constantly update records within a DB than to deal with text files for this matter?).
But yeah, this is pretty much what apps like OneNote do in the background for the user, and so the user have to never worry about saving the file or losing data in situations where the computer loses power or the app terminates unexpectedly.
So if I created a class that detected changes to the document and then update the underlying file, is the WHOLE file rewritten or just the particular parts (bytes?) that were changed within (or appended to) the text?
I'm just looking for the most efficient way to constantly update a file because if a user is a fast typist, the system will have to be able to keep up with every single keystroke input.
Last, would the entire file have to be rewritten if the user makes random changes to the text at random locations (rather than append to the end of the file)? Does any of this even make sense. I tend to write a lot to ask a simple question. I have problems....
I would do a timertick event and have it automatically save every 3 to 5 seconds. I do this alot. I understand what your doing, but automatically saving every key stroke, would be putting a lot of stress on the program.
I would automatically save every few seconds on a if basis,
If a change is detected then it will save. Think about this answer, it hs been saved almost 100 times if done by keystroke.

Programatic truncation of large file names (zipped) for different Windows editions

So I'm bugfixing on a program that allows a user to create an Excel document, which it first zip compresses before serving to the customer. One of the client complaints is that, while the zip file can always be downloaded, sometimes the user must copy the Excel file out of the archive (or extract it) before opening it, using the standard Windows compressed tools. Standard "File name too long" error.
The algorithm is, of course, setting the .zip archive name to the same as the report - which can be 100+ character long strings.
The solution I'm implementing is to check the length of the potential report to see it if it violate MAX_PATH, naturally, and truncate the .zip name as needed.
Testing it on Windows 7, this works perfectly. But something odd happens when testing it under 8.1
It still throws an error trying to open from the archive - but this error is a bit more enigmatic.
"Sorry, we couldn't find C:\Users{My user name}\AppData\Local\Temp\Temp1_{Rest of the truncated archive name}.zip{Full file name}.xlsx. Is it possible it was moved, renamed or deleted?"
This error keeps popping up, regardless of whether the file is "Open"ed or "Save"d from the browser.
Normally, I'd just try further tweaking, but testing on the Windows 8.1 platform involves a lot of overhead at the moment, and it doesn't look like the problem is the path name length.
What is going on? Does Windows 8 have problem with the length of names within archives, or...?
Also, the reason I am posting this here, and not, say, User Experience, is because I feel like the solution will be programmatic - something in the code of the program. I readily concede that "allow the end user to skip hitting the Extract button" is about providing an easy user experience, but truncating the length of the entire path solved the problem on Windows 7.
Just not 8.1, and googling/searching the SO site family provides no help - ironically, because of the keyword Excel.
So, as I found in the link above (http://answers.microsoft.com/en-us/office/forum/office_2013_release-excel/sorry-unable-to-find/595333d0-1463-499f-967e-4da8ac2e2047?auth=1) the crux seems to be that, although MAX_PATH is 260 characters, Excel 2013 can't handle anything over 212.
I still haven't been able to give this the rigorous testing it deserves, but if anyone else encounters this problem, and finds this page in their quest for a fix, just truncate until the entire path is under 212 and you should be good.

Keeping the last X minutes of a recording video

I'm developing an app which requires me to record a lot of video data but I don't need to store them all.
What I need to do is to keep just the last X minutes of the recorded stream.
This means that I need a way to remove the oldest sample everytime that I need to store a new one.
So I started working with this article: http://msdn.microsoft.com/library/windowsphone/develop/hh394041%28v=vs.105%29.aspx
The first idea that I had was to just call the StopVideoRecording() and then the StartVideoRecording() using a timer each X minutes.
Now, at first this made sense but it won't work.
The problem is that doing this way will delete the previous data each X minutes.
This means that, if we record 12 minutes and we need to keep the last 5, following this idea we'll delete the first 5 and then the second five, leaving just the last 2 minutes and this is not what I was looking for.
I moved then my attention to the VideoSink class because of the OnSample method.
This seems pretty simple, we intercept every sample and we store it in a fixed size byte array (the size depends on the needed length and the sample's size).
When the buffer is full we just shift everything on the left before adding the new sample.
The problem is that a test video of just 1 minute generated something like 2GB of samples and this makes this way really hard to manage.
I know that those samples are uncompressed, but wouldn't be hard, for a smartphone, to get a sample, compress it, shift a big array, insert the sample and write the array to a file and do it on EVERY sample received?
Yeah, I'm talking about writing the array to a file because we need to persist this video somehow. It may happen that the battery stops working, and having it just in RAM will let us loose everything that we recorded!
The last idea that came to mind was to use a combo of VideoSink and FileSink.
While the FileSink does the compression magic (I even decompiled this class to understand what it does but there's no code inside!), we use the VideoSink's OnSample method to manually remove the unneeded data from the mp4 file used by the FileSink.
This one sounds quite hard because I don't know if I can write to the file with both FileSink and VideoSink without concurrency issues, and I've not found a good c# library to help me working with the mp4 files without having to deal with its structure.
The only library that I found is this one http://basemedia.codeplex.com/ but it totally lacks documentation (each link in the documentation page gives a 404 error).
I'm starting to think that this is something that can't be done, but I'd like to see if there's someone here which can point me to the right direction.
EDIT:
Just to be clear, I used the "recording" word and not the "recorded" one beacuse I'm talking about trimming the video while it's still recording!
This is not about editing it once it has been saved, but something more like removing stuff from the stream while I'm writing it to disk.
I cannot provide a code to you but just an idea. Because you have requirements:
I need to do is to keep just the last X minutes of the recorded stream.
Target platform is windows phone 8
I want to add some modifications to your first idea:
Write each minute of video stream in the separate file
Also you need to leave +1 video file more then a number of minutes. For instance if you need 5 minutes you should always keep 6 files because last file may be not full.
By use DirectShow you will able to join this files into one. Be ready to use C++ (As alternative to this you can use some service or make own back-end for this)

Categories