473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Multithreading in C#

hi i'm new to multithreading in c#, i have this code:


Expand|Select|Wrap|Line Numbers
  1. namespace WindowsApplication4
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.          Thread t1;
  6.          delegate void d1();
  7.  
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.             t1 = new Thread(new ThreadStart(Sta));
  12.             t1.IsBackground = false;
  13.             t1.start();
  14.         }
  15.  
  16.         public void Sta()
  17.         {  
  18.             button1.Invoke(new d1(DD));
  19.         }
  20.  
  21.        public void DD()
  22.         {
  23.             webbrowser1 .Navigate("www.google.com");        
  24.         }
  25.      }          
  26.   }
  27.  
  28. }
and it work.now i want to make it work with 2 threads,i want to have two threads,each one open a webbrowser and navigate google.com.i've written this code but it say's invoke cannot be used on a control until the window handle has been created.i don t know what to do.please help me



Expand|Select|Wrap|Line Numbers
  1. namespace WindowsApplication4
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.          Thread t1;
  6.          delegate void d1();
  7.          Thread t2;
  8.          delegate void d2();
  9.  
  10.  
  11.         public Form1()
  12.         {
  13.             InitializeComponent();
  14.             t1 = new Thread(new ThreadStart(Sta));
  15.             t1.IsBackground = false;
  16.             t1.Start();
  17.             t2 = new Thread(new ThreadStart(St));
  18.             t2.IsBackground = false;
  19.             t2.Start();
  20.         }
  21.  
  22.  
  23.         public void Sta()
  24.         {          
  25.             button1.Invoke(new d1(DD));
  26.            // Use any form control for this ....
  27.         }
  28.  
  29.  
  30.         public void St()
  31.         {
  32.  
  33.             button2.Invoke(new d2(DD2));
  34.            // Use any form control for this ....
  35.         }
  36.  
  37.  
  38.        public void DD()
  39.         {
  40.             webbrowser1 .Navigate("www.google.com");       
  41.          }
  42.  
  43.         public void DD2()
  44.         {
  45.            webBrowser2.Navigate("www.google.com");
  46.         }
  47.  
  48.     }
  49.  
  50. }
Jun 21 '09 #1
3 2822
tlhintoq
3,525 Expert 2GB
invoke cannot be used on a control until the window handle has been created
Where does it say this? Which line? Which control?
wbbbrowser1 and 2 are created outside of the code shown here. Is it possible you didn't initialize webbrowser2 the same as you did #1?
Jun 21 '09 #2
no i created both browsers on my form.and the error is shown on button1.Invoke(new d1(DD)); lin 25 of secon code,but it doesn't show this same error on the first code
Jun 21 '09 #3
Christian Binder
218 Expert 100+
The Invoke()-Method requires an already created Window-Handle.
You could put
Expand|Select|Wrap|Line Numbers
  1. object o = Handle;
between lines 13 and 14 respectively before you start the threads.
Referring to Handle causes the form to create a Window-Handle if it hasn't been created yet.

Also it would be possible to put the (thread-)code in an eventhandler of the HandleCreated-event. Within a handler of the Shown-event it should work as well.

The answer to your question, why the problem only occurs in the second code is (or could be ...) that in the first code the Window-Handle is created before the thread calls the Invoke-Method. In the second code you create a second thread after calling the first thread's Start()-method. Thread-creation is expensive, your main-thread (GUI-thread) is blocked when creating the 2nd thread but the first (already started) thread could call Invoke() meanwhile.
Jun 22 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
9
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
5
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.