window handle already exists

R

René Iversen

Hi,

I've begun programming C# a few days ago, and now I'm experiencing a problem
I really don't understand.

I've create a new Windows Application project in Visual Studio .NET and my
Main method looks like this:

public static void Main( string[] args )
{
ProgramFrame pf = new ProgramFrame();
LogHandler.getInstance().registerLog( new ScreenLog( pf.LogArea ) );
LogHandler.getInstance().registerLog( new OutputLog() );
new Thread( new ThreadStart( Server.Server.getInstance().run ) ).Start();
Application.Run( pf );
}

pf is a System.Windows.Forms.Form object containing a RichTextBox (LogArea)

When the server thread starts it uses the singleton instance of LogHandler
to write som text to the RichTextBox and I get the following error:

An unhandled exception of type System.InvalidOperationException occured in
system.windows.forms.dll

Additional information: Window handle already exists.

Can anyone please explain to me, why this isn't possible, and what can I do
to make it work?

Best regards,

René
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi rene,


I don't quite understand what you wanna do, a login screen?

The problem may be that you are using of.LogArea before calling
Application.Run , I'm not an expert in WIN32 though.

Also what the worker thread does?

I would change the LogHandler calls to the ProgramFrame OnLoad handler

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



René Iversen said:
Hi,

I've begun programming C# a few days ago, and now I'm experiencing a problem
I really don't understand.

I've create a new Windows Application project in Visual Studio .NET and my
Main method looks like this:

public static void Main( string[] args )
{
ProgramFrame pf = new ProgramFrame();
LogHandler.getInstance().registerLog( new ScreenLog( pf.LogArea ) );
LogHandler.getInstance().registerLog( new OutputLog() );
new Thread( new ThreadStart(
Server.Server.getInstance().run ) ).Start();
 
R

René Iversen

I've found out if I insert Thread.Sleep( 100 ) in the ScreenLog it appears
to be working... but why? Is this really necessary?

/René
 
J

Jon Skeet [C# MVP]

René Iversen said:
I've begun programming C# a few days ago, and now I'm experiencing a problem
I really don't understand.

I've create a new Windows Application project in Visual Studio .NET and my
Main method looks like this:

public static void Main( string[] args )
{
ProgramFrame pf = new ProgramFrame();
LogHandler.getInstance().registerLog( new ScreenLog( pf.LogArea ) );
LogHandler.getInstance().registerLog( new OutputLog() );
new Thread( new ThreadStart( Server.Server.getInstance().run ) ).Start();
Application.Run( pf );
}

pf is a System.Windows.Forms.Form object containing a RichTextBox (LogArea)

When the server thread starts it uses the singleton instance of LogHandler
to write som text to the RichTextBox and I get the following error:

An unhandled exception of type System.InvalidOperationException occured in
system.windows.forms.dll

Additional information: Window handle already exists.

Can anyone please explain to me, why this isn't possible, and what
can I do to make it work?

What is your other thread doing? If it's trying to do anything with the
UI, that may well be the cause of the problem.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi rene,

What are you trying to do?

The thread you create may be the problem, maybe you are trying to access
the UI from there.

Cheers,
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Rene,

First of all it is not correct and most of the time leads to unpredictable
effects if you try use a control from code that runs in a thread not created
the control. In your case the thread that run that server will probably try
to write in the richeditbox, which has been created in the main UI thread.

It is really hard to say what is wrong unless you don't provide a simple
working application that we can play with in order to find the problem. So
if you can post such a code we could help you more. Otherwise we can only
guess
--

Stoitcho Goutsev (100) [C# MVP]


René Iversen said:
Hi,

I've begun programming C# a few days ago, and now I'm experiencing a problem
I really don't understand.

I've create a new Windows Application project in Visual Studio .NET and my
Main method looks like this:

public static void Main( string[] args )
{
ProgramFrame pf = new ProgramFrame();
LogHandler.getInstance().registerLog( new ScreenLog( pf.LogArea ) );
LogHandler.getInstance().registerLog( new OutputLog() );
new Thread( new ThreadStart(
Server.Server.getInstance().run ) ).Start();
 
R

René Iversen

Ignacio Machin ( .NET/ C# MVP ) said:
Hi rene,


I don't quite understand what you wanna do, a login screen?

No, it's a server that runs in it's own thread :: new Thread( new
ThreadStart( Server.Server.getInstance().run ) ).Start();

When a new connection is made to the listening socket it uses the singleton
instance of LogHandler to log the activity ( and thereafter when there are
any activity). When the loghandler runs through the list of registered logs
which in this case is a ScreenLog and and OutputLog (just
system.console.writeline(...) ) the ScreenLog will append some text to the
LogArea which is a RichTextBox. Hope this helps you to understand my problem
better.
The problem may be that you are using of.LogArea before calling
Application.Run , I'm not an expert in WIN32 though.

Maybe, I'm no experts also... at all! :). But I don't think so. the
constructor on the RichTextBox has been called, so it should be OK in my
opinion.
Also what the worker thread does?

See above...
 
R

René Iversen

Ignacio Machin ( .NET/ C# MVP ) said:
Hi rene,

What are you trying to do?

The thread you create may be the problem, maybe you are trying to access
the UI from there.

Yes, it is accessing the UI... the RichTextBox (LogArea.AppendText)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top