vb.net 2008 - terminate a function not the whole program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wal

    vb.net 2008 - terminate a function not the whole program

    I have written a program which lists all possible English words from a
    set of letters. Trouble is if you enter something over 8 letters long
    the [swap letters around then verify word] function can go on for
    several minutes.

    I've put in a progress bar and word counter to show the program is busy
    working in the background but they may still want to exit early (without
    using Windows End Task).

    I can't figure out how to interrupt it because all resources are busy so
    clicking a Cancel button wont get any attention until the process ends.

    Perhaps if the process can check for user input at regular intervals eg.
    the Esc key has been pressed?
  • Armin Zingler

    #2
    Re: vb.net 2008 - terminate a function not the whole program

    "Wal" <no_reply@norep ly.comschrieb
    I have written a program which lists all possible English words from
    a set of letters. Trouble is if you enter something over 8 letters
    long the [swap letters around then verify word] function can go on
    for several minutes.
    >
    I've put in a progress bar and word counter to show the program is
    busy working in the background but they may still want to exit early
    (without using Windows End Task).
    >
    I can't figure out how to interrupt it because all resources are
    busy so clicking a Cancel button wont get any attention until the
    process ends.
    >
    Perhaps if the process can check for user input at regular intervals
    eg. the Esc key has been pressed?
    My personal basic rule is: If you want to do two things at a time (keep UI
    responsive and do the calculcations), use two Threads. Look for multi
    threading in the documentation. Be aware that multi threading is an advanced
    programming technique. Have a look at the BackgroundWorke r class that some
    people find helpful. I posted the following example few days ago: (in Sub
    OnDoWork, replace "Do..Loop" by your own code; and don't forget to check the
    BGW's CancellationPen ding property in your code, too.)

    Imports System.Componen tModel

    Public Class Form1

    Private Sub Form1_Load( _
    ByVal sender As System.Object, ByVal e As System.EventArg s) _
    Handles MyBase.Load

    Dim bgw As New BackgroundWorke r

    bgw.WorkerSuppo rtsCancellation = True
    AddHandler bgw.DoWork, AddressOf OnDoWork
    AddHandler bgw.RunWorkerCo mpleted, AddressOf OnWorkerComplet ed

    Debug.Print("St arting worker")
    bgw.RunWorkerAs ync()

    Debug.Print("Wa iting 5 seconds...")
    Threading.Threa d.Sleep(5000)
    Debug.Print("Ca ncelling worker")
    bgw.CancelAsync ()

    End Sub

    Private Sub OnDoWork( _
    ByVal sender As Object, _
    ByVal e As System.Componen tModel.DoWorkEv entArgs)

    Debug.Print("St art OnDoWork")
    Do
    Threading.Threa d.Sleep(250)
    Loop Until DirectCast(send er, BackgroundWorke r).Cancellation Pending
    Debug.Print("En d OnDoWork")

    End Sub

    Private Sub OnWorkerComplet ed( _
    ByVal sender As Object, _
    ByVal e As System.Componen tModel.RunWorke rCompletedEvent Args)

    Debug.Print("Wo rker completed")

    End Sub


    End Class



    Armin

    Comment

    • Clive Lumb

      #3
      Re: vb.net 2008 - terminate a function not the whole program


      "Wal" <no_reply@norep ly.coma écrit dans le message de news:
      xUyQk.11532$sc2 .7101@news-server.bigpond. net.au...
      >I have written a program which lists all possible English words from a set
      >of letters. Trouble is if you enter something over 8 letters long the [swap
      >letters around then verify word] function can go on for several minutes.
      I hope that you are not checking each combination against the dictionary? If
      you are doing this then it is obvious that it will take forever. It will be
      faster if you check every word (of 8 letters or less) in the dictionary
      against the 8 letter word - at least then you are only dealing with words
      that exist.


      Comment

      • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

        #4
        RE: vb.net 2008 - terminate a function not the whole program

        Though not answering directly the original question, someone had a similar
        task some time ago on one of the boards. The recommended solution was to
        keep a dictionionary that had a key be the string of letters to lookup
        (sorted alphabetically) , and then store a list of words from your input word
        list into the dictionary by letters they use. For example:

        Dictionary <string, list<string>DIC TIONARY;

        CAR is stored under DICTIONARY ["ACR"].
        ARC is also stored under DICTIONARY ["ACR"].
        RACE is stored under DICTIONARY ["ACER"].

        You can quickly fill the dictionary on load, and the lookups should be quick.

        "Wal" wrote:
        I have written a program which lists all possible English words from a
        set of letters. Trouble is if you enter something over 8 letters long
        the [swap letters around then verify word] function can go on for
        several minutes.
        >
        I've put in a progress bar and word counter to show the program is busy
        working in the background but they may still want to exit early (without
        using Windows End Task).
        >
        I can't figure out how to interrupt it because all resources are busy so
        clicking a Cancel button wont get any attention until the process ends.
        >
        Perhaps if the process can check for user input at regular intervals eg.
        the Esc key has been pressed?
        >

        Comment

        Working...