Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MonoTouch BackgroundWorker调用textview不工作_C#_Iphone_Xamarin.ios_Backgroundworker - Fatal编程技术网

C# MonoTouch BackgroundWorker调用textview不工作

C# MonoTouch BackgroundWorker调用textview不工作,c#,iphone,xamarin.ios,backgroundworker,C#,Iphone,Xamarin.ios,Backgroundworker,这个代码不起作用 btnUpload.TouchDown += delegate { BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += delegate(object sender, DoWorkEventArgs e) { DoUpload();

这个代码不起作用

btnUpload.TouchDown += delegate 
            {               
                BackgroundWorker bw = new BackgroundWorker();

                bw.DoWork += delegate(object sender, DoWorkEventArgs e) {
                    DoUpload(); 
                };

                bw.RunWorkerAsync();

                bw.RunWorkerCompleted += delegate(object sender3, RunWorkerCompletedEventArgs e3)
                {
                    Console.WriteLine("Uploades");
                    txtUploadStatus.Text = "Uploaded";
                                      }

                };

            };
如果我删除txtUploadStatus.Text,它将作为spected工作,这是一个UITextView

堆栈跟踪是

2011-02-07 12:49:40.645 PruebaGData[3476:940f] bool _WebTryThreadLock(bool), 0x4a6fd30: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
Stacktrace:

  at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging.void_objc_msgSend_IntPtr (intptr,intptr,intptr) <0xffffffff>
  at MonoTouch.UIKit.UITextView.set_Text (string) <0x000e8>
  at PruebaGData.AppDelegate.<FinishedLaunching>m__4 (object,System.ComponentModel.RunWorkerCompletedEventArgs) [0x0000a] in /Users/Alex/Projects/PruebaGData/PruebaGData/Main.cs:79
  at System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted (System.ComponentModel.RunWorkerCompletedEventArgs) <0x00043>
  at System.ComponentModel.BackgroundWorker.<CompleteWorker>m__1 (object) <0x0007b>
  at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0x000cb>

Native stacktrace:

    0   PruebaGData                         0x0060c9e0 mono_handle_native_sigsegv + 404
    1   PruebaGData                         0x005f60b8 mono_sigsegv_signal_handler + 348
    2   libSystem.B.dylib                   0x311dcddf _sigtramp + 42
    3   WebCore                             0x33ced821 _ZL17_WebTryThreadLockb + 156
    4   WebCore                             0x33ced76f WebThreadLock + 46
    5   UIKit                               0x339f71a1 -[UITextView setText:] + 16
    6   PruebaGData                         0x0004f0b8 (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr (intptr,intptr,intptr) + 80
    7   PruebaGData                         0x000187cc PruebaGData.AppDelegate:<FinishedLaunching>m__4 (object,System.ComponentModel.RunWorkerCompletedEventArgs) + 232
    8   PruebaGData                         0x00084094 System.ComponentModel.BackgroundWorker:OnRunWorkerCompleted (System.ComponentModel.RunWorkerCompletedEventArgs) + 68
    9   PruebaGData                         0x00084120 System.ComponentModel.BackgroundWorker:<CompleteWorker>m__1 (object) + 124
    10  PruebaGData                         0x0039e6f0 (wrapper runtime-invoke) object:runtime_invoke_dynamic (intptr,intptr,intptr,intptr) + 204
    11  PruebaGData                         0x005f5d30 mono_jit_runtime_invoke + 3032
    12  PruebaGData                         0x006d4b80 mono_runtime_invoke + 140
    13  PruebaGData                         0x006d7770 mono_runtime_invoke_array + 2068
    14  PruebaGData                         0x006db444 mono_message_invoke + 640
    15  PruebaGData                         0x006f8320 mono_async_invoke + 212
    16  PruebaGData                         0x006fa5cc async_invoke_thread + 400
    17  PruebaGData                         0x006fc964 start_wrapper + 656
    18  PruebaGData                         0x00722048 thread_start_routine + 240
    19  PruebaGData                         0x00751054 GC_start_routine + 132
    20  libSystem.B.dylib                   0x311df88d _pthread_start + 248
    21  libSystem.B.dylib                   0x311d4a90 thread_assign_default + 4294967295

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================
2011-02-07 12:49:40.645 PruebaGData[3476:940f]bool\u WebTryThreadLock(bool),0x4a6fd30:尝试从主线程或web线程以外的线程获取web锁。这可能是从辅助线程调用UIKit的结果。现在崩溃了。。。
堆栈跟踪:
at(包装器管理为本机)MonoTouch.ObjCRuntime.Messaging.void_objc_msgSend_IntPtr(IntPtr,IntPtr,IntPtr)
位于MonoTouch.UIKit.UITextView.set_文本(字符串)
在/Users/Alex/Projects/PruebaGData/PruebaGData/PruebaGData/Main.cs:79中的PruebaGData.AppDelegate.m_uu4(object、System.ComponentModel.RunWorkerCompletedEventArgs)[0x0000a]
位于System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(System.ComponentModel.RunWorkerCompletedEventArgs)
位于System.ComponentModel.BackgroundWorker.m__1(对象)
at(包装器运行时调用)object.runtime_invoke_dynamic(intptr、intptr、intptr、intptr)
本机堆栈跟踪:
0 PruebaGData 0x0060c9e0单声道手柄本机sigsegv+404
1个PruebaGData 0x005f60b8单信号信号处理器+348
2 libSystem.B.dylib 0x311dcddf_sigtramp+42
3网络核心0x33ced821\u ZL17\u网络核心读取锁定B+156
4 WebCore 0x33ced76f WebThreadLock+46
5 UIKit 0x339f71a1-[UITextView setText:+16
6 PruebaGData 0x0004f0b8(包装器管理为本机)MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend_IntPtr(IntPtr,IntPtr,IntPtr)+80
7 PruebaGData 0x000187cc PruebaGData.AppDelegate:m_uu4(对象、系统、组件模型、运行工作或组件开发)+232
8 PruebaGData 0x00084094 System.ComponentModel.BackgroundWorker:OnRunWorkerCompleted(System.ComponentModel.RunWorkerCompletedEventArgs)+68
9 PruebaGData 0x00084120系统.组件模型.后台工作人员:m__1(对象)+124
10 PruebaGData 0x0039e6f0(包装器运行时调用)对象:运行时调用动态(intptr、intptr、intptr、intptr)+204
11 PruebaGData 0x005f5d30单声道jit运行时调用+3032
12 PruebaGData 0x006d4b80单声道运行时调用+140
13 PruebaGData 0x006d7770单声道运行时调用数组+2068
14 PruebaGData 0x006db444单声道消息调用+640
15 PruebaGData 0x006f8320单声道异步调用+212
16 PruebaGData 0x006fa5cc异步调用线程+400
17 PruebaGData 0x006fc964开始包装+656
18 PruebaGData 0x00722048线程启动例程+240
19 PruebaGData 0x00751054 GC_启动_例程+132
20 libSystem.B.dylib 0x311df88d_pthread_start+248
21 libSystem.B.dylib 0x311d4a90线程分配默认值+4294967295
=================================================================
在执行本机代码时收到SIGSEGV。这通常表明
mono运行时或某个本机库中出现致命错误
由您的应用程序使用。
=================================================================

那么如何从bw.RunWorkerCompleted调用UI元素呢?

BackgroundWorker
将使用它在开始处理完成事件时获得的
SynchronizationContext
。如果
触地
事件在后台线程中触发,则上下文将不适合与UIKit一起使用。

您正在从UI线程修改UI元素

将RunWorkerCompleted更改为:

     bw.RunWorkerCompleted += (s, e) => {
         txtUploadStatus.InvokeOnMainThread (delegate {
                    Console.WriteLine("Uploades");
                    txtUploadStatus.Text = "Uploaded";
         });
     };