字符串调用问题
本文关键字:问题 调用 字符串 | 更新日期: 2023-09-27 18:31:22
我正在使用后台工作者,我认为有一个交叉线程的东西。但我解决不了。
我的代码在这里
private void bgworkerGameLoad_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
var arg = e.Argument.ToString();
var liste = webHelper.ShowGame("http://www.abx.com/" + arg);
txtHowToPlay.Invoke(new Action(() => txtHowToPlay.Text = String.Format("Oyun Bilgi: {0}", liste[0])));
txtInfo.Invoke(new Action(() => txtInfo.Text = String.Format("Nasıl Oynanır: {0}", liste[1])));
bgworkerGameLoad.ReportProgress(0,liste[2]);
}
private void bgworkerGameLoad_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
if (FlashPlayerActive)
UnLoad();
string url="";
Invoke(new MethodInvoker(() =>
{
url= e.UserState.ToString();
Thread.Sleep(2);
}));
axShockwaveFlash1.Movie = url;
LoadFlash();
pbWaitForChannelLoading.Visible = false;
axShockwaveFlash1.Play();
}
问题是我无法为我的 shocwaveplayer 获取 e.UserState.ToString()。我使用了一个局部字符串变量,但它是相同的结果。
它发生在程序中的目标调用异常.cs
Application.Run(new FrmMain());;
但该代码在 frmMain 中.cs
这是异常的详细信息
System.Reflection.TargetInvocationException 未处理 消息=异常已被调用的目标引发。 来源=mscorlib 堆栈跟踪: at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Delegate.DynamicInvokeImpl(Object[] args) at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbacks() at System.Windows.Forms.Control.WndProc(Message&m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) 在 x.Program.Main() in c:''Users..............''Program.cs:line 23 at System.AppDomain._nExecuteAssembly(Assembly Assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.NullReferenceException 消息=对象引用未设置为对象的实例。 Source=System.Windows.Forms 堆栈跟踪: at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean sync) at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) at System.Windows.Forms.Control.Invoke(Delegate method) at XXX.FrmMain.bgworkerGameLoad_RunWorkerCompleted(Object sender, RunWorkerCompleteEventArgs e) in c:''Users..........''FrmMain.cs:line 332 at System.ComponentModel.BackgroundWorker.OnRunWorkerComplete(RunWorkerCompleteEventArgs e) at System.ComponentModel.BackgroundWorker.AsyncOperationComplete(Object arg) 内部异常:
我的错误是什么?我尝试使用委托,但它是一样的..
听起来问题就像e.UserState
为空一样简单。您甚至不需要使用 Invoke
来获取UserState
- 如果您只有:
if (FlashPlayerActive)
UnLoad();
string url = e.UserState.ToString();
Thread.Sleep(2); // This is generally a bad idea anyway, btw...
你在哪里设置UserState
?我怀疑您没有在任何地方设置它,因此它仍然是空的,这就是您收到异常的原因。
我解决了这个问题。
在设置值之前,我没有将axShockwaveFlash1添加到我的表单中。
在加载闪光功能中,它是
public void LoadFlash()
{
axShockwaveFlash1 = new AxShockwaveFlash();
axShockwaveFlash1.BeginInit();
axShockwaveFlash1.Location = new Point(14, 196);
axShockwaveFlash1.Name = "Test Movie";
axShockwaveFlash1.TabIndex = 0;
axShockwaveFlash1.Movie = liste[2];
axShockwaveFlash1.EmbedMovie = true;
axShockwaveFlash1.AutoSize = true;
axShockwaveFlash1.Size = new Size(640, 480);
axShockwaveFlash1.Visible = true;
axShockwaveFlash1.EndInit();
splitContainerControl.Panel2.Controls.Add(axShockwaveFlash1);
FlashPlayerActive = true;
}
但是我像这样更改了此代码
public void LoadFlash()
{
axShockwaveFlash1 = new AxShockwaveFlash();
splitContainerControl.Panel2.Controls.Add(axShockwaveFlash1);
axShockwaveFlash1.BeginInit();
axShockwaveFlash1.Location = new Point(14, 196);
...
它有效..
如果在这里找到解决方案
http://forum.fusioncharts.com/topic/2424-how-to-resolve-invalidactivexstateexception-while-loading-chart/#entry8855
感谢