You can use a Background Thread : BackgroundWorker Component Overview
Fix UI Unresponsive
![](https://techprofile.blob.core.windows.net/images/fLDUTnfcFkSP5rQElA0PBw.png?8D8BAC)
HI
I am working on a program where it reads a text file using zira.speak. it works fine but when i try to click on close_command button to stop it but the User Interface become non-responsive and i am unable to close it untill it has finished reading the whole text form the text file.
below is my code
Function Read_Book()
Show_Statics("< \ A I . . .Read Book >")
Dim pic_counter As Integer = 0, FAF As Integer = 0, File_Create_Object, filename, objStartFolder, objFSO, objFolder, colFiles, Script_Disctionary, Opened_File, File_Row
Dim File_Line, Books(0)
objStartFolder = "D:\All Projects\Demo Projects\AI-Projects\Books"
objFSO = CreateObject("Scripting.FileSystemObject")
objFolder = objFSO.GetFolder(objStartFolder)
colFiles = objFolder.Files
For Each objFile In colFiles
Books(FAF) = objFile.Name
FAF = FAF + 1
ReDim Preserve Books(FAF)
Next
For i = 0 To FAF - 1
If i = 0 Then
Zira.speak(" Allright , hope you are sitting tight. I am going to read" & Books(i) & "to you")
Else
Zira.speak(" Do you want to take a break or I must start reading the" & i & "Book for you.")
End If
Try
File_Create_Object = CreateObject("Scripting.FilesystemObject")
Script_Disctionary = CreateObject("Scripting.Dictionary")
Opened_File = File_Create_Object.OpenTextFile(objStartFolder & "\" & Books(i), 1)
File_Row = 0
Do Until Opened_File.AtEndOfStream
File_Line = Opened_File.Readline
stat_counter_all += 1
Script_Disctionary.Add(File_Row, File_Line)
File_Row = File_Row + 1
Zira.speak(File_Line)
LIve_Status.Width = 36
Loop
Catch ex As Exception
Show_Statics("< \ A I . . .Error >")
Zira.speak("There is an error." & "the," & ex.Message)
End Try
Next
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
how to fix it >
2 additional answers
Sort by: Most helpful
-
Lokesh R 61 Reputation points
2021-01-25T18:50:02.56+00:00 And yes i also have a question on that camera preview window i wanted to add some more property like height and width to that Bitmap inside we are drawing " This is a test " string so that it shows less background of black box - I kinda of new to this thread designs of microsoft community it confuses me where to ask or write >
-
Karen Payne MVP 35,401 Reputation points
2021-01-25T22:00:33.75+00:00 Hello,
You can use asynchronous coding via Task coupled with await keyword and have an option to cancel and also show progress if needed. I have several basic code samples.
Note this extension guards against cross threading issues.