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

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

For references, Tipps and solutions I would be grateful.

Ralf Gedrat

Here is a simple example of 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
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

Found the following interesting discussion in the Newsgroups:

Error after a few hours of DoEvents
by:Jeff Cooper

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



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

An unhandled exception of type 'System.ArgumentException' occurred in
Additional information: Item has already been added. Key in
"-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
happening here. The program is basically just sitting there, waiting
something to happen. I left it running last night and found the error
morning, so it's not like I'm accidentaly clicking a window or icon
raising something. I can't find anything in various groups relating to
this. Anyone have any thoughts?



by:Armin Zingler

Replace the whole loop by

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

How to quote and why:

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


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.