MonoTouch BackgroundWorker 调用文本视图不起作用

发布于 2024-10-16 10:06:23 字数 4263 浏览 6 评论 0原文

这段代码不起作用

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 它会按预期工作,它是一个 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.
=================================================================

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

This code its not working

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";
                                      }

                };

            };

If i remove the txtUploadStatus.Text it works as spected, it is a UITextView

the stacktrace is

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.
=================================================================

So how can i call an UI Element from bw.RunWorkerCompleted???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2024-10-23 10:06:23

您正在脱离 UI 线程修改 UI 元素。

将 RunWorkerCompleted 更改为:

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

You are modifying UI elements off the UI thread.

Change the RunWorkerCompleted to:

     bw.RunWorkerCompleted += (s, e) => {
         txtUploadStatus.InvokeOnMainThread (delegate {
                    Console.WriteLine("Uploades");
                    txtUploadStatus.Text = "Uploaded";
         });
     };
鼻尖触碰 2024-10-23 10:06:23

BackgroundWorker 将使用启动时获取的 SynchronizationContext 来处理完成事件。如果 TouchDown 事件在后台线程中触发,则上下文将不适合与 UIKit 一起使用。

BackgroundWorker will use the SynchronizationContext it gets when started to process the completion events. If the TouchDown event fires in a background thread, then the context will be inappropriate for use with the UIKit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文