Note :
Although in some similar cases BackgroundWorker is recommended
I prefer to do this job without it, cause this way I could make more generic code
that could be used across my applications, and it will use my naming conventions.
What I want :
Show my arraylist inside MyListBox via ShowResult(…) Method
What do you suggest as a best practice in this case
Also maybe I have to move the Focus Method right ater the thread.Start method, which I don’t know
Sample code
btnPress Method :
SetStatus(" Openning & Initializing the File");
ThreadStart threadStart = new ThreadStart(HeavyProcess);
threadProcess = new Thread(threadStart);
threadProcess.Start();
HeavyProcess Method
public void HeavyProcess()
{
SetStatus("Thread Started!");
// Filling an ArrayList arrHealth ....
ShowResult(...);
// Filling the ListBox with the ArrayList
// At this time I want to be sure that the extra thread be finished and it
// returned to the GUI thread
The Cross-Thread Solution of mine :
public delegate void SetStatusDelegate(string text);
public void SetStatus(string stStatus)
{
if (statLLS.InvokeRequired)
statLLS.Invoke(new SetStatusDelegate(SetStatus),stStatus);
else
statLLS.Text = stStatus;
}
— Edit —
Path1 :
The same as above
This will cause an error :
Invalid Operation …
Cross-thread operation not valid …
When I want to access the ListBox and put the Arraylist result in it
It shows that it’s still in the extra thread
how can I leave this “ShowResult(…); ” for the main thread
Path2 :
threadProcess.Start();
ShowResult(...);
// Filling the ListBox with the ArrayList
// the Watch shows that ArrayList is null
this will cause nothing !
Your problem is in calling
MyListBox.Focus()in the background thread. You can’t do that with a Windows Forms app — you must leave GUI handling to the main GUI thread. A more typical approach would be to poll or wait for the background thread to complete and then do the GUI stuff in the main thread. The point of a BackgroundWorker is that it makes this easy by providing things like the RunWorkerCompleted event to actively notify you when the thread is done.Windows Forms does provide a specific mechanism for this kind of thing, though, with the
Windows.Forms.Timer(NOTSystem.Timers.Timer). GUI actions performed by the event handlers attached to theTickevent, even though originating from a separate thread, will work.Basically, the format is: