Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Valid Threading

Posted on 2003-11-27
8
Medium Priority
?
336 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Accepted Solution

by:
AlexFM earned 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

824 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