locked
Database connection in new thread RRS feed

  • Question

  • Hi I have problem and I am looking for best way to solve it.
    I am creating program which will connect to Mysql database and work on connected database.
    But when I am connecting to MySQL database my main window hangs up and don`t answer for input. I know that I should open connection in new thread. But I don`t know how to pass MySql connection object to new thread. When I try to use MySQL connection object from main window in some other thread it says that this object doesn`t belong to that thread.  So my question are:
    1. Can I use object from main Window in other thread?
    2. Can I create object as new thread and then call this object methods?
    3. How should I open new database connection and avoid frozing GUI?

    Thanks for help
    Mark

    Thursday, October 9, 2008 12:16 PM

Answers

  • Hello Mark,

      Sorry for the delayed reply.

      Based on my understanding, Button3_Click executes at the UI thread too.

       Is there is something wrong that I pass handle to Window1 component in constructor of Class1?

       A: nothing wrong. it's a normal way.

       Am I doing right when I call to myClass.OpenConnection() method from Window1 (I thought that this want work cause myClass is another thread) ??

       A: Sorry. No.  It executes at the UI thread.

        In the seperate thread the code create one instance of Class1. Then the thread ends.

       There are several ways to avoid freezing UI thread.

       Here is an artile for one way.

       Asynchronous Programming Overview

       If you have any question please let me know.

      Thanks.

    Please mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by Mark PL Tuesday, October 14, 2008 8:47 PM
    • Marked as answer by Mark PL Tuesday, October 14, 2008 8:47 PM
    • Marked as answer by Mark PL Tuesday, October 14, 2008 8:47 PM
    Tuesday, October 14, 2008 1:38 PM

All replies

  • Hello Mark,

      As I know if the connection isn't a UI control, it can also be accessed from a background thread.

      Of course you need to handle the Sync issue between UI thread and background thread.
      
      Could you please give a simple example to show the problem? 

      
      Additional, here is an article Threading Model. Does it help?

      Thanks. 
    Please mark the replies as answers if they help and unmark them if they provide no help
    • Edited by Hua Chen Friday, October 10, 2008 9:42 AM Edit
    Friday, October 10, 2008 9:32 AM
  • I have already read about threading model but I decided to read that again and I have solve the problem. But I want you to tell me is this good solution and after reading my code answer foolowing questions:

    1. Is there is something wrong that I pass handle to Window1 component in constructor of Class1?
    2. Am I doing right when I call to myClass.OpenConnection() method from Window1 (I thought that this want work cause myClass is another thread) ??

    Here is what I done:

    Code of my main window in which I create new object of Class1 in new thread.
     

    1 //my Main Widnow  
    2 public partial class Window1 : Window  
    3     {  
    4         Class1 myClass;  
    5         public Window1()  
    6         {  
    7             InitializeComponent();  
    8         }  
    9         private void create_new_Thread_Click(object sender, RoutedEventArgs e)  
    10         {  
    11             Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));  
    12             newWindowThread.SetApartmentState(ApartmentState.STA);  
    13             newWindowThread.IsBackground = true;  
    14             newWindowThread.Start();  
    15         }  
    16         private void ThreadStartingPoint()  
    17         {  
    18             myClass = new Class1(this);  
    19             System.Windows.Threading.Dispatcher.Run();  
    20               
    21         }  
    22  
    23         private void button2_Click(object sender, RoutedEventArgs e)  
    24         {  
    25             myClass.CloseConnection();  
    26         }  
    27  
    28         private void button3_Click(object sender, RoutedEventArgs e)  
    29         {  
    30             myClass.OpenConnection();  
    31         }  
    32     } 

    Here is my class Class1 definition.

    1 namespace WpfApplication1  
    2 {  
    3     public class Class1  
    4     {  
    5         private MySqlConnection mysql = new MySqlConnection("Database=my_database;Data Source=localhost;User Id=user;Password=pass");  
    6         private Window1 window;  
    7         public Class1(Window1 window_)  
    8         {  
    9             mysql.StateChange += new System.Data.StateChangeEventHandler(mysql_StateChange);  
    10             window = window_;  
    11         }  
    12  
    13         void mysql_StateChange(object sender, System.Data.StateChangeEventArgs e)  
    14         {  
    15             window.Dispatcher.BeginInvoke(DispatcherPriority.Normal,  
    16                 (ThreadStart)delegate()  
    17             {  
    18                 window.txt.Text += "\r\n" + mysql.State.ToString();  
    19             });  
    20         }  
    21         public void CloseConnection()  
    22         {  
    23             mysql.Close();  
    24         }  
    25         public void OpenConnection()  
    26         {  
    27             mysql.Open();  
    28         }  
    29  
    30     }  
    31
    Saturday, October 11, 2008 1:47 PM
  • Hello Mark,

      Sorry for the delayed reply.

      Based on my understanding, Button3_Click executes at the UI thread too.

       Is there is something wrong that I pass handle to Window1 component in constructor of Class1?

       A: nothing wrong. it's a normal way.

       Am I doing right when I call to myClass.OpenConnection() method from Window1 (I thought that this want work cause myClass is another thread) ??

       A: Sorry. No.  It executes at the UI thread.

        In the seperate thread the code create one instance of Class1. Then the thread ends.

       There are several ways to avoid freezing UI thread.

       Here is an artile for one way.

       Asynchronous Programming Overview

       If you have any question please let me know.

      Thanks.

    Please mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by Mark PL Tuesday, October 14, 2008 8:47 PM
    • Marked as answer by Mark PL Tuesday, October 14, 2008 8:47 PM
    • Marked as answer by Mark PL Tuesday, October 14, 2008 8:47 PM
    Tuesday, October 14, 2008 1:38 PM