Solved

Threading in .net

Posted on 2009-04-14
7
228 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
  • 4
  • 2
7 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
our thread is not running asynchronously!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:obrienslalom
Comment Utility
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
Comment Utility
Thanks. I will try the code now! :)
0
 

Author Comment

by:sultansaadat
Comment Utility
basically we wont have to create other threads?
0
 
LVL 6

Accepted Solution

by:
obrienslalom earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Total in textboxes 9 35
Library to convert HTML to PDF. 8 44
Check if number is currency 15 29
Create XML 5 30
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now