0

我正在使用后台工作人员,我认为存在跨线程问题。但我无法解决。

我的代码在这里

  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=Exception 已被调用的目标抛出。Source=mscorlib StackTrace: 在 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) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 参数, CultureInfo 文化, Boolean skipVisibilityChecks) 在 System.Delegate.DynamicInvokeImpl(Object[] args) 在 System.Windows.Forms 。控制。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() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback 回调,对象状态)在 System.Threading.ThreadHelper.ThreadStart() InnerException:System.NullReferenceException 消息=对象引用未设置为对象的实例。Source=System.Windows.Forms StackTrace:在 System.Windows.Forms.Control.MarshaledInvoke(控制调用者,委托方法,

我的错误是什么?我尝试使用委托,但它是一样的..

4

2 回答 2

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

谢谢

于 2013-09-14T13:51:39.120 回答
0

听起来问题就像为e.UserState空一样简单。您甚至不需要使用Invoke来获取UserState- 如果您有以下问题,您会看到同样的问题:

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

你在哪里设置UserState?我怀疑您没有在任何地方设置它,所以它仍然为空,这就是您遇到异常的原因。

于 2013-09-14T13:15:48.010 回答