Manage form elements from another stream in Windows Mobile

Trying to get a thread to change format controls in Windows Mobile.

Throws an unsupported exception.

Does this mean that it is impossible to do at all?

If not, how do I do this? Forms are created in the parent / main thread, then the thread is created to do some work in the background, but I want to make the Background thread update the form to show it completed ...

+1
c # forms windows-mobile
Jun 14 '10 at 12:44
source share
3 answers

You cannot access GUI elements in a thread other than a GUI. You will need to determine if a call to the GUI thread is required. For example (here I made some of them earlier):

public delegate void SetEnabledStateCallBack(Control control, bool enabled); public static void SetEnabledState(Control control, bool enabled) { if (control.InvokeRequired) { SetEnabledStateCallBack d = new SetEnabledStateCallBack(SetEnabledState); control.Invoke(d, new object[] { control, enabled }); } else { control.Enabled = enabled; } } 

or

 public delegate void AddListViewItemCallBack(ListView control, ListViewItem item); public static void AddListViewItem(ListView control, ListViewItem item) { if (control.InvokeRequired) { AddListViewItemCallBack d = new AddListViewItemCallBack(AddListViewItem); control.Invoke(d, new object[] { control, item }); } else { control.Items.Add(item); } } 

Then you can set the enabled property (from my first example) with ClassName.SetEnabledState(this, true); .

+6
Jun 14 2018-10-12T00:
source share

You need to use the Control.InvokeRequired property, as the user interface elements must be accessible from the main thread.

In the background thread, you are creating an event.

 public event EventHandler<MyEventArgs> MyApp_MyEvent; this.MyApp_MyEvent(this, new MyEventArgs(MyArg)); 

In the main user interface thread, you subscribe to this event:

 this.myThread.MyApp_MyEvent+= this.MyAppEventHandler; 

and the handler itself:

 private void MyApp_EventHandler(object sender, MyEventArgs e) { if (this.MyControl.InvokeRequired) { this.MyControl.Invoke((MethodInvoker)delegate { this.MyAction(e.MyArg); }); } else { this.MyAction(e.MyArg); } } 
+4
Jun 14 '10 at 12:50
source share

See also:

  • Forced multi-threaded VB.NET class to output results to one form
  • Bring data from an asynchronous operation to the main thread.
  • Update user interface from multiple user threads (.NET)
  • Problems with C # Thread
  • How can you find out if there is a user interface in the main thread? (In CF)
  • Separation of GUI and logic in different threads in a Windows Form application
0
Jun 14 '10 at 14:35
source share



All Articles