Solved

Valid Threading

Posted on 2003-11-27
8
323 Views
Last Modified: 2010-04-16

Hey gurus,

I've created a small threading test and I would like to see what you all think.  
Basically I want to know if I am breaking any threading rules.

Ok this is the Code.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Data;
using System.Data.SqlClient;

namespace ThreadingTest
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.CheckedListBox clstSettings;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            ///
            
            //Thread
            private Thread thOpenDatabaseConnection;

            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //Setup the open database connection thread
                  thOpenDatabaseConnection = new Thread(new ThreadStart(OpenDatabaseConnection));
                  thOpenDatabaseConnection.Name = "Open Database Connection";
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.clstSettings = new System.Windows.Forms.CheckedListBox();
                  this.SuspendLayout();
                  //
                  // clstSettings
                  //
                  this.clstSettings.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
                  this.clstSettings.Location = new System.Drawing.Point(8, 8);
                  this.clstSettings.Name = "clstSettings";
                  this.clstSettings.SelectionMode = System.Windows.Forms.SelectionMode.None;
                  this.clstSettings.Size = new System.Drawing.Size(504, 229);
                  this.clstSettings.TabIndex = 2;
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(520, 254);
                  this.Controls.Add(this.clstSettings);
                  this.Name = "Form1";
                  this.Text = "Threading Test";
                  this.Load += new System.EventHandler(this.Form1_Load);
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void CheckLine(int nIndex, bool bValue)
            {
                  clstSettings.SetItemChecked(nIndex,bValue);
                  this.Refresh();
            }

            private int AddLine(string strString)
            {
                  int nReturn = 0;

                  nReturn = clstSettings.Items.Add(strString,CheckState.Unchecked);
                  this.Refresh();

                  return nReturn;
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                  //Let the user know we are checking the database connection...
                  int nLine1 = AddLine("Verifying database connection settings");
                  //Check Line 1
                  CheckLine(nLine1,true);

                  //Run the open database connection thread
                  thOpenDatabaseConnection.Start();
            }

            private void OpenDatabaseConnection()
            {
                  try
                  {
                        //Attempt to open the database connection
                        int nLine2 = AddLine("Opening a database connection");

                        //Create a new sql connection object
                        SqlConnection sqlCn = new SqlConnection();

                        //Set the connection string
                        sqlCn.ConnectionString = "Persist Security Info=False;Initial Catalog=Db;Data Source=MAIN\\SQL";

                        sqlCn.Open();

                        //Check Line 2
                        CheckLine(nLine2,true);

                        int nLine3 = AddLine("Database connection verified!");
                        //Check Line 3
                        CheckLine(nLine3,true);

                  }
                  catch
                  {
                                          
                        //Add a line to say that the database connection failed
                        int nLine4 = AddLine("Database connection failed!");
                        //Check Line 4
                        CheckLine(nLine4,true);
                  }

            }
      }
}
0
Comment
Question by:ACanadian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 10

Expert Comment

by:smegghead
ID: 9834731
the main thing, and common mistake people make when threading is that they can't talk directly to the gui.. any command to change any of the GUI should be called using Invoke...

So, when you are calling checkline from your thread, this is in turn calling a GUI update.
0
 

Author Comment

by:ACanadian
ID: 9834995

How does one use invoke to communicate with the GUI?
0
 

Author Comment

by:ACanadian
ID: 9835215

And what is the reason behind having to use invoke. Everything works fine if you don't.  Maybe .net doesn't need to use this invoke dealy.

Tim
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 9835431
1) All UI operations from the worker thread should be done using Invoke or BeginInvoke. Direct call from the worker thread may cause unpredictable results because .NET framework keeps some data in the thread local storage (TLS) and when you call UI functions from another thread, this data is not available. If your particular program works now, it doesn't mean this is right.

2) You run the worker thread without any control over it. To make professional multi-threaded program you need to control the thread execution - to stop the thread at any time when this is required. I think this article can help you:

http://www.codeproject.com/csharp/workerthread.asp
0
 
LVL 10

Expert Comment

by:smegghead
ID: 9836862
To call something, using invoke, the following syntax can be used

      private delegate void ThreadWorkerDef(NotifyEventArgs e);
      public void UpdateHandler(object sender,NotifyEventArgs e)
      {
            this.Invoke(new ThreadWorkerDef(ThreadWorker),new object[1] {e});
      }

      public void ThreadWorker(NotifyEventArgs e)
      {
            // Code to update the UI
      }
0
 

Author Comment

by:ACanadian
ID: 9839699
I gave the points to AlexFM because he posted information on a great example.

Thanks

0
 
LVL 10

Expert Comment

by:smegghead
ID: 9840375
Fair enough, but do you know you can split point.. I also told you everything you needed to know. And gave you an example which works.

Smg.
0
 

Author Comment

by:ACanadian
ID: 9843894
The other example did work as well, however yours was simpler.  

I suppose I could have split the points but are point really that important?  

Tim
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question