Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB Expert!

Join 244,289 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 986 people online right now. Registration is fast and FREE... Join Now!




How to Allocate Memory to my Program

 
Reply to this topicStart new topic

How to Allocate Memory to my Program, Slow Program Usage on Text Files

mjsmikey
5 Jan, 2009 - 10:11 AM
Post #1

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 16

I wrote a program that takes text and strips out certain characters LINE by LINE and saves them to a new text box.

The problem I am having is since this is a text editor it tends to take a while. The computer is barely using any computing power to run the program, as I can continue to do anything I want while this program is sifting through my 10,000 lines of text. The program will show the "(NOT Responding)" at the top when it is actually functioning.

Is there a way to allocate more memory or CPU usage to a program so that it will process Simple but LONG tasks faster?

The computer being used is Core 2 Duo - 2.4GHz x 2 with 2046 MB Ram and 3000 MB of page file.

If this cannot be allocated is there a way to stop the computer from showing "(Not Responding)" so the progress bar will continue to function as the text is processed?

Thanks,

MIKE

User is offlineProfile CardPM
+Quote Post


reyben_89
RE: How To Allocate Memory To My Program
10 Jan, 2009 - 01:46 AM
Post #2

D.I.C Head
**

Joined: 30 Nov, 2008
Posts: 54



Thanked: 2 times
My Contributions
do you mean do you load the 10k line of texts when the forms is loaded in splash form before the actual program loaded but the the error is the program alert you that the program didn't responding even though your pc is
have a high specification.
User is offlineProfile CardPM
+Quote Post

mjsmikey
RE: How To Allocate Memory To My Program
13 Jan, 2009 - 05:49 AM
Post #3

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 16

QUOTE(reyben_89 @ 10 Jan, 2009 - 01:46 AM) *

do you mean do you load the 10k line of texts when the forms is loaded in splash form before the actual program loaded but the the error is the program alert you that the program didn't responding even though your pc is
have a high specification.

I'm not sure if I'm loading in Splash, as I don't know what that is. But what I think is happening is when I call read line X from 300 to 1400 and write lines to RichTextBox it is appearing to read lines 1,2, .. 300, 301, 302 up to 1400 to write only line 1400. The 10K Lines of Text take longer and longer to process as the system is writing each line to memory.

I could do three things, but i'm not sure how or if I can.
1: Allocate a Huge Chunk of Ram (Its functioning at about 42Mb Buffer). It needs much more.
2: Stop the Read from Reading Line 1,2, ... 10000 to write lines 10000 by keeping the entire read text in memory so that It doesn't read it 10000 times for changes
3: Empty the Buffer at every Read Loop... at 10000 line Text File is only 1 MB so it would be allot faster to dump the buffer.

Can I do any of these?

How?
User is offlineProfile CardPM
+Quote Post

KylBlz
RE: How To Allocate Memory To My Program
14 Jan, 2009 - 06:27 PM
Post #4

New D.I.C Head
*

Joined: 14 Jan, 2009
Posts: 17



Thanked: 2 times
My Contributions
QUOTE(mjsmikey @ 13 Jan, 2009 - 05:49 AM) *

I'm not sure if I'm loading in Splash, as I don't know what that is. But what I think is happening is when I call read line X from 300 to 1400 and write lines to RichTextBox it is appearing to read lines 1,2, .. 300, 301, 302 up to 1400 to write only line 1400. The 10K Lines of Text take longer and longer to process as the system is writing each line to memory.

I could do three things, but i'm not sure how or if I can.
1: Allocate a Huge Chunk of Ram (Its functioning at about 42Mb Buffer). It needs much more.
2: Stop the Read from Reading Line 1,2, ... 10000 to write lines 10000 by keeping the entire read text in memory so that It doesn't read it 10000 times for changes
3: Empty the Buffer at every Read Loop... at 10000 line Text File is only 1 MB so it would be allot faster to dump the buffer.

Can I do any of these?

How?


just throw in this code:

CODE

sub stuff()
do
'code for loading from 1 line
application.doevents() 'sweet, sweet method happy.gif
loop

it will make the loop slightly slower, but it will make it so the app can multitask. also you might want to try multithreadding. drag a "background worker" onto your form and do this:
CODE

private sub backgroundworker1_dowork(byval blah blah blah it makes this code for you) handles backgroundworker.dowork()
do
'code for loading from 1 line
loop
'there is no need for application.doevents because this process is a seperate process from the form

When you want to run the method [in a button press or the form's load sub] just call backgroundworker.dowork().
User is offlineProfile CardPM
+Quote Post

mjsmikey
RE: How To Allocate Memory To My Program
15 Jan, 2009 - 05:36 AM
Post #5

New D.I.C Head
*

Joined: 18 Dec, 2008
Posts: 16

I have tried to implement this MULTI-Threading code but... I do not know how. Since I don't think this is properly related to my code.
--Problem Code--SLOW--

Document. is a RichTextBox that I load with a TextFile just for visual reason.

TextBox1 and TextBox2 are the Strings I'm searching Line by Line for.

StartLine is How many Lines I want to copy above this string

Ending is How many Lines I want to Copy Below this String

LineCount is Total Number of Lines in the Document

EndLine is the 75th line, this is so I don't copy lines I already copied.

CODE
Private Sub Strip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Strip.Click
        RichTextBox1.Text = String.Empty
        RichTextBox1.Text &= "(THIS DOCUMENT HAS BEEN MODIFIED WITH STRIP-N-SAVE)" & vbNewLine
        Dim endLine = CInt(TextBox5.Text)
        Dim LineCount As Integer = Document.Lines.Length
        Dim randarray(0 To LineCount) As Long
        ProgressBar1.Maximum = UBound(randarray)
        Dim StartLine = CInt(TextBox3.Text)
        Dim Ending = CInt(TextBox4.Text)
        Dim J, L, kk As Integer
        'Correcting Input if Document too Short
        If LineCount < endLine - 1 Then
            endLine = LineCount
        End If
        'Copying First Lines At all times
        For x As Integer = 0 To endLine - 1
            RichTextBox1.Text &= Document.Lines(x) & vbNewLine
            J = x
        Next
       For x As Integer = endLine To LineCount - 1
            If Document.Lines(x).Contains(TextBox1.Text) Or Document.Lines(x).Contains(TextBox2.Text) Then
                L = x - StartLine
                kk = L
                If kk <= J + StartLine Then
                    L = J + 1
                End If
                For k As Integer = L To x + Ending
                    RichTextBox1.Text &= Document.Lines(k) & vbNewLine
                    If LineCount <= k Then
                        Exit For
                    End If
                Next
                x = x + Ending
                J = x
                ProgressBar1.Value = x
                'Stop if Document is Too Short
                If x >= LineCount - 1 Then
                    Exit For
                End If
            Else
                If x >= LineCount - 1 Then
                    Exit For
                End If
            End If
            'Stop if Document is Too Short
            If x >= LineCount - 1 Then
                Exit For
            End If
       Next
        ProgressBar1.Value = LineCount
        Try
            My.Computer.Audio.Play("C:\WINDOWS\Media\notify.wav")
        Catch ex As Exception
        End Try
        MsgBox("Document Complete - Press Save", vbOKCancel)
    End Sub

This runs really really slow...

I also tried to change the loops to

CODE
                    RichTextBox1.Text = String.Join(vbNewLine, Document.Lines, L, x + Ending)
                    If endLine <> UBound(Document.Lines) Then
                    J = x + Ending - 1
                    End If

This will write the lines faster then the other Loop Method but, the writing isn't the big slow down... its the Read using Loops. Also it creates other problems, I want to add a text line to the beginning of the document saying it has been modifed, String.Join doesn't let me use "TEXT".

NEXT...

I attempted to use this but I cannot get the code to read correctly as it throws exceptions for Dim d as New.
I'm guess that document.Text isn't new since I already loaded it using the streamreader, but I'm not sure.

Also I'm tring to get this to output to RichTextBox1, and this doesn't do that.
CODE

dim curLine as string
dim oldLine as string
dim bFlag as boolean
Dim SavedLines As New Stack
dim fs = New FileStream(document.text,FileMode.Open,FileAccess.Read)
Dim d as new StreamReader(fs)
d.BaseStream.Seek(0,SeekOrigin.Begin)
while
d.peek()>-1    
if bflag = true then          
bflag = false          
SavedLines.push(d.readLine())    
else          
oldLine = curLine          
curLine = d.readline()          
If curLine.Contains(TextBox1.Text) Or curLine.Contains(TextBox2.Text) Then
bFlag = true              
SavedLines.push(oldLine)              
SavedLines.push(curLine)          
end if    
end if
End while
d.close()

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 03:37PM

Live VB Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month