473,406 Members | 2,713 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,406 software developers and data experts.

Application.DoEvents() triggers exception!

System.ArgumentException: Item has already been added. Key in
dictionary: "-1" Key being added: "-1"
at System.Collections.Hashtable.Insert(Object key, Object nvalue,
Boolean add)
at System.Collections.Hashtable.Add(Object key, Object value)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMethods+IMsoComponentManager. FRegisterComponent(IMsoComponent
component, MSOCRINFOSTRUCT pcrinfo, Int32& dwComponentID)
at System.Windows.Forms.ThreadContext.get_ComponentMa nager()
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop( Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.DoEvents()

I'm working with the Windows Media Encoder object library, needing to
call Application.DoEvents() to process the message pump to be notified
when an asynchronous encoding job completes. Unfortunately I cannot
reproduce the problem in a small complete example because it requires
the Windows Media Encoder SDK, but I will post my code nonetheless.

What I can tell you is that I'm running a two-threaded Windows.Forms
application that invokes the WMEncoder.Transcode() call from the
non-GUI thread. Also, I'm writing out a lot of output to a ListBox,
which could have been overfilled beyond 64K items. The non-GUI thread
properly calls back to the GUI thread via the BeginInvoke() procedure.

The application is basically a batch WMA file transcoder that processes
thousands of files during one instance of the application.

The following is the complete code for my transcoder wrapper class.
The exception is generated from the call to Application.DoEvents() in
the while (!done) loop.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WMEncoderWrapper {
/// <summary>
/// Summary description for WMEncoder.
/// </summary>
public class WMEncoder {
private const int WMENC_CONTENT_ONE_AUDIO = 1;
private const int
WMA9STD_FOURCC = 353,
WMA9PRO_FOURCC = 354,
WMA9LSL_FOURCC = 355,
WMSPEECH_FOURCC = 10,
PCM_FOURCC = 0;

private bool done = false;

public WMEncoder() {
}

/// <summary>
/// Handles an encoder state change
/// </summary>
/// <param name="state"></param>
private void OnStateChange(WMEncoderLib.WMENC_ENCODER_STATE state) {
if (state == WMEncoderLib.WMENC_ENCODER_STATE.WMENC_ENCODER_STO PPED)
done = true;
}

/// <summary>
/// Transcode the input audio file (MP3, WMA, etc.) to a WMA9STD file
at 64kbps
/// </summary>
/// <param name="inputFile"></param>
/// <param name="outputFile"></param>
public void Transcode(string inputFile, string outputFile) {
WMEncoderLib.WMEncoder enc = null;
WMEncoderLib.IWMEncSourceGroup srcGroup = null;
WMEncoderLib.IWMEncSource src = null;
WMEncoderLib.IWMEncProfile2 profile = null;
WMEncoderLib.IWMEncAudienceObj audience = null;

try {
done = false;

// Create encoder:
enc = new WMEncoderLib.WMEncoderClass();
enc.OnStateChange += new
WMEncoderLib._IWMEncoderEvents_OnStateChangeEventH andler(OnStateChange);

profile = new WMEncoderLib.WMEncProfile2Class();
profile.ContentType = WMENC_CONTENT_ONE_AUDIO;
profile.ProfileName = "Profile1";
audience = profile.AddAudience(10000000);

// Set the encoding codec to WMA9STD with 1-pass CBR encoding:
int idxCodec =
profile.GetCodecIndexFromFourCC(WMEncoderLib.WMENC _SOURCE_TYPE.WMENC_AUDIO,
WMA9STD_FOURCC);
//profile.EnumAudioCodec(idxCodec, out codecName);
audience.set_AudioCodec(0, idxCodec);

// 2 channels, 44kHz sampling rate, 64,040bps bitrate, 16-bit
samples
audience.SetAudioConfig(0, 2, 44100, 64040, 16);

// Create a source group:
srcGroup = enc.SourceGroupCollection.Add("SG_1");
srcGroup.set_Profile(profile);

// Set the input file:
src =
srcGroup.AddSource(WMEncoderLib.WMENC_SOURCE_TYPE. WMENC_AUDIO);
src.SetInput(inputFile, String.Empty, String.Empty);

// Set the output file:
enc.File.LocalFileName = outputFile;

// Transcode:
enc.AutoStop = true;
enc.PrepareToEncode(true);
enc.Start();

// Wait until encoding stops:
while (!done) {
Application.DoEvents();

// Yield to other threads.
System.Threading.Thread.Sleep(0);
}
enc.Stop();
} finally {
if (enc != null) Marshal.ReleaseComObject(enc);
if (srcGroup != null) Marshal.ReleaseComObject(srcGroup);
if (src != null) Marshal.ReleaseComObject(src);
if (profile != null) Marshal.ReleaseComObject(profile);
if (audience != null) Marshal.ReleaseComObject(audience);
}
}
}
}

I've seen others in this newsgroup post about similar problems but with
no resolve on the issue. Can someone investigate this problem?

Oct 2 '06 #1
5 4520


<ja**********@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
System.ArgumentException: Item has already been added. Key in
dictionary: "-1" Key being added: "-1"
at System.Collections.Hashtable.Insert(Object key, Object nvalue,
Boolean add)
at System.Collections.Hashtable.Add(Object key, Object value)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMethods+IMsoComponentManager. FRegisterComponent(IMsoComponent
component, MSOCRINFOSTRUCT pcrinfo, Int32& dwComponentID)
at System.Windows.Forms.ThreadContext.get_ComponentMa nager()
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop( Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.DoEvents()

I'm working with the Windows Media Encoder object library, needing to
call Application.DoEvents() to process the message pump to be notified
when an asynchronous encoding job completes. Unfortunately I cannot
reproduce the problem in a small complete example because it requires
the Windows Media Encoder SDK, but I will post my code nonetheless.

What I can tell you is that I'm running a two-threaded Windows.Forms
application that invokes the WMEncoder.Transcode() call from the
non-GUI thread. . . .
If Transcode is called from a background thread, why is it calling DoEvents
at all? If you need to wait for the job to complete, the Thread.Sleep
should do it. But don't sleep(0), or that thread will constantly get
scheduled: Sleep(100) at least.

David

Oct 2 '06 #2

Since you get event notification when the transcoding is complete you
don't need to poll for done. Just move the local variables to
instance variables, end Transcode() after Start() and then do the
finally stuff when the done event comes in.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
Oct 2 '06 #3

David Browne wrote:
<ja**********@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
System.ArgumentException: Item has already been added. Key in
dictionary: "-1" Key being added: "-1"
at System.Collections.Hashtable.Insert(Object key, Object nvalue,
Boolean add)
at System.Collections.Hashtable.Add(Object key, Object value)
at
System.Windows.Forms.ComponentManager.System.Windo ws.Forms.UnsafeNativeMethods+IMsoComponentManager. FRegisterComponent(IMsoComponent
component, MSOCRINFOSTRUCT pcrinfo, Int32& dwComponentID)
at System.Windows.Forms.ThreadContext.get_ComponentMa nager()
at System.Windows.Forms.ThreadContext.RunMessageLoopI nner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop( Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.DoEvents()

I'm working with the Windows Media Encoder object library, needing to
call Application.DoEvents() to process the message pump to be notified
when an asynchronous encoding job completes. Unfortunately I cannot
reproduce the problem in a small complete example because it requires
the Windows Media Encoder SDK, but I will post my code nonetheless.

What I can tell you is that I'm running a two-threaded Windows.Forms
application that invokes the WMEncoder.Transcode() call from the
non-GUI thread. . . .

If Transcode is called from a background thread, why is it calling DoEvents
at all? If you need to wait for the job to complete, the Thread.Sleep
should do it. But don't sleep(0), or that thread will constantly get
scheduled: Sleep(100) at least.

David
The Windows Media Encoder library's callback mechanism does not work
unless the Windows message pump is running. I've tried without the
DoEvents call and it does not ever fire the event, even after I know
the transcoding is done (watching the filesystem).

Besides, the real problem here is why DoEvents is throwing an exception
about Hashtable.

Oct 4 '06 #4

Samuel R. Neff wrote:
Since you get event notification when the transcoding is complete you
don't need to poll for done. Just move the local variables to
instance variables, end Transcode() after Start() and then do the
finally stuff when the done event comes in.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
I would do that if I wanted my Transcode() function to be asynchronous,
but I don't want that. I want it as part of a batch process and I want
the batch to run sequentially so I get meaningful log output.

Oct 4 '06 #5

If you don't want Transcode() to be async then don't use a separate
thread. Run the Transcode on the main message thread (although this
is very unusual in normal WinForm programming methodology--background
processes normally don't run on the UI thread and normally don't
require the message pump).

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 4 Oct 2006 11:36:38 -0700, ja**********@gmail.com wrote:
>
Samuel R. Neff wrote:
>Since you get event notification when the transcoding is complete you
don't need to poll for done. Just move the local variables to
instance variables, end Transcode() after Start() and then do the
finally stuff when the done event comes in.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

I would do that if I wanted my Transcode() function to be asynchronous,
but I don't want that. I want it as part of a batch process and I want
the batch to run sequentially so I get meaningful log output.
Oct 6 '06 #6

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

Similar topics

5
by: Srinivas Kollipara | last post by:
Hey guys, In my project one place I am using Application.DoEvents() and waiting for 5 minutes to finish my required task in that block. My project need to run for almost 60 hours continuously to...
6
by: Ollie Riches | last post by:
I understand the use of Application.DoEvents() to process all outstanding messages on the message queue in a winforms application if you have long running process on the UI thread. But can anyone...
20
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business...
0
by: Mika M | last post by:
Hi! I'm using Rs232-class, which was shipped with 101 VB.NET samples by Microsoft. Application is made to read COM-port with the following code... Try Me.tmrReadCommPort.Enabled = True If...
7
by: ddd | last post by:
Hi, I am having problems with using the DrawToDC of the MSHTML.iHTMLElementRender in a VB.net application. For some reason I am getting a "catastrophic error". I am basing the code on c#...
4
by: Tad Marshall | last post by:
Hi, I'm having limited luck getting an ApplicationException to work right in my code. This is VB.NET, VS 2003, Windows XP SP2, .NET Framework 1.1. I thought it would be convenient to take...
0
by: Ralf Gedrat | last post by:
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...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
8
by: Bharathi Harshavardhan | last post by:
Hi, I have an application in which I have used Application.DoEvents() method in the click event of a NumericUpDown. This particular method is throwing "Object reference not set to an instance of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.