Jump to content
  • Advertisement
Sign in to follow this  
coderWalker

Maintaining program and C# .NET Window

This topic is 2497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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..."

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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()
}

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!