Simultaneous control of the gui

  • Thread starter Thread starter Richard Aubin
  • Start date Start date
R

Richard Aubin

I'm creating an application that needs to go through over 2,000 loops
repeatedly.

However, the user interface is "locking up" while the processing is
happening.

Why is this happening, and how can I implement buttons to either pause or
stop the code?
 
you might want to look into threads

Richard Aubin said:
I'm creating an application that needs to go through over 2,000 loops
repeatedly.

However, the user interface is "locking up" while the processing is
happening.

Why is this happening, and how can I implement buttons to either pause or
stop the code?
 
Hi Richard,
I'm creating an application that needs to go through over 2,000 loops
repeatedly.

May I ask what the loop is performing?

There are a few ways to prevent lock ups, one way is to run the routine
in a separate thread, for example

Private cmythread as Threading.Thread

Private Sub startReading()
cmythread = New Threading.Thread(AddressOf stuff)
cmythread.IsBackground = True
Call cmythread.Start()
End Sub

Private Sub stuff()
'Any code in here is performed in a new thread, thus preventing your
main thread from being
'locked up!
End Sub

The above will initiate a new thread to perform the tasks in "stuff",
once the routine is ended the thread will end. Another way is to "yield" to
the operater at the end of each loop by using Application.DoEvents. But
this can cause strange effects in your application and also slow it down a
hell of a lot. But all in all it depends what you are doing in your loop.
However, the user interface is "locking up" while the processing is
happening.

Basically a "lock-up" occurs when your application cannot get enough
time to process windows messages as the time is being alloted elsewhere,
your intensive loop for example. The idea of placing an
"Application.DoEvents" statement at the end of each loop is to cause your
application to process any outstanding messages passed to it via the
operator.

Nick.
 
A simple solution would be to add Application.DoEvents in the loop.

hope this helps..
Imran.
 
I'll see if I can make this a little more clear, but I think I'm getting the
jist of it.

The user enters a keyword to search through a ton of strings held in an
array.

I want to be able to have a cancel button on the same form to be accessible
if the operation is taking too long.

Will starting another thread allow me to do this efficiently?

Richard.
 
Hi Richard,
I'll see if I can make this a little more clear, but I think I'm getting the
jist of it.

The user enters a keyword to search through a ton of strings held in an
array.

Right, what about using a HashTable,

Dim pop as new HashTable

pop.add("key","object") 'Add an object to the hastable, key can be
*any* object as can object.

pop.contains("key") 'Check the existance of "key" within the
hashtable, again, key can be *any* object

Hashtables are so much quicker to use than array's especially if you
would like to search for a specific entry, you will find that you can find a
specific object within a hashtable containing thousands upon thousands of
objects within milliseconds!

Are you just checking to see if the keyword matches a list of keywords?
Remember that you can add *any* object into the hashtable and have it
"associated" with the keyword so to speak, so lets say you have a specific
class which performs a certain task on a value,

Public Interface MyInterface

Function doStuff(Byval iValue as integer) as integer

End Interface

Public Class addOne
Implements MyInterface

Public Function doStuff(Byval iValue as integer) as integer implements
MyInterface.doStuff
Return(iValue + 1)
End Function

End Class

Public Class takeOne
Implements MyInterface

Public Function doStuff(Byval iValue as integer) as integer implements
MyInterface.doStuff
Return(iValue - 1)
End Function

End Class

Dim pop As New Hashtable()

pop.Add("addone", New addOne())
pop.Add("takeone", New takeOne())

Dim poo As Integer = 10

MessageBox.Show(CType(pop.Item("addone"), MyInterface).doStuff(poo))
MessageBox.Show(CType(pop.Item("takeone"), MyInterface).doStuff(poo))

I hope this helps!! :-)

Nick.
 
I Should have explained that a little also,

Using polymorphism you can create a generic interface for the action that
you want your "keyword" to perform. Then create a class for each "keyword"
and implement your own version of "dostuff", then you can bung loads of
different "functions" so to speak into a hashtable for lightning fast
access.

Anyway, any probs just yell!

Nick.
 
* "Richard Aubin said:
I'm creating an application that needs to go through over 2,000 loops
repeatedly.

However, the user interface is "locking up" while the processing is
happening.

Why is this happening, and how can I implement buttons to either pause or
stop the code?

Call 'Application.DoEvents' every n iterations, or use multithreading:

Multithreading:

<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms06112002.asp>
<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms08162002.asp>
<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms01232003.asp>

<URL:http://www.devx.com/dotnet/Article/11358/>

<URL:http://msdn.microsoft.com/library/e...SystemWindowsFormsControlClassInvokeTopic.asp>

Multithreading in Visual Basic .NET (Visual Basic Language Concepts)
<URL:http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconthreadinginvisualbasic.asp>

Sample:

<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/downloads/FileSystemEnumerator.zip>
 
Richard,

When you have predefined keys, than I think there cannot be any discussion
about that great idea from Nick.

However when your keywords can be spread all over the place than show us
your code. The method you use can be very important in that.

When you use only a simple regex it can be by instance often much more than
100 times slower than by using by instance a Visual Basic INSTR.

We did a test october last year (for which I used the counting method from
Nick)

In that test it was that for searching a string the most performance had the
Visual.Basic INSTR function. For a single characters it was the indexof I
thougth,

http://www.google.com/[email protected]

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top