Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

My app' gathers information from lots of large text files which can take quite a while. I have managed to make this process as quick as possible but I have two questions...

 

1) During the compile process the user is given progress feedback and other info in a scrollable textbox like most app's of its kind. Thing is... If I try to scroll that box while the process is still running the app' hangs. Infact if I try to do anything else at all (minimise, run another program etc...) while the process is running the app' hangs. What can I do to prevent this?

 

2) I have put a check box on the form that will allow the user to choose whether the app' has low or normal CPU priority (labelled 'Run the in background') but I have no clue how to go about this so some pointers would be appreciated.

 

Cheers,

Andy.

I'm getting the hang of this now... No really I am!
  • Administrators
Posted

for question 1 have a look at the BeginRead method of the stream class. This will allow you to handle the reads asyncronously (sp?) and prevent blocking in your UI.

 

as for question 2 something along the lines of

system.Threading.Thread.CurrentThread.Priority=Threading.ThreadPriority.BelowNormal

 

should do it.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Ok question 2 is sorted. Cheers for that!

 

I forgot to mention that although mostly the program hangs if I try to do anything else, occasionally it won't even let me scroll or minimze it. If for example I click the minimize button, the app' doesn't minimze until compiling is complete.

I'm getting the hang of this now... No really I am!
Posted

You could try running your various file processes in a seperate thread. That way you can be sure that your window won't be held up by the other processes. You can then set the priority to Lowest for this thread but keep your program on 'normal'.

 

Handling multiple threads can get tricky, especially when prematurely killing a thread, but it's extremely powerful.

 

 

There's a great section in the MSDN library about using threads and threading (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconusingthreadsthreading.asp). Have a read of that and try playing around with creating your own threads. I'm currently learning this for my own application (I've got a function that scans the harddrive for specific files) so if you've got any further questions I might be able help out.

Posted

Excellent! Cheers!

 

Seems to run great now. However, because this is the first time I've used threads I'll run through what I did...

 

Publicly declared the thread...

Public CompileThread As System.Threading.Thread

Then created a new instance of the thread when the 'Start Compile' button is pressed and started it immediately...

CompileThread = New System.Threading.Thread(AddressOf CompileLogs)
CompileThread.Start()

Where CompileLogs is the sub with all the code in.

 

At the end of that sub I stop the thread...

CompileThread.Abort

Like I said this works a treat but I'm just wondering if there's something else I need to do to avoid any as yet unencountered probs.

 

Thanks again!

I'm getting the hang of this now... No really I am!
Posted

Looks good to me. You can set priority with CompileThread.Priority = [whatever].

 

Note than when you do Thread.Abort, then an ThreadAbortException is thrown in the thread. This can be handled but not supressed in the function, so you'll need to have something like:

 

Try
          CompileThread.Abort
Catch e as ThreadAbortException
          'Everthing's ok, thread abort exception
End Try

 

Another useful command is Thread.Join. This waits for the thread to abort (doing Thread.Abort isn't instant(. Calling Thread.Join after doing an abort will hold the code until the thread has sucesfully been terminated.

 

Have fun!

Posted

There's always something isn't there?...

 

As I said, I thought everything was working perfectly but there is a problem after all.

 

When I try to close the app' at any point after running the CompileLogs sub in the CompileThread described above it throws an 'Object reference not set to instance of object' exception at me on the line...

MyBase.Dispose(disposing)

At first I didn't think it was anything to do with the thread because I have another process which runs in a different thread (not at the same time) which does not produce this problem.

 

Anyway, I removed the CompileThread and called the subroutine normally and the error disappeared so obviously it is but not knowing enough about threads I'm buggered if I know why it does it after running one sub but not the other.

 

The only difference in the subroutines is that CompileLogs does a lot more with the data from the logfiles and runs for a lot longer than the other one.

I'm getting the hang of this now... No really I am!
Posted

Try:

CompileTread.IsBackground = true

Threads in the background will automatically close down when the foreground thread (your application) closes. If you don't set your thread to background, then you have to explicitly end it (with the CompileThread.Abort ... CompileThread.Join.

 

Other than that setting a thread to background makes no difference.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...