Reputation: 1459
In my application I am start process (Tshark) and start capturing, after I am finish to capturing I am checking the created file and parse from the process output the number of received packets in order to update my UI. in this point if the created file is big all the UI stuck until the result on number of packets received so I want to do it in different Thread.
Capinfos capInfo = new Capinfos(); //my class who return the number of packets
ThreadStart tStarter = delegate {label.Text = capInfo._numberOfPackets.ToString("#,##0"); };
Thread thread = new Thread(tStarter);
thread.IsBackground = true;
thread.Start();
This code return a cross threading error.
Upvotes: 0
Views: 253
Reputation: 3440
It appears that you're trying to update the UI using a thread other than the UI thread (label.Text = ---
). I think that is probably what the problem is. You could use Task
s to achieve asynchrony. You could use the Task
's ContinueWith
method to wait on the task to finish before updating your UI thread using BeginInvoke
.
http://msdn.microsoft.com/en-us/library/dd235678.aspx
Upvotes: 0
Reputation: 1499860
You're doing it the wrong way round. You should be performing the packet capture in a different thread, only accessing the UI elements within the UI thread.
There are loads of different ways of doing this. The most common are probably:
Control.Invoke
and Control.BeginInvoke
to marshal back to the UI thread when you need toBackgroundWorker
which does some of this for you; you'd hook up the progress reporting event to update the UI from the right threadUpvotes: 1