System.Argument Exception: Item has already been added. Key in
dictionary: "-1" Key being added: "-1"
at System.Collecti ons.Hashtable.I nsert(Object key, Object nvalue,
Boolean add)
at System.Collecti ons.Hashtable.A dd(Object key, Object value)
at
System.Windows. Forms.Component Manager.System. Windows.Forms.U nsafeNativeMeth ods+IMsoCompone ntManager.FRegi sterComponent(I MsoComponent
component, MSOCRINFOSTRUCT pcrinfo, Int32& dwComponentID)
at System.Windows. Forms.ThreadCon text.get_Compon entManager()
at System.Windows. Forms.ThreadCon text.RunMessage LoopInner(Int32
reason, ApplicationCont ext context)
at System.Windows. Forms.ThreadCon text.RunMessage Loop(Int32 reason,
ApplicationCont ext context)
at System.Windows. Forms.Applicati on.DoEvents()
I'm working with the Windows Media Encoder object library, needing to
call Application.DoE vents() 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.Trans code() 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.DoE vents() in
the while (!done) loop.
using System;
using System.Windows. Forms;
using System.Runtime. InteropServices ;
namespace WMEncoderWrappe r {
/// <summary>
/// Summary description for WMEncoder.
/// </summary>
public class WMEncoder {
private const int WMENC_CONTENT_O NE_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(W MEncoderLib.WME NC_ENCODER_STAT E state) {
if (state == WMEncoderLib.WM ENC_ENCODER_STA TE.WMENC_ENCODE R_STOPPED)
done = true;
}
/// <summary>
/// Transcode the input audio file (MP3, WMA, etc.) to a WMA9STD file
at 64kbps
/// </summary>
/// <param name="inputFile "></param>
/// <param name="outputFil e"></param>
public void Transcode(strin g inputFile, string outputFile) {
WMEncoderLib.WM Encoder enc = null;
WMEncoderLib.IW MEncSourceGroup srcGroup = null;
WMEncoderLib.IW MEncSource src = null;
WMEncoderLib.IW MEncProfile2 profile = null;
WMEncoderLib.IW MEncAudienceObj audience = null;
try {
done = false;
// Create encoder:
enc = new WMEncoderLib.WM EncoderClass();
enc.OnStateChan ge += new
WMEncoderLib._I WMEncoderEvents _OnStateChangeE ventHandler(OnS tateChange);
profile = new WMEncoderLib.WM EncProfile2Clas s();
profile.Content Type = WMENC_CONTENT_O NE_AUDIO;
profile.Profile Name = "Profile1";
audience = profile.AddAudi ence(10000000);
// Set the encoding codec to WMA9STD with 1-pass CBR encoding:
int idxCodec =
profile.GetCode cIndexFromFourC C(WMEncoderLib. WMENC_SOURCE_TY PE.WMENC_AUDIO,
WMA9STD_FOURCC) ;
//profile.EnumAud ioCodec(idxCode c, out codecName);
audience.set_Au dioCodec(0, idxCodec);
// 2 channels, 44kHz sampling rate, 64,040bps bitrate, 16-bit
samples
audience.SetAud ioConfig(0, 2, 44100, 64040, 16);
// Create a source group:
srcGroup = enc.SourceGroup Collection.Add( "SG_1");
srcGroup.set_Pr ofile(profile);
// Set the input file:
src =
srcGroup.AddSou rce(WMEncoderLi b.WMENC_SOURCE_ TYPE.WMENC_AUDI O);
src.SetInput(in putFile, String.Empty, String.Empty);
// Set the output file:
enc.File.LocalF ileName = outputFile;
// Transcode:
enc.AutoStop = true;
enc.PrepareToEn code(true);
enc.Start();
// Wait until encoding stops:
while (!done) {
Application.DoE vents();
// Yield to other threads.
System.Threadin g.Thread.Sleep( 0);
}
enc.Stop();
} finally {
if (enc != null) Marshal.Release ComObject(enc);
if (srcGroup != null) Marshal.Release ComObject(srcGr oup);
if (src != null) Marshal.Release ComObject(src);
if (profile != null) Marshal.Release ComObject(profi le);
if (audience != null) Marshal.Release ComObject(audie nce);
}
}
}
}
I've seen others in this newsgroup post about similar problems but with
no resolve on the issue. Can someone investigate this problem?
dictionary: "-1" Key being added: "-1"
at System.Collecti ons.Hashtable.I nsert(Object key, Object nvalue,
Boolean add)
at System.Collecti ons.Hashtable.A dd(Object key, Object value)
at
System.Windows. Forms.Component Manager.System. Windows.Forms.U nsafeNativeMeth ods+IMsoCompone ntManager.FRegi sterComponent(I MsoComponent
component, MSOCRINFOSTRUCT pcrinfo, Int32& dwComponentID)
at System.Windows. Forms.ThreadCon text.get_Compon entManager()
at System.Windows. Forms.ThreadCon text.RunMessage LoopInner(Int32
reason, ApplicationCont ext context)
at System.Windows. Forms.ThreadCon text.RunMessage Loop(Int32 reason,
ApplicationCont ext context)
at System.Windows. Forms.Applicati on.DoEvents()
I'm working with the Windows Media Encoder object library, needing to
call Application.DoE vents() 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.Trans code() 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.DoE vents() in
the while (!done) loop.
using System;
using System.Windows. Forms;
using System.Runtime. InteropServices ;
namespace WMEncoderWrappe r {
/// <summary>
/// Summary description for WMEncoder.
/// </summary>
public class WMEncoder {
private const int WMENC_CONTENT_O NE_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(W MEncoderLib.WME NC_ENCODER_STAT E state) {
if (state == WMEncoderLib.WM ENC_ENCODER_STA TE.WMENC_ENCODE R_STOPPED)
done = true;
}
/// <summary>
/// Transcode the input audio file (MP3, WMA, etc.) to a WMA9STD file
at 64kbps
/// </summary>
/// <param name="inputFile "></param>
/// <param name="outputFil e"></param>
public void Transcode(strin g inputFile, string outputFile) {
WMEncoderLib.WM Encoder enc = null;
WMEncoderLib.IW MEncSourceGroup srcGroup = null;
WMEncoderLib.IW MEncSource src = null;
WMEncoderLib.IW MEncProfile2 profile = null;
WMEncoderLib.IW MEncAudienceObj audience = null;
try {
done = false;
// Create encoder:
enc = new WMEncoderLib.WM EncoderClass();
enc.OnStateChan ge += new
WMEncoderLib._I WMEncoderEvents _OnStateChangeE ventHandler(OnS tateChange);
profile = new WMEncoderLib.WM EncProfile2Clas s();
profile.Content Type = WMENC_CONTENT_O NE_AUDIO;
profile.Profile Name = "Profile1";
audience = profile.AddAudi ence(10000000);
// Set the encoding codec to WMA9STD with 1-pass CBR encoding:
int idxCodec =
profile.GetCode cIndexFromFourC C(WMEncoderLib. WMENC_SOURCE_TY PE.WMENC_AUDIO,
WMA9STD_FOURCC) ;
//profile.EnumAud ioCodec(idxCode c, out codecName);
audience.set_Au dioCodec(0, idxCodec);
// 2 channels, 44kHz sampling rate, 64,040bps bitrate, 16-bit
samples
audience.SetAud ioConfig(0, 2, 44100, 64040, 16);
// Create a source group:
srcGroup = enc.SourceGroup Collection.Add( "SG_1");
srcGroup.set_Pr ofile(profile);
// Set the input file:
src =
srcGroup.AddSou rce(WMEncoderLi b.WMENC_SOURCE_ TYPE.WMENC_AUDI O);
src.SetInput(in putFile, String.Empty, String.Empty);
// Set the output file:
enc.File.LocalF ileName = outputFile;
// Transcode:
enc.AutoStop = true;
enc.PrepareToEn code(true);
enc.Start();
// Wait until encoding stops:
while (!done) {
Application.DoE vents();
// Yield to other threads.
System.Threadin g.Thread.Sleep( 0);
}
enc.Stop();
} finally {
if (enc != null) Marshal.Release ComObject(enc);
if (srcGroup != null) Marshal.Release ComObject(srcGr oup);
if (src != null) Marshal.Release ComObject(src);
if (profile != null) Marshal.Release ComObject(profi le);
if (audience != null) Marshal.Release ComObject(audie nce);
}
}
}
}
I've seen others in this newsgroup post about similar problems but with
no resolve on the issue. Can someone investigate this problem?
Comment