Solved

Threading in .net

Posted on 2009-04-14
7
233 Views
Last Modified: 2013-11-08
we are making an application which requires fetch from the database using a webservice. Until the fetch completes the application hangs. We incoroporated threads into the application both plain threads and background threads but none of them are able to bring the application out of the hang until the fetch completes. What could be wrong with our approach? we want a separate thread doing a fetch and doesnot effect the rest of the application!
0
Comment
Question by:sultansaadat
[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
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24141053
Could you possibly post some code dealing with where you are creating the thread and where you are making the threaded call to the webservice?
0
 

Author Comment

by:sultansaadat
ID: 24141179
here goes the code. This one has a simple call to an online database.
 ClassDAL cdl = new ClassDAL();
 DataSet ds = new DataSet();
 void testThread()
        {
            
            
             if (InvokeRequired)
            {
                   
                MethodInvoker method = new MethodInvoker(testThread);
                Invoke(method);
                return;
            }
 
           
            ClassDAL cdl = new ClassDAL();
            //DataSet ds = new DataSet();
            ds = cdl.getAllProducts();
 
        
           
        }
 
 private void button1_Click(object sender, EventArgs e)
        {
         
            this.backgroundWorker1.RunWorkerAsync();
            
            
            
        }
 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
 
            testThread();
            //System.Threading.Thread.Sleep(500);
           
        }
 
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = ds.Tables[0].TableName;
        }

Open in new window

0
 

Author Comment

by:sultansaadat
ID: 24141197
our thread is not running asynchronously!
0
Independent Software Vendors: 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 6

Expert Comment

by:obrienslalom
ID: 24141968
Take a look at the documentation for the ISychronizeInvoke interface:
http://msdn.microsoft.com/en-us/library/system.componentmodel.isynchronizeinvoke_methods.aspx.
What happens with Invoke is that is basically just a call to BeginInvoke followed by a call to EndInvoke.  Invoke will be a synchronous call.
0
 

Author Comment

by:sultansaadat
ID: 24141991
Thanks. I will try the code now! :)
0
 

Author Comment

by:sultansaadat
ID: 24142027
basically we wont have to create other threads?
0
 
LVL 6

Accepted Solution

by:
obrienslalom earned 500 total points
ID: 24144223
I am not going to say that you don't need another thread.  That is what the BackGroundWorker will do for you.  What you are running into in your case is a bit odd actually.  A little background might be needed.

Imagine you have a single GUI thread, which creates a bunch of controls. Now you spawn off another thread to perform some work. This new thread shouldn't be able to update the controls created and owned by the GUI thread.

Invoke/BeginInvoke transfers the tasks onto the GUI threads. That is why you are seeing an unresponsive interface.  You are spawning a background worker, which just transfers the work right back to the original thread.

So, basically, you don't need to use Invoke unless you are updating the controls on the GUI.  So do as much work as you can first, then use Invoke when you have to (which will be any cross-thread updates).
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Silverlight ERP To Angularjs,HTML5 3 58
PowerShell: ForEach ($y in $x.licenses | where {....} 3 55
Adding where clause to entity statement c# 1 47
c#, case, if 4 18
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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