473,322 Members | 1,480 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,322 software developers and data experts.

Bug in .NET Tab Control? Help!

I have an app written in C# (obviously). Here is my dilemma:

The form loads, instantiates a bunch of worker threads that execute one at a
time. When one finishes, it starts the next one in line. I have about 5
datagrids on 5 different tab pages in a tab control. What I'm using the
Threads for is pretty basic: get data from a database and fill a datagrid. I
use delegates throughout to allow the worker thread to call the
'FillDataGrid' method in the main thread (the form thread). I've had
problems before with threads not accessing other threads controls which is
why I use the delegates. But still it crashes with the following:

An unhandled exception of type 'System.ArgumentException' occurred in
system.windows.forms.dll

Additional information: Controls created on one thread cannot be parented to
a control on a different thread.

I decided to add another tab page (out of pure imagination) as the first tab
page in the control. voila! now everything loads properly (without changing
any code whatsoever). Problem is, I try to programmatically remove the
temporary page (with a generic "please wait while loading" message). It
removes the page fine, but on the next page in line, all the controls
dissappear. If I don't change or remove tab pages programmatically, all the
controls stay intact. I'm not sure what I am missing, and leaving the temp
page intact is just ugly. Anybody know what I'm missing?
Nov 16 '05 #1
5 2077
slylos <sl****@discussions.microsoft.com> wrote:
I have an app written in C# (obviously). Here is my dilemma:

The form loads, instantiates a bunch of worker threads that execute one at a
time. When one finishes, it starts the next one in line. I have about 5
datagrids on 5 different tab pages in a tab control. What I'm using the
Threads for is pretty basic: get data from a database and fill a datagrid. I
use delegates throughout to allow the worker thread to call the
'FillDataGrid' method in the main thread (the form thread). I've had
problems before with threads not accessing other threads controls which is
why I use the delegates. But still it crashes with the following:

An unhandled exception of type 'System.ArgumentException' occurred in
system.windows.forms.dll

Additional information: Controls created on one thread cannot be parented to
a control on a different thread.


That suggests your threading isn't what you think it is. Are you sure
you're using Control.Invoke rather than calling Invoke or BeginInvoke
directly on the delegate?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
I'll try to give you more of my code without running out of space here:

I've got a Windows form where the tabcontrol is located
(frmScheduleRequestsAdmin)
I've got a db layer that connects to a web service that I built to retreive
the records from the database (tsClientWebServiceConnection.dll)
the db layer has a method 'GetPendingScheduleRequests'. here is the
delegates in the db layer:

------------------------------------>db layer
code<----------------------------------------
public delegate void PendingScheduleRequestsComplete(DataSet dsPending);
public static PendingScheduleRequestsComplete PendingRequestsComplete;

//here is the method that retrieves the pending requests

public void GetPendingScheduleRequests()
{
MyWebServiceObject obj = new MyWebServiceObject();//name changed =^)
DataSet dsPending;

try
{
dsPending = obj.GetPendingRequests();
}
catch(System.Net.WebException we)
{
//save error info to file
}

if (PendingRequestsComplete != null) //invoke delegate
{
PendingRequestsComplete(dsPending);
}
}
------------------------------------>db layer
code<----------------------------------------

and here is the delegate connection and thread start
------------------------------------>Form
code<------------------------------------------
//form_load
private void frmScheduleRequestAdmin_Load(object sender, System.EventArgs e)
{
tsClientWebServiceConnection ts = new tsClientWebServiceConnection;
tsClientWebServiceConnection.PendingRequestsComple te = new
tsClientWebServiceConnection.PendingScheduleReques tsComplete(LoadPendingScheduleRequests);
Thread tGetPending = new Thread(new
ThreadStart(ts.GetPendingScheduleRequests));

tGetPending.Start();
}
//target for delegate
private void LoadPendingScheduleRequests(DataSet dsPending)
{
dsLocalPendingCopy = dsPending.Copy();
dgPending.DataSource = dsLocalPendingCopy.Tables[0];
ProgressComplete();
}
//progress complete handles tab removal
private void ProgressComplete()
{
tcTabs.TabPages.Remove(tpLoadingPage);
}

------------------------------------->Form
code<-------------------------------------------

when ProgressComplete executes, thats when the next tab in line loses its
controls ... please help!!

That suggests your threading isn't what you think it is. Are you sure
you're using Control.Invoke rather than calling Invoke or BeginInvoke
directly on the delegate?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
slylos <sl****@discussions.microsoft.com> wrote:
I'll try to give you more of my code without running out of space here:

I've got a Windows form where the tabcontrol is located
(frmScheduleRequestsAdmin)
I've got a db layer that connects to a web service that I built to retreive
the records from the database (tsClientWebServiceConnection.dll)
the db layer has a method 'GetPendingScheduleRequests'. here is the
delegates in the db layer:


It looks like you're doing exactly what the error message is
complaining about - updating a control from a non-UI thread.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

Just "using delegates" doesn't do everything automatically - you need
to call Control.Invoke/BeginInvoke appropriately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Well I'm not having a problem with the error message; that went away as soon
as I added a tabpage in front of the first tabpage with the datagrid. The
problem I'm having is when I programmatically remove that first (temp)
tabpage to expose the tabpage with the datagrid, all the controls on the tab
with the datagrid disappear(including the datagrid, but not their tabpage
parent).

Like I said, I do the same exact thing in another form (with different data,
same controls i.e. tab pages/datagrids) and I never have problems. The
difference between that form and this form is the tabpage on that form is 3rd
in the tabpage order, and on the new form, its the first tabpage in the
order. The problem I'm having is controls mysteriously disappearing . . .
Nov 16 '05 #5
slylos <sl****@discussions.microsoft.com> wrote:
Well I'm not having a problem with the error message; that went away
as soon as I added a tabpage in front of the first tabpage with the
datagrid. The problem I'm having is when I programmatically remove
that first (temp) tabpage to expose the tabpage with the datagrid,
all the controls on the tab with the datagrid disappear(including the
datagrid, but not their tabpage parent).
Just adding a tabpage hasn't fixed your threading error though. I'm not
sure why the error message went away, but the problem hasn't.
Like I said, I do the same exact thing in another form (with
different data, same controls i.e. tab pages/datagrids) and I never
have problems. The difference between that form and this form is the
tabpage on that form is 3rd in the tabpage order, and on the new
form, its the first tabpage in the order. The problem I'm having is
controls mysteriously disappearing . . .


Just because something has worked in the past doesn't mean it was
guaranteed to work, or that you weren't doing things wrong. If you
access the UI from a different thread, you shouldn't be doing that.
Your code appears to be doing that. Fix it (everywhere you're doing it,
not just in the place where you're seeing problems), and then if it
still doesn't work, we'll look again.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
11
by: -D- | last post by:
How can I turn the visibility of the xml control on or off? <%@ Control Language="c#" AutoEventWireup="false" Codebehind="TopNavBar.ascx.cs" Inherits="compass.user_controls.TopNavBar"...
9
by: Nathan Sokalski | last post by:
I am using ASP.NET 2.0's ImageMap Control to create 2 imagemaps, one directly below the other. When I do this a thin blank space appears between them. After several days of frustration I realized...
9
by: Nathan Sokalski | last post by:
I am using ASP.NET 2.0's ImageMap Control to create 2 imagemaps, one directly below the other. When I do this a thin blank space appears between them. After several days of frustration I realized...
1
by: yusufjammy | last post by:
How to control parallel port with visual basic 2008 ? thanks in advance
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.