简体   繁体   中英

IO Operation is blocking my UI Thread and also another Background Thread

I am a novice in Threads, but I want to know how to order thread execution in my scenario, and is the following:

  • UI Thread: Windows Form Background
  • Thread: Kind of print daemon implemented with recursive methods
  • IO Operation: a StreamWriter that do the job of File.AppendText()

When I execute my main app.exe, sometimes it launchs the Windows Forms and the execution process in the task manager (app.exe), in some oportunities its just launchs the execution process in background.

How can I determine the behavior of launching UI, I have to stablish a priority or somewhat else?

My code snippets:

In Form Load (app.exe) it calls the follow function OnStart():

Dim start As ThreadStart = New ThreadStart(AddressOf Writing)
                Hilo = New Thread(start) 
                Hilo.IsBackground = True
                Hilo.Start()

Hilo is a global variable type of Thread declared on Form.vb

Writting is a function that read database and execute some actions, when it finish execution it abort the thread calling OnStop():

Protected Sub OnStop()
        Try
            If Not (Hilo Is Nothing) And Hilo.IsAlive Then
                Hilo.Abort()
            End If
            objWriter.WriteLine("------------------------------------------------")
            objWriter.Close()
        Catch exc As Exception
            LogEventSI(exc.Message & " - " & exc.StackTrace)
        End Try
    End Sub

ObjWriter is the StreamWriter that write lines in a.txt its scope is in all the app.exe

I'm using framework 2.0 and any answers could be writted in C# or Vb.Net

Thanks in advance!

I become with a solution, using a recursive method with a condition when I need to call OnStart() function, then if conditions is not satisfied it calls to OnStop().

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM