How can I manage caching in VB.Net? - vb.net

I have a scanned book review VB.Net app that displays two pages (TIF images) at a time and lets the user move forward or backward in the book. The books usually have between 100 and 600 pages and are stored on a network drive. It seems that the TIF images are being cached, because I can go backward in the book a lot lot faster than forward. The Task Manager also shows that the app is using more memory, the further I go in the book.
Does the VB app automatically cache files that it reads and displays?
If so, how can I reduce the amount of time a file spends in cache?
Public fileNames(10) As String
Public p As Integer ' page number of right-hand image
Public totalImages as Integer
' --------
Dim TNpath as String = "S:\Audit\CP_TN-123456"
Dim txtFiles = Directory.EnumerateFiles(TNpath, "*.tif")
totalImages = txtFiles.Count
ReDim fileNames(totalImages)
' -------- read all fileNames from the Directory
p = 1 ' when starting a new book, the right-hand image is 1
' -------- moving right in the book
p = p + 1
Me.PicBoxRight.Image.Dispose()
Me.PicBoxRight.Image = Nothing
imgRight.Dispose()
' -------- loading next Right image
fileRight = TNpath + "\" + fileNames(p)
imgRight = Image.FromFile(fileRight)
Me.PicBoxRight.Image = imgRight
Note: Book pages are scanned at 300 DPI and saved in single image TIF files. Each color page is about 25MB and each black/white page is about 8MB. So cache fills up quickly. The app reads files, but does not write files...

Related

How to use images from external DLL on VB.NET desktop app

I got problem with resources. My project have 400 small images for status indicator. All images are set in 4 groups (4 different folders). Group are identified by My.Settings.imagevalue parameter. Indicator can give value from 1 to 100. Every new indicator value are displayed in: MainForm.indication_Value.Text .
As I have so many images, I would like to use dll with images as external resource. I have putted all images in 4 dll's, 100 images on each one. On top of DLL structure are RCData, where are set all images by value 1 to 100. I plan to use same DLL's for few apps seamlessly.
I use very simple code on text changes to change image:
Private Sub preview()
Dim indication As String
indication = MainForm.indication_Value.Text & ".jpg"
Dim resource As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, My.Settings.imagevalue, indication)
If IO.File.Exists(resource) Then
indication_preview.Image = Image.FromFile(resource)
Else
indication_preview.Image = My.Resources.Resources._error
End If
End Sub
How can use images from externa DLL with logic I have created before? I really don't want to write messy code for each image.
Thanks in advance for response!

Opening multiple explorer.exe windows with different sizes and locations

Im rather new to vb.net and have encountered following issue:
Im trying to open up 5 different folders in 5 different explorer windows. All of them should be opened in a fixed position and with a fixed size.
Lets say Folder1 should be at (5,5) with a size of (100,150)
Folder2 should be at (50,5) with a size of (80,75)
Folder3 should be at (100,5) with a size of (100,50)
Folder4 should be at (150,5) with a size of (35,90)
Folder5 should be at (200,5) with a size of (50,35)
I've found this method online to move ALL instances of explorer.exe to a fixed position and resize it -> https://social.msdn.microsoft.com/Forums/vstudio/en-US/f4effba6-ed9b-4e4d-a163-922fc158fdfd/open-file-explorer-and-change-its-sizelocation?forum=vbgeneral
Sub MoveAllExplorerWindows()
Dim ExplorerFileName As String
For Each ExplorerWindow As SHDocVw.InternetExplorer In New SHDocVw.ShellWindows()
ExplorerFileName = Path.GetFileNameWithoutExtension(ExplorerWindow.FullName).ToLower()
If ExplorerFileName.ToLowerInvariant() = "explorer" Then
ExplorerWindow.Left = 0
ExplorerWindow.Top = 0
ExplorerWindow.Width = Screen.PrimaryScreen.WorkingArea.Width / 2
ExplorerWindow.Height = Screen.PrimaryScreen.WorkingArea.Height
End If
Next ExplorerWindow
End Sub
But I want to have, depending on the actual folder, a different location and size. The code above would resize every explore.exe window and put it in the location (0,0).
Does anyone have any idea how I could change the code or implement something completely new?
Thanks in advance! <3

Sort ZipArchive by FullName VB.net

I'm working with ZIP Files and I'm using the following code:
Dim archive As ZipArchive
archive = ZipFile.OpenRead(Filelocation)
Do While (stopLoop)
entry = archive.Entries.Item(counter)
If (entry.FullName.Contains(".jpeg") Or entry.FullName.Contains(".jpg") Or entry.FullName.Contains(".png")) Then
stopLoop = False
Else
counter += 1
End If
Loop
These zip files contain images and documents but the images are in a specific order. The user can flip through the images and sometimes my code works perfectly fine (images show 1 throught 10) but other times, the order is completely garbage (images show 2,9,3,7,8....). I understand that the entries are based on when they were added into the archive but is there anyway to sort these by fullname?

Relative path to images in access reports

It is my first time building a database and I wanted to share a solution to a problem I encountered.
My problem was that I wanted to show different images for each record in a report, but I also wanted to be able to move the database. This was a problem. I search in all the forums and all the different solutions didn’t work. I also found an article written by Microsoft saying that the only way is to either store the full path to the images or to store the image in the database. But this causes a problem if the database is moved, or storing the images in the database will take up a lot of storage space.
The problem is that the codes doesn’t work for each record in the report, the codes are for the entire report. So writing codes to find the backend and the image folder would result in displaying the first image in the report for all the records in that report.
However I discovered, when only storing the name of the image in a table, it would sometimes work (but it shouldn’t have, because I didn’t have the path) but when I restarted the database it would stop working. Investigating further I discovered that whenever you open the file browser it will store the path in some kind of memory. As long as the path to the images is stored in the memory it will be able to link the images to the path.
So my solution…
When the form, from where you access the reports is opened, the file browser is opened and the path to the images is pasted in (using codes to find backend and the image folder) and then the browser is closed. And this creates a link to the image names (stored in a table) with the path. And each different images will be shows for each different records in the report.
Not a pretty solution. Whenever the form is opened, you will see a flash of the file browser. But it gets the job done.
In the load form event:
`' this will find the backend and the image folder:
Dim filepath As String
Dim strBackEndPath As String
Dim lenPath As Integer
Dim i As Integer
Dim j As Integer
strBackEndPath = CurrentDb.TableDefs("yourTabeInBackend").Connect
j = InStrRev(strBackEndPath, "=") + 1
strBackEndPath = Mid(strBackEndPath, j)
BackPath = Left(strBackEndPath, InStrRev(strBackEndPath, "\"))
filepath = BackPath & "YourImageFolder\"
'this will open the folder browser and paste in the path and close it:
Dim f As Object
Set f = Application.FileDialog(msoFileDialogFolderPicker)
Dim varFile As Variant
Dim strPath As String
Dim fileName As String
With f
.InitialFileName = (filepath)
.AllowMultiSelect = False
SendKeys "{ESC}", True
f.Show
For Each varFile In .SelectedItems
Next varFile
End With
`
You can move the pictures to a subfolder of the folder of your database.
Then save the pictures' names like this:
Picture1.jpg
Picture2.jpg
etc.
When you run the application, obtain the path to the pictures:
PictureFolder = CurrentProject.Path & "\FolderName\"
Then the path to a picture will be:
PictureFolder & Me!PictureFileName.Value
When you "move" your database, move both the database file and the folder with the picture files with it.
yup, i just encountered same problem and i agree with what Richard_Ha said, but in my case i solve it with storing image path on textbox and its work..
first textbox name "fileimage_txt" and bound to list of image filename on table
second textbox name "image_path" with property
Source Control : =GetImagePath()
visible : No (if u dont want it get printed)
and image control with property
Source Control : =[path_txt] & [fileimage_txt]

program logic to speed up processing

I have setup an application to parse about 3000 files a day where each contains around 4000-5000 posts in xml format with like 100 fields.
It involves a lot of cleanup and parsing but on average it takes around 6 seconds per post. Now I tried threading but because of the way I have everything set up with variables being overwritten etc...I have divided the files into different folders and just created copies of the program to run and access the assigned folder. It is running on a windows 2008 server with 16 G of memory and I am told I need to reprogram to speed up the process and also not use so much memory.
Does anyone have any suggestions or does this process I have set up seem fine? I am the new guy and literally everyone thinks I am an idiot.
For i As Integer = 0 To fileLists.Count - 1
Do
Try
If Not completeList.Contains(fileLists(i).ToString) AndAlso fileLists(i).EndsWith("xml") Then
If fileLists(i).Contains("Fa") Then
inputFile = New StreamReader(fileLists(i))
data = String.Empty
infile = fileLists(i).ToString
swriter.WriteLine(infile.ToString)
swriter.Flush()
Dim objFileInfo As New FileInfo(fileLists(i))
fileDate = objFileInfo.CreationTime
Dim length As Integer = objFileInfo.Length
data = inputFile.ReadToEnd
If Not data Is Nothing Then
parsingTools.xmlLoad(data)
tempList.Add(fileLists(i))
completeList.Add(fileLists(i))
End If
inputFile.DiscardBufferedData()
End If
End If
End If
Ok I am not sure what code to post because there is literally a lot of code. The above is the main module and onece it reads in data it tries to load it into xml document, if it fails it parses it using ordinary text parsing. It navigates to each field I need to extract and also connects to a couple of web services to get more content before all this is added together to create a new xml file.
manager.AddNamespace("x", "http://www.w3.org/2005/Atom")
manager.AddNamespace("a", "http://activitystrea.ms/spec/1.0/")
Dim nodecount As Integer = xmlParser.getNodesCount(navigator, "x:entry", manager)
For i As Integer = 1 To nodecount
statid = xmlParser.XPathFind(navigator, "x:entry[" & i & "]/x:id", manager)
contentDate = xmlParser.XPathFind(navigator, "x:entry[" & i & "]/x:published", manager)
template = xmlParser.XPathFind(navigator, "x:entry[" & i & "]/x:title", manager)
title = xmlParser.XPathFind(navigator, "x:entry[" & i & "]/x:source/x:title", manager)
ctext = xmlParser.XPathFind(navigator, "x:entry[" & i & "]/x:summary", manager)
htext = xmlParser.XPathFind(navigator, "x:entry[" & i & "]/a:object/x:content", manager)
author = xmlParser.XPathFind(navigator, "x:entry[" & i & "]/x:author/x:name", manager)
authorUri = xmlParser.XPathFind(navigator, "x:entry[" & i & "]/x:author/x:uri", manager)
avatarUrl = xmlParser.XPathFind(navigator, "x:entry[" & i & "]/a:author/x:link[#rel='avatar']/#href", manager)
Next
The problem with something like this is the hard drive itself - depending upon many factors it can act as a funnel and essentially constrict the number of files you are able to interact with on the drive concurrently.
With that said, I'd highly recommend you take a look at the TPL (task parallel library) in .NET v4.0. It's a framework that vastly simplifies the act of "spreading the work across all the available cores" of your processors. My computer has dual processors, each with 4 native cores (Intel Xeon's # 3GHz) which gives me 8 cores. I have an application that downloads ~7,800 different URL's off the net and analyzes their content. Depending on the values it finds it will do some additional processing then store the results. This is somewhat similar to your situation in that we both share a restricting resource (for me it's the network) and we have to manually parse and evaluate the contents of the files we're working with.
My program used to take between 26 to 30 minutes (on average) to process all those files. This was using a properly implemented multithreaded application. By switching the code over to the TPL it now only takes 5 minutes. A HUGE improvement.
Take a look at the TPL and plan on having to make some changes to your code in order to maximize the potential improvements. But, the payoff can be fantastic if done correctly.
Does the application run continuously as a service or is it something you run once/or few times a day? If it doesnt run continuously you could try that and limit the processing to a few concurrent threads.