Getting Data from Form Control in Thread - c#

Having an issue getting data from a form control from within a thread. I need to access the data, then modify it.
The following doesn't work I'm aware, but I used it as an example to see what I'm trying to do.
Thread t = new Thread(() => {
foreach (ListViewItem row in listView1.Items)
{
row.SubItems[0].Text = "Checking";
Thread.Sleep(2000);
}
});
t.Start();
I've read the MSDN documentation on making thread safe calls, but I can't seem to get access to the actual list view control. The examples I've seen use delegates to "update" controls, but I need access to the data in the controls before I update the data in them.
Edit:
I'd like to see an example, or a link to an example, detailing how to get access to the ListView1 form control in the foreach loop.

You need to use Invoke pattern, in order to be able to access any UI element or its properties from the thread other then main UI thread. All UI controls on windows allways run on the main thread, to handle message chain correctly between OS and UI presented on the screen.

The (quickly written) example I was talking about, this assumes that you do not need to really use the controls, I included a function that is based off tigran's link
Thread t = new Thread(() => UpdateText(listBox1.Items));
t.Start();
private void UpdateText(ListBox.ObjectCollection items)
{
foreach (var item in items)
{
SetText(item.ToString());
Thread.Sleep(1000);
}
}

You can't do what you want to do. All accesses and updates to UI must go in UI thread. It is mandatory.
What you can do is writing your raw data into cache on UI then processing your cache and callbacks to UI after all processings are finished.
public class CacheData {
private object row;
public CacheData(object row)
{
//initialization
}
public static ProcessedData ProcessData(List<CacheData> dataToProcess)
{
return new ProcessedData();
}
}
public class ProcessedData { }
private void AccessControl()
{
ListView list = new ListView();
List<CacheData> cache = new List<CacheData>();
//Filling the cache on UI
foreach (var row in list.Items)
{
cache.Add(new CacheData(row));
}
//Process result async and then invoke on UI back
System.ComponentModel.BackgroundWorker bg = new System.ComponentModel.BackgroundWorker();
bg.DoWork += (sender,e) => {
e.Result = CacheData.ProcessData(cache);
};
bg.RunWorkerCompleted += (sender, e) => {
//If you have started your bg from UI result will be invoked in UI automatically.
//Otherwise you should invoke it manually.
list.Dispatcher.Invoke((Action) delegate {
//pass e.result to control here)
},null);
};
bg.RunWorkerAsync();
}

Method 1:
Use Invoke like Tigran describes.
For Winforms this would look like:
Thread t = new Thread(() =>
{
if (!Dispatcher.CurrentDispatcher.CheckAccess())
{
Dispatcher.CurrentDispatcher.BeginInvoke(
new Action(() =>
{
foreach (ListViewItem row in listView1.Items)
{
row.SubItems[0].Text = "Checking";
Thread.Sleep(2000);
}
}),
DispatcherPriority.ApplicationIdle,
null);
}
else
{
foreach (ListViewItem row in listView1.Items)
{
row.SubItems[0].Text = "Checking";
Thread.Sleep(2000);
}
}
});
t.Start();
The CheckAccess() Call returns true if called from the UI-Thread otherwise false.
The Dispatcher Class is located in the "System.Windows.Threading" Namespace in the "WindowsBase" NET. Assembly
Dispatcher info copied from: https://stackoverflow.com/a/4429009/1469035
Edit: Changed code to WinForms.
Edit: Code Fixed.
Method 2:
Use a Callback:
Untested Code:
public partial class Form1 : Form
{
private delegate void SetCallback(ListViewItem row, string text);
public Form1()
{
InitializeComponent();
}
private void SomeMethod()
{
Thread t = new Thread(() =>
{
foreach (ListViewItem row in listView1.Items)
{
if (listView1.InvokeRequired)
{
SetCallback d = new SetCallback(SetText);
this.Invoke(d, new object[] { row, "Checking" });
}
Thread.Sleep(2000);
}
});
t.Start();
}
private void SetText(ListViewItem row, string text)
{
row.SubItems[0].Text = text;
}
}
AFAIK readonly Access to Controls from Threads other than the UI-Thread is allowed in Winforms. So you can check any Control-Property you want and pass the required information to the Delegate.
And even if Reading doents work that way, you can just make another Delegate that has a return value. The Invoke() Method returns an object:
Similar to this:
private delegate object GetCallback(ListViewItem row);
private object o;
...
GetCallback g = new GetCallback(GetText);
o = this.Invoke(g, new object[] { row });
private string GetText(ListViewItem row)
{
return row.SubItems[0].Text;
}
Derived From: Link

Related

Update ObservableCollection in list box in thread

Hy,
I have a Observable Collection which is bind with a list box. I add logs to the Observable Collection. I always add the message immediately to the Observable Collecten. But the list gets only updated when the loop is finished but I want to Update it when I add one item in the for loop. This is why I use a Thread but I have a few problems.
I have a thread safe ObservableCollection:
class ThreadSafeObservableCollection<T> : ObservableCollection<T>
{
public override event NotifyCollectionChangedEventHandler CollectionChanged;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler collectionChanged = this.CollectionChanged;
if (collectionChanged != null)
foreach (NotifyCollectionChangedEventHandler handler in collectionChanged.GetInvocationList())
{
DispatcherObject dispatcherObject = handler.Target as DispatcherObject;
if (dispatcherObject != null)
{
Dispatcher dispatcher = dispatcherObject.Dispatcher;
if (dispatcher != null && !dispatcher.CheckAccess())
{
dispatcher.BeginInvoke(
(Action)(() => handler.Invoke(this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))),
DispatcherPriority.DataBind);
continue;
}
}
handler.Invoke(this, e);
}
}
}
This is my test class:
public partial class MainWindow : Window
{
ThreadSafeObservableCollection<Animal> list = new ThreadSafeObservableCollection<Animal>();
public MainWindow()
{
InitializeComponent();
list.Add(new Animal() { Name = "test1" });
list.Add(new Animal() { Name = "test2" });
this.DataContext = list;
}
private void dsofsdkfd(object sender, RoutedEventArgs e)
{
//Version 1
Task.Factory.StartNew(() => test());
//Version2
/*
var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
var token = Task.Factory.CancellationToken;
Task.Factory.StartNew(() => test(), token, TaskCreationOptions.None, uiScheduler);
*/
}
public void test()
{
for (int i = 0; i < 10000; i++)
{
list.Add(new Animal() { Name = "test" + i });
System.Threading.Thread.Sleep(1);
}
}
}
See the private void dsofsdkfd(object sender, RoutedEventArgs e) function to the comment Version1.
In the beginning it works so the list updates everytime I add a item. After a few entries I get an exception:
"Information for developers (use Text Visualizer to read
this):\r\nThis exception was thrown because the generator for control
'System.Windows.Controls.ListBox Items.Count:1089' with name 'Logger'
has received sequence of CollectionChanged events that do not agree
with the current state of the Items collection. The following
differences were detected:\r\n Accumulated count 994 is different
from actual count 1089. [Accumulated count is (Count at last Reset +
Adds - #Removes since last Reset).]\r\n\r\nOne or more of the following sources may have raised the wrong events:\r\n
System.Windows.Controls.ItemContainerGenerator\r\n
System.Windows.Controls.ItemCollection\r\n
System.Windows.Data.ListCollectionView\r\n *
WpfApplication1.ThreadSafeObservableCollection`1[[WpfApplication1.Animal,
WpfApplication1, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]\r\n(The starred sources are considered more
likely to be the cause of the problem.)\r\n\r\nThe most common causes
are (a) changing the collection or its Count without raising a
corresponding event, and (b) raising an event with an incorrect index
or item parameter.\r\n\r\nThe exception's stack trace describes how
the inconsistencies were detected, not how they occurred. To get a
more timely exception, set the attached property
'PresentationTraceSources.TraceLevel' on the generator to value 'High'
and rerun the scenario. One way to do this is to run a command
similar to the following:\n
System.Diagnostics.PresentationTraceSources.SetTraceLevel(myItemsControl.ItemContainerGenerator,
System.Diagnostics.PresentationTraceLevel.High)\r\nfrom the Immediate
window. This causes the detection logic to run after every
CollectionChanged event, so it will slow down the application.\r\n"
See private void dsofsdkfd(object sender, RoutedEventArgs e) function to the comment Version2.
I also tried it with the TaskScheduler using FromCurrentSynchronizationContext.
Then it throws no exception but I have the same problem like at the beginning, so the list box refreshes only if the for each loop is finished.
How I can accomplish that the list box updates when I add an element?
Best regards
I wouldn't roll my own ObservableCollection for this. I'd just perform the .Add call on the UI thread.
public void test()
{
for (var i = 0; i < 10000; i++)
{
// create object
var animal = new Animal {Name = "test" + i};
// invoke list.Add on the UI thread
this.Dispatcher.Invoke(new Action(() => list.Add(animal)));
// sleep
System.Threading.Thread.Sleep(1);
}
}
Note that since you're in a Window subclass, this.Dispatcher will correspond to the dispatcher for the UI thread. If you move this logic to, say, a model or view model class, you'll need to explicitly capture the value of Dispatcher.Current on the UI thread, and pass that dispatcher manually to the background thread.
EDIT: OP asked for more information on using the Dispatcher outside of a FrameworkElement class. Here's how you would do that. The dispatcher for the UI thread is acquired on the UI thread by calling Dispatcher.CurrentDispatcher. That dispatcher is then passed directly into the background thread procedure.
public class MainWindowViewModel
{
// this should be called on the UI thread
public void Start()
{
// get the dispatcher for the UI thread
var uiDispatcher = Dispatcher.CurrentDispatcher;
// start the background thread and pass it the UI thread dispatcher
Task.Factory.StartNew(() => BackgroundThreadProc(uiDispatcher));
}
// this is called on the background thread
public void BackgroundThreadProc(Dispatcher uiDispatcher)
{
for (var i = 0; i < 10000; i++)
{
// create object
var animal = new Animal { Name = "test" + i };
// invoke list.Add on the UI thread
uiDispatcher.Invoke(new Action(() => list.Add(animal)));
// sleep
System.Threading.Thread.Sleep(1);
}
}
}
You need to maintain current dispatcher thread for the same. You must update collection in current dispatcher thread only. One way to do it is to use BiginInvoke() method of dispatcher class.
Save current dispatcher in a variable in constructor and then use it when needed.
_currentDispatcher = Application.Current.Dispatcher;
For example: We have a scenario where we popup an error window if we have an error. We need to close an Error window if error count is zero. Now if we are handling events and message in another thread (not on UI thread) then we need to save the UI thread dispatcher object and need to use it to update collection or any other action. Here I am closing Error Window. (I don't have solution ready for updating collection.)
if (ErrorNotifications.Count == 0)
_currentDispatcher.BeginInvoke(DispatcherPriority.Normal, new Action<ErrorNotificationWindow>(CloseErrorNotificationWindow), _errWindow);
Here CloseErrorNotificationWindow is method with parameter _errWindow.
private void CloseErrorNotificationWindow(ErrorNotificationWindow _errWindow)
{
if (_errWindow == null)
return;
if (_errWindow.IsActive)
_errWindow.Close();
}
In CloseErrorNotificationWindow() method you can update your collections and it should not give any exception as you would be using main UI thread to do it.
Hope this will helpful.

In C#, how do you lock a form?

I'm trying to lock the main form while a please wait box is shown on the screen, but it won't work. Here's my dilemma.
I have 2 forms. The main form that the user clicks a refresh button to load the list of SQL Servers, and a Please wait form that shows while it's loading the list. The SQL Server thread is a separate thread by default while using C# and it locks out the main thread in order to process the SQL request.
I can add a background worker, but then I can't update my combo box to show the list as its a UI control. If I use a handler for that, my show_dialog() for the please wait box will stop locking down the main form.
How is it even possible to lock this form down without the left click queue being run after the main thread goes active again? I added the code that needs to be executed while the user waits.
public void PullServers()
{
bool ServersFound = false;
foreach (string Value in SQL.LocateSqlInstances())
{
this.cmbServer.Items.Add(Value);
ServersFound = true;
}
if (!ServersFound)
{
this.cmbServer.Items.Add(Strings.Lang("ddServerNoneFound"));
this.cmbServer.SelectedIndex = 0;
}
else
{
if (!s.empty(General.setting("SQLSERVER")))
{
this.cmbServer.Text = General.setting("SQLSERVER");
}
else
{
this.cmbServer.SelectedIndex = 0;
}
}
this.picRefreshServers.Image = Properties.Resources.Refresh;
}
public static Array LocateSqlInstances()
{
using (DataTable sqlSources = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources())
{
string Servers = null;
foreach (DataRow source in sqlSources.Rows)
{
string instanceName = source["InstanceName"].ToString();
if (!s.empty(instanceName))
{
Servers += source["ServerName"].ToString() + "\\" + instanceName + "[[SERVBREAK]]";
}
}
string[] ServersList = Servers.Split(new string[] { "[[SERVBREAK]]" }, StringSplitOptions.RemoveEmptyEntries);
return ServersList;
}
}
I think you are on the right track with a BackgroundWorker. I have found the following pattern to work well for me.
In your main form, you need to perform the following steps.
Create a BackgroundWorker to perform the long running operation.
Start the BackgroundWorker.
Display the waiting form as a modal dialog.
// Step 1:
BackgroundWorker bg = new BackgroundWorker()
bg.DoWork += new DoWorkEventHandler(bg_DoWork);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
// Step 2:
bg.RunWorkerAsync();
// Step 3:
waitingForm = new WaitingForm();
waitingForm.ShowDialog();
As you know, you can't update the UI from the bg_DoWork handler since it does not run on the UI thread. So just get the data you need here and pass it on to the the bg_RunWorkerCompleted handler using the e.Result parameter.
private void bg_DoWork(object sender, DoWorkEventArgs e)
{
Array servers = SQL.LocateSqlInstances();
e.Result = servers;
}
The bg_RunWorkerCompleted runs on the UI thread so it is safe to update your controls here. This is where you should close the waiting form and then update your UI.
private void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Close the Waiting form.
waitingForm.Close();
// Retrieve the result of bg_DoWork().
Array servers = e.Result as Array;
bool ServersFound = false;
foreach (string Value in servers)
{
this.cmbServer.Items.Add(Value);
ServersFound = true;
}
if (!ServersFound)
{
this.cmbServer.Items.Add(Strings.Lang("ddServerNoneFound"));
this.cmbServer.SelectedIndex = 0;
}
else
{
if (!s.empty(General.setting("SQLSERVER")))
{
this.cmbServer.Text = General.setting("SQLSERVER");
}
else
{
this.cmbServer.SelectedIndex = 0;
}
}
this.picRefreshServers.Image = Properties.Resources.Refresh;
}

How to update a list box by an asynchronous call?

I have developed a windows forms c# application, i just want update items in a Listbox in the main form by spin offing another thread without blocking the GUI form.
Since threads cannot access form entities like listbox, i thought of using delegates.
Following code in the below shows how i used a delegate to do that task, but it blocks the GUI form. so i just want to convert it to an asynchronous delegate which updates list box without blocking the GUI Form
delegate declaration
delegate void monitoringServiceDel();
calling the delegate
new monitoringServiceDel(monitoringService).BeginInvoke(null, null);
delegate method implementation
private void monitoringService()
{
this.listEvents.Invoke(new MethodInvoker(delegate()
{
int i = 0 ;
while (i<50)
{
listEvents.Items.Add("count :" + count++);
Thread.Sleep(1000);
i ++;
}
}));
}
For Win Forms you'll need to use the Control's Invoke method:
Executes the specified delegate on the thread that owns the control's
underlying window handle
The basic scenario is:
Do the heavy lifting work with a BackgroundWorker to retrieve all of your items on a non UI blocking thread.
On the BackgroundWorker.RunWorkerCompleted Event, use the Control's Invoke method to add the items to the Control (ListBox in your case).
Something along the lines of:
var bw = new BackgroundWorker();
bw.DoWork += (sender, args) => MethodToDoWork;
bw.RunWorkerCompleted += (sender, args) => MethodToUpdateControl;
bw.RunWorkerAsync();
This should get you going in the right direction.
Edit: working sample
public List<string> MyList { get; set; }
private void button1_Click( object sender, EventArgs e )
{
MyList = new List<string>();
var bw = new BackgroundWorker();
bw.DoWork += ( o, args ) => MethodToDoWork();
bw.RunWorkerCompleted += ( o, args ) => MethodToUpdateControl();
bw.RunWorkerAsync();
}
private void MethodToDoWork()
{
for( int i = 0; i < 10; i++ )
{
MyList.Add( string.Format( "item {0}", i ) );
System.Threading.Thread.Sleep( 100 );
}
}
private void MethodToUpdateControl()
{
// since the BackgroundWorker is designed to use
// the form's UI thread on the RunWorkerCompleted
// event, you should just be able to add the items
// to the list box:
listBox1.Items.AddRange( MyList.ToArray() );
// the above should not block the UI, if it does
// due to some other code, then use the ListBox's
// Invoke method:
// listBox1.Invoke( new Action( () => listBox1.Items.AddRange( MyList.ToArray() ) ) );
}
If you are modifying a UI element, then you are going to HAVE to block the UI thread. If the items come in bursts or require processing between adding each one, then you might want to think about running the processing behind the scenes (via a backgroundworker or a Task). But, if you are just taking data and populating the list, then you are required to use the UI thread.
The easiest solution would be to use the BackgroundWorker control, combined with two Panels. The idea is to have one panel on the foreground Visible when the form loads, and have an ImageBox inside of it that plays a simple loading gif. The ListBox will be inside the other panel that won't be visible by default and will be right behind the first panel.
Once the form is loaded, start your BackgroundWorker and accomplish whatever Data retrieving or updating that you have to do and once the Task is complete, set the data inside your ListBox and simply bring the ListBox panel and make it visible.
That way you'll have a Semi Asynchronous loading of your ListBox, while it's not updated after every item being added. You can use this technique anytime you want, not simply on form load!
Here is a code example:
namespace AsyncForm
{
public partial class Form1 : Form
{
private List<String> collectionItems = new List<String>();
public Form1()
{
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 20; i++)
{
((List<String>)e.Argument).Add("Something " + i);
System.Threading.Thread.Sleep(200);
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
listBox1.Items.AddRange(collectionItems.ToArray());
listBox1.Visible = true;
pictureBox1.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync(collectionItems);
}
}
}
You should separate function to update UI and long-time process.
To handle UI logic..
private void UpdateUI(string item)
{
if (Thread.CurrentThread.IsBackground)
{
listEvents.Dispatcher.Invoke(new Action(() => //dispatch to UI Thread
{
listEvents.Items.Add(item);
}));
}
else
{
listEvents.Items.Add(item);
}
}
To do asynchronous process using TaskParallel
private void Dowork()
{
Task task = Task.Factory.StartNew(() =>
{
int i = 0;
while (i < 10)
{
Thread.Sleep(1000);
UpdateUI(i.ToString());
i++;
}
});
}

Winforms: Update label on Form from another class

I have a project where I need to update a labels text from inside another classes method. It's worth noting that this method is being called from a Background worker thread.
I have tried passing in the text to update as a UserState Obj in the Workers ReportProgress(); method and then updating the label when the workers progress changed event is fired off on the main form. This works, but obviously only updates the labels text when the progress changed event is raised.
I have code that's loading/removing proxies constantly and I need a label to show this as it happens (as opposed to only updating when the bg workers progress changed event fires). Hopefully someone can help.
Thanks
Edit* Here's some code to make the problem a little easier to understand: -
public string Request(string action)
{
if (string.IsNullOrWhiteSpace(action))
{
return "";
}
HttpWebRequest req;
string response = string.Empty;
while (response.Equals(string.Empty) && proxy != null)
{
try
{
req = (HttpWebRequest)WebRequest.Create(action);
req.Proxy = proxy;
response = new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();
}
catch (Exception ex)
{
RemoveProxy(proxy);
MessageBox.Show("Proxy Removed: " + proxy.Address.ToString());
proxy = GenNewProx();
MessageBox.Show("New proxy" + proxy.Address.ToString());
}
}
return response;
}
^^^ - Where i need to set the labels text, using Msgboxs at the moment but updating a label on the main form is obviously preferable
foreach (string url in URLs)
{
result.URL = url;
result.Shares = grabber.GetFacebookShares(url);
result.Tweets = grabber.GetTweetCount(url);
result.PlusOnes = grabber.GetPlusOnes(url);
bgWorker.ReportProgress((outputGridView.Rows.Count * 100) / importGridView.Rows.Count, result);
}
^^^ - Inside the bg workers do_work method on the main form.
2nd Edit*
I'm a bit new to events but could i not fire off a custom event say Proxy_Changed everytime i switch proxys and pass in a string argument with the new proxy/msg w.e and then subscribe to this event in the main form, then set the label on the main forms text = the string args when this event fires off? I'm probably talking jibberish tbh though :/
Here's what I think the important parts of your class that needs to do the background works looks like:
public class Grabber
{
public event EventHandler<MyArgs> NotifyParentUI;
// other code.....
public string Request(string action)
{
if (string.IsNullOrWhiteSpace(action))
{
return "";
}
HttpWebRequest req;
string response = string.Empty;
while (response.Equals(string.Empty) && proxy != null)
{
try
{
req = (HttpWebRequest)WebRequest.Create(action);
req.Proxy = proxy;
response = new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();
}
catch (Exception ex)
{
RemoveProxy(proxy);
NotifyParentUI(this, new MyArgs()
{ Message = string.Format("Proxy Removed: {0}", proxy.Address.ToString()) });
proxy = GenNewProx();
NotifyParentUI(this, new MyArgs()
{ Message = string.Format("New Proxy: {0}", proxy.Address.ToString()) });
}
}
return response;
}
}
In your main form you have a method to update your label that is thread-safe:
public void UpdateMyLabel(object sender, MyArgs ea)
{
this.Invoke(new MethodInvoker(
delegate()
{
labelControl1.Text = ea.Message;
}
));
}
Also in the main form you must create an instance of your "grabber":
Grabber grabber = new Grabber();
grabber.NotifyParentUI += UpdateMyLabel;
You should have a method that runs on its own thread:
public void ThreadProc()
{
// other code before this....
foreach (string url in URLs)
{
result.URL = url;
result.Shares = grabber.GetFacebookShares(url);
Thread.Sleep(0); // may want to take the Sleeps out
result.Tweets = grabber.GetTweetCount(url);
Thread.Sleep(0);
result.PlusOnes = grabber.GetPlusOnes(url);
Thread.Sleep(0);
}
}
Here's how you start the thread in the part of your main form:
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
As a side note, if you need to pass data to your Thread, look here:
Passing Data to Threads and Retrieving Data from Threads
Use Invoke method to run anonymous function on UI thread to update the label. For example:
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (sender, args) =>
{
for (int i = 0; i < 10000; ++i)
{
DoSomeWorkInBackground();
// Update the label in UI thread
MyOtherFormInstance.Invoke((MethodInvoker)delegate()
{
MyOtherFormInstance.SetLabelText(i);
});
DoSomOtherWorkInBackground();
}
};
In your Form:
public void SetLabelText(int i)
{
myLabel.Text = i.ToString();
// not sure that this needed, but try:
myLabel.Invalidate();
}
It sounds from your question and subsequent comments to other answers that you are running your code within a WinForms project, correct me if I am wrong? In a winform the main program thread is usually always static (static void Main()) therefore you must make your EventHandler static also to avoid null exceptions. I believe this will resolve your issue as it sounds like the rest of your code is correct?

Multithreading access of win forms controls c#

I'm trying to iterate through the selected values on a ListBox to see if I have a match, and then set a bool if I do. I've got this code that works when not threading:
for (int i = 0; i < FileTypesExcludedListBox.SelectedItems.Count; i++)
if (currentFiles[currentFileLoc].EndsWith(FileTypesExcludedListBox.SelectedItems[i].ToString()))
doNotCompare = true;
Now I've changed the app so this is happening inside a thread that didn't create the control. I've used anonymous delegates to update text labels before, but I need the return value for this and I'm not sure how to do that...
Use Form's Invoke Method (this.Invoke) to Execute a Delegate from the thread.
private delegate void YourDelegate(obj param1,obj param2);
private void YourFunction(obj param1,obj param2)
{
//Write Code Here
}
//Call this from your Thread
this.Invoke(new YourDelegate(YourFunction), new object[] { param1,param2});
1) You can Invoke calls to control on UI thread. Of course, you can pass array of objects as parameter. But it's really easy to put some parameter on wrong place. I think, it's better to use anonymous delegate instead.
Same thing for returning values from control. You just need to specify type of returned value in delegate signature.
public string ExcludedFileType
{
get
{
if (InvokeRequired)
return (string)Invoke((Func<string>)delegate { return ExcludedFileType; });
return (string)listBox1.SelectedItem;
}
set
{
if (InvokeRequired)
{
Invoke((MethodInvoker)delegate { ExcludedFileType = value; });
return;
}
listBox1.SelectedItem = value;
}
}
Usage from worker thread:
ExcludedFileType = "jpg";
MessageBox.Show(ExcludedFileType);
2) You can provide parameters when starting thread.
// start async operation
ThreadPool.QueueUserWorkItem(DoSomething, ExcludedFileTypes);
So, you will not need access to control from worker thread:
private void DoSomething(object state)
{
IList<string> excluedeFileTypes = (IList<string>)state;
foreach(string fileType in excluedeFileTypes)
if (currentFiles[currentFileLoc].EndsWith(fileType))
doNotCompare = true;
}
private IList<string> ExcludedFileTypes
{
get
{
List<string> filteTypes = new List<string>();
foreach (var item in listBox1.SelectedItems)
filteTypes.Add(item.ToString());
return filteTypes;
}
}
3) You can use AOP e.g. PostSharp to implement thread dispatching automatically.
You can access to windows control only from thread, that created them.

Categories

Resources