Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Whenever you create an event handler for your form or controls it runs on the UI thread. The UI thread in a win forms app. is often also called the main application thread. This is the thread running the Application.Run() message loop.</p> <p>I find it concerning that you say you "naturally decided to create a thread". Introducing extra threads is almost guaranteed to increase complexity and make your application harder to debug, as you have immediately discovered.</p> <p>You have said you are inexperienced in non-web applications, so why introduce the complexity of threading on top of the learning curve of Windows Forms? Try a bit harder to get it working without using threads, then consider threads later on if it is still absolutely necessary.</p> <p>In Windows forms you can often avoid threads by using a Timer and/or Application.DoEvents(). I am not saying don't use threads, just that I have seen many applications where it doesn't even make sense to allow the user to continue working while a process has not complete. </p> <p>If you do want to stick with threads, I would also strongly recommend using the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx" rel="nofollow noreferrer">BackgroundWorker</a> class. It nicely encapsulates a worker thread and provides good safe communication such as ProgressChanged and RunWorkerCompleted events.</p> <p>Looking at your code I would also suggest doing some reading on accessing UI controls from worker threads. In short <em>don't do it</em>, unless using Control.Invoke() or Control.BeginInvoke(). This <a href="https://stackoverflow.com/questions/229554/whats-the-difference-between-invoke-and-begininvoke">previous StackOverflow question</a> would be a good start.</p> <p>[UPDATE]</p> <p>There are a number of places where your code is setting or checking properties of a UI control <em>directly from the worker thread</em>. To avoid exceptions and undefined behaviour you must always access UI controls <em>from the thread that created them</em>, ie. the UI/Main thread. Using Invoke() or BeginInvoke() allow you to do this safely.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload