Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Valid Threading

Posted on 2003-11-27
8
Medium Priority
?
330 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 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