Static Form Control

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

I just inherited a .NET 1.1 GUI and upgraded to .NET 2.0. Naturally, there
are several errors indicating that a control was accessed in a thread that
is not the main thread. I have been fixing these errors all over the place,
and there is plenty of documentation on the Internet explaining this error
....

http://msdn2.microsoft.com/en-us/library/ms171728.aspx

Anyway, while I have fixed several of these issues, one has proven to be a
little more difficult. The code basically looks like this ...

public class X
{
private static System.Windows.Forms.StatusBarPanel panel = null;

....

public static StatusBarPanel TheStatusBar
{
set
{
panel = value;
}
}

public static void SetPanelText(string theText)
{
if(null != panel)
{
// This is where the thread access exception occurs
panel.Text = theText;
}
}
}

I have tried the normal method of creating a delegate and setting a callback
function. However, the static panel control does not have an Invoke
function. I have tried calling panel.Parent.Invoke(...), but this does not
do anything either.

After this didn't work for me, I tried creating a background worker like it
says to do in the link I reference earlier. The code for my
RunWorkerCompleted function basically looked like this ...
{
if (null != panel)
{
// mText was set earlier in the SetPanelTextFunction. Later, in the
SetPanelText function,
// I call the RunWorkerAsync to reach this code in the
RunWorkerCompleted function.
panel.Text = mText;
}
}

Anyway, this didn't work for me either. I get the same cross thread
exception. (The BackgroundWorker is created when the panel value is set).

All in all, does anybody know how I can Invoke this control since the
control has been declared as static? I can't take away the static
declaration either without breaking about 15 projects in the solution that
seem to use this control.

Thanks,
-- Jim
 
There is no good reason to make a Form instance static, and in fact, good
reasons not to. I would get rid of the static modifier and make it an
instance instead.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
I would agree, but I just inherited this project from somebody else. If I
do that, that means that the 15 other projects that call into the static
method will need a handle to the instance. It will probably result in a
major change. Right now, I'm looking for an easy way to get this thing to
build without any exceptions.

Any other ideas?

Thanks,
-- Jim
 
All in all, does anybody know how I can Invoke this control since the
control has been declared as static? I can't take away the static
declaration either without breaking about 15 projects in the solution that
seem to use this control.

Why does the static-ness of the variable stop you from calling Invoke
on it? Just use panel.Invoke (or panel.BeginInvoke) as you normally
would.

Jon
 
Excellent Question!

It appears as though a System.Windows.Forms.StatusBarPanel does not have the
invoke method. At least intellisense does not pick it up and the compiler
yells at me if I try to add it manually.

Is there another way to call invoke to get this to work?

Thanks again.
 
Jim said:
Excellent Question!

It appears as though a System.Windows.Forms.StatusBarPanel does not have the
invoke method. At least intellisense does not pick it up and the compiler
yells at me if I try to add it manually.

Is there another way to call invoke to get this to work?

StatusBarPanel isn't a control - but you can use panel.Parent.Invoke
instead. That will call Invoke on the StatusBar which is the parent of
the panel.
 
WOW. I've never done any drugs, but I swear I must have lost my mind when I
initially tried to invoke the parent. I even talked about
panel.Parent.Invoke not working correctly in my initial post. I don't know
what I had wrong when I initially tried the Parent.Invoke, but I just
re-wrote the code just to try it again and now it works.

I hate these kinds of problems. Well, thanks Jon. I have no idea why that
didn't work the first time.

-- Jim
 
Back
Top