The form manipuation is done in the form. The thread only does the search and raises an event which is hanlded in the main form which does the manipuation i.e
private void Search_Match(object sender, MatchEventArgs e)
{
DataRow dr = dtSearchResults.NewRow();
dr[0] = "path";
dr[1] = "Name";
dr[2] = "The data";
dtSearchResults.Rows.Add(d
dgSearchResults.DataSource
}
this is done in the form. Also if it will help, the exception happens here:
static void Main()
{
try
{
Application.Run(new Form1());
}
catch(Exception ee)
{
string s= "a";
}
}
and the following stack trace is thrown
at System.Windows.Forms.DataG
ontext)\r\n at System.Windows.Forms.Appli
Main Topics
Browse All Topics





by: der_jthPosted on 2006-10-29 at 01:03:35ID: 17828148
There are various things that could go wrong here. Determining which one of them is behind your problem is hard; debugging threading problems generally is. One obvious problem is that you're raising an event from another thread. If I get you right, the event is handled by your main form. In this case, code that manipulates the Form controls runs in a thread you have spawned yourself. This is a major problem. Generally, you should never touch any UI components unless you're in the "main thread" (more specifically, the thread that created those items).
m/en-us/li brary/ syst em.windows .forms.con trol.invok e.aspx> and <http://weblogs.asp.net/ju stin_roger s/articles /126345.as px> for more information.
hreading-H andbook-To bin-Titus/ dp/ 1861008 295/sr=8-1 /qid=11621 12573/ref= pd_bbs_sr_ 1/002-7064 283- 737523 4?ie=UTF8& s=books>.
To circumvent this, you should always make control manipulating methods run in the main thread. Use the method Control.Invoke for this. In your case, you might want to call the entire "match" event (or whatever its name is) in the main thread to make UI changes possible. See <http://msdn2.microsoft.co
Note that your method also has other possible points of failure; DataTable isn't thread safe for writing either, but it's impossible to say whether or not the problem is caused by that. I'd say no based on the fact that your exception is thrown by the System.Windows.Forms dll. I strongly suggest you take some time to read a good book about multithreading, for example <http://www.amazon.com/C-T
Let me know if this helps.