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.
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.InvokeandControl.BeginInvoketo marshal back to the UI thread when you need toBackgroundWorkerwhich does some of this for you; you’d hook up the progress reporting event to update the UI from the right thread