By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,575 Members | 1,841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,575 IT Pros & Developers. It's quick & easy.

Application.Run(<ApplicationContext>) Error 'Item has already been added. Key in dictionary: "- 1" key being added: "- 1"'

P: n/a
Hello!

I have a Application, this throws after some time following exception:

Item has already been added. Key in dictionary: "- 1" key being added: "- 1"

I use Application.Run with ApplicationContext.

This error message comes from deeper levels must be thrown (mscorlib.dll?!)
?.

I saw a similar problem. (see
http://www.dotnet4all.com/Dot-Net-Bo...-doevents.html)

For references, Tipps and solutions I would be grateful.

Bye
Ralf Gedrat

Here is a simple example of Code:
---------------------------------

***START CODE***

'
Public Class ClassMyApplication
'
Inherits System.Windows.Forms.ApplicationContext
'
Private WithEvents PrivateForm1 As New Form
Private WithEvents PrivateForm2 As New Form
Private WithEvents PrivateForm3 As New Form
Private WithEvents PrivateFormN As New Form
'
Public Sub New()
'
Call PrivateForm1.Show()
Call PrivateForm2.Show()
Call PrivateForm3.Show()
Call PrivateFormN.Show()
PrivateForm1.Location = New Point(0, 0)
PrivateForm2.Location = New Point(32, 32)
PrivateForm3.Location = New Point(64, 64)
PrivateFormN.Location = New Point(96, 96)
'
End Sub
Private Sub PrivateFormX_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles PrivateForm1.Closing,
PrivateForm2.Closing, PrivateForm3.Closing, PrivateFormN.Closing
'
Call PublicClassMyApplication.ExitThread()
'
End Sub
Private Sub TuWasMitDerForm(ByVal sender As Object, ByVal e As
System.EventArgs) Handles PrivateForm1.Click, PrivateForm2.Click,
PrivateForm3.Click, PrivateFormN.Click
'
CType(sender, Form).Text = Now
'
End Sub
'
End Class
Module ModuleMyApplication
'
Public PublicClassMyApplication As ClassMyApplication
'
<STAThread()> Public Sub Main()
'
Dim ClApplicationLoaded As Boolean
Try
PublicClassMyApplication = New ClassMyApplication
Call Application.Run(PublicClassMyApplication)
Catch ex As Exception
Call MsgBox(ex.Message)
End Try
'
End Sub
'
End Module

***END CODE***

Here is a listing from
http://www.dotnet4all.com/Dot-Net-Bo...-doevents.html
----------------------------------------------------------------------------
---------------------------------

Found the following interesting discussion in the Newsgroups:

Error after a few hours of DoEvents
by:Jeff Cooper
Hi,

I have an app that often has no windows and uses a NotifyIcon. My Sub
Main
has a loop:
Do Until gExit

Application.DoEvents()

Loop

When running the app in development, it works fine. But when running
the
exe, DoEvents() raises the following error after several hours:

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Item has already been added. Key in
dictionary:
"-1" Key being added: "-1"

There's nothing in my app that should happen without human interaction
(clicking, double-clicking the notifyicon, etc). So I'm not sure
what's
happening here. The program is basically just sitting there, waiting
for
something to happen. I left it running last night and found the error
this
morning, so it's not like I'm accidentaly clicking a window or icon
and
raising something. I can't find anything in various groups relating to
this. Anyone have any thoughts?

Thanks,

Jeff

Reply:
by:Armin Zingler

Replace the whole loop by

Application.run

Otherwise you get 100% CPU usage. Instead of setting gExit, call
application.ExitThread. If this doesn't fix the problem, find out the
error
line and post it here.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


Reply:
by:James Uren
I'm having the same problem - my loop's almost identical - help us
please!

James

Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.