Maintaining program and C# .NET Window

Started by
7 comments, last by RobinsonUK 12 years, 2 months ago
I am making a program in C#. It has tasks that it has to run as well as updating
the window. The problem is the tasks can take a different amount of time each
time and I need the Window to keep refreshing and remain responsive while
doing the tasks. What is the best way to accomplish this?

The problem now is i have a loop in the progam under a button click event that
includes "while(tasks!=0)". and inside the loop "this.refresh()". This however only
updates the drawing of the Window and the window is not responsive to dragging,
resizing, etc.

Thanks in advance,

Coderwalker
If this post was helpful please +1 or like it !

Webstrand
Advertisement
WPF or WinForms? (or other?)
You should use an async pattern in order to perform your tasks. Look up multithreading, and the C# Dispatcher.
I am using the standard GUI designed window inside of Visual Studio 2008 .NET. I assume it's Win Forms. Is this right?
If this post was helpful please +1 or like it !

Webstrand
It can be either, but probably winforms. Take a look at the BackgroundWorker component, it'll do much for you. Also look at Invoke(), BeginInvoke() etc.
I looked into the background worker object. I have one problem however, I have a Treeview.

The program basically parses a text files for certain keywords, and adds them as nodes.
When ever accessing the Treeview from another thread I get an error "Wrong Thread accessing..."
If this post was helpful please +1 or like it !

Webstrand

I looked into the background worker object. I have one problem however, I have a Treeview.

The program basically parses a text files for certain keywords, and adds them as nodes.
When ever accessing the Treeview from another thread I get an error "Wrong Thread accessing..."


This is what the Dispatcher (Invoke / BeginInvoke) functions are for. You can't directly touch a Form from any thread other than the thread that created it. However, you can use Invoke/BeginInvoke to run an anonymous function on it.
I still don't quite understand, I'm not sure if I'm wording my post correctly.

What I am curious about is if I want the program doing a task that requires updating the form, as well as keeping the form responding, how would I do that?

Is there a step event for a form?

Basically I just want to make sure the Window doesnt remain frozen.

Can I implement something like the following? (I come from a C++ background)


while (running)
{
performShortTask()
Update Window()
}
If this post was helpful please +1 or like it !

Webstrand

You can use [color=#333333]Application.DoEvents(); to pump the window message queue while you're inside a loop. It's not considered best practice to do this, however. You should prefer threads. From a thread you can update components on the main window thread by using invoke/begininvoke on the controls, to marshal the data safely. i.e. you can't call functions on the object from a different thread. invoke/begininvoke allows you to, however. Personally I would also batch changes to the tree, rather than updating it every single time you parse out some data. So, for example, update the contents of the tree for every 1,000 items you parse (or 100, I'm not too sure about your use case here).

This topic is closed to new replies.

Advertisement