字符串调用问题

时间:2013-09-14 13:03:49

标签: c# multithreading backgroundworker invoke

我正在使用背景工作者,我认为有一个交叉线程的东西..但我无法解决它。

我的代码在这里

  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()。我使用了本地字符串变量,但结果相同。

它发生在program.cs

中的targetofaninvocation异常
     Application.Run(new FrmMain());;

但该代码在frmMain.cs

这是例外的细节

  

System.Reflection.TargetInvocationException未处理     Message =调用目标抛出了异常。     来源= 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)          在System.Delegate.DynamicInvokeImpl(Object [] args)          在System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)          在System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)          在System.Threading.ExecutionContext.runTryCode(Object userData)          在System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码,CleanupCode backoutCode,Object userData)          在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)          在System.Windows.Forms.Control.InvokeMarshaledCallbacks()          在System.Windows.Forms.Control.WndProc(消息& m)          在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)          在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)          在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)          在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)          在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)          在System.Windows.Forms.Application.Run(Form mainForm)          在XXX.Program.Main()在c:\ Users .............. \ Program.cs:第23行          在System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)          在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Threading.ThreadHelper.ThreadStart()     InnerException:System.NullReferenceException          Message =对象引用未设置为对象的实例。          来源= System.Windows.Forms的          堆栈跟踪:               在System.Windows.Forms.Control.MarshaledInvoke(Control caller,Delegate方法,Object [] args,布尔同步)               在System.Windows.Forms.Control.Invoke(Delegate方法,Object [] args)               在System.Windows.Forms.Control.Invoke(Delegate方法)               at XXX.FrmMain.bgworkerGameLoad_RunWorkerCompleted(Object sender,RunWorkerCompletedEventArgs e)在c:\ Users .......... \ FrmMain.cs:332行               在System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)               在System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)          InnerException:

我的错误是什么?我试图使用委托,但它是相同的..

2 个答案:

答案 0 :(得分:0)

听起来问题就像e.UserState为空一样简单。你甚至不需要使用InvokeUserState - 如果你有这个问题,你会看到同样的问题:

if (FlashPlayerActive)
    UnLoad();
string url = e.UserState.ToString();
Thread.Sleep(2); // This is generally a bad idea anyway, btw...

你在哪里设置UserState?我怀疑你没有把它设置在任何地方,所以它仍然是空的,这就是为什么你得到一个例外。

答案 1 :(得分:0)

我解决了这个问题..

在设置值之前,我没有将axShockwaveFlash1添加到我的表单中。

在LoadFlash函数中,它是

    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

感谢名单