Jump to content
  • Advertisement
Sign in to follow this  
Winegums

[.net] multithreading in c#

This topic is 3709 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

Hi, I've been playing around with threads in C# and have written an application using them. however, when my thread is running the UI doesn't update or respond (this ironically being one of the reasons people put heavy work on separate threads). I don't see why this could be the case? I gave the secondary thread low priority, so the main app should be able to redraw and respond without any trouble shouldn't it?

Share this post


Link to post
Share on other sites
Advertisement
Its hard to tell without more information but the only thing I can think of is if you somehow called the working coding in your main thread and not creating a new one,
try printing your thread ID in each area of code to make sure its different

Share this post


Link to post
Share on other sites
Are you properly invoking your drawing calls? You aren't allowed to talk to anything gui from another thread directly, you must invoke those functions indirectly, I forget exactly how.

Share this post


Link to post
Share on other sites
UI (Winforms as well as WPF) can *only* be manipulated by the thread that created them.

If another thread wants to do anything with the UI, it has to call the .Invoke member, which puts it in a queue for the owner thread to perform. So it won't ever be multithreaded.

Share this post


Link to post
Share on other sites
Ok, the thread that I've created goes on an long loop, and updates a progress bar on the screen as it goes...


delegate void ShortThreadOpDelegate(object progBar);
public void ShortThreadOp(object progBar)
{
System.Windows.Forms.ProgressBar progressBar = (System.Windows.Forms.ProgressBar)progBar;
if (progressBar.InvokeRequired)
{
ShortThreadOpDelegate method = new ShortThreadOpDelegate(ShortThreadOp);
progressBar.Invoke(method, new object[] { progressBar });
//return;
}

else
{
while (true)
{
/* for (int i = 0; i < 999999; ++i )
{
double x = System.Math.Sqrt((double)i);
}*/

progressBar.Value++;
if (progressBar.Value == progressBar.Maximum)
{
return;
}
}
}
}



Could it be this update of the progress bar that's killing the interface rendering? I thought Invoke, like Spoonbender said, adds your instruction to the instruction queue to the thread that you're invoking.

Share this post


Link to post
Share on other sites
Invoke calls the method passed on the UI thread. You're running your entire loop inside the (InvokeRequired == false) branch, which means the entire loop is running on the main thread. You should (a) call Invoke inside the loop, and (b) do it less frequently, maybe every 100 times through the loop or so.

Share this post


Link to post
Share on other sites
OK I've tried changing the code so that the invoke calls a function that updates the Button, rather than invoking the whole expensive loop again, but I still have the same problem..


delegate void UpdateProgressBarDelegate(System.Windows.Forms.ProgressBar progressBar);
public void UpdateProgressBar(System.Windows.Forms.ProgressBar progressBar)
{
if (progressBar.Value == progressBar.Maximum)
{
return;
}
else
{
progressBar.Value++;
}


if (progressBar.Value > progressBar.Maximum)
{
progressBar.Value = progressBar.Maximum;
}


}

delegate void ShortThreadOpDelegate(object progBar);
public void ShortThreadOp(object progBar)
{
while (true)
{
for (int i = 0; i < 999999; ++i)
{
double x = System.Math.Sqrt((double)i);
}


System.Windows.Forms.ProgressBar progressBar = (System.Windows.Forms.ProgressBar)progBar;
if (progressBar.InvokeRequired)
{
UpdateProgressBarDelegate method = new UpdateProgressBarDelegate(ShortThreadOp);
progressBar.Invoke(method, new object[] { progressBar });
//return;
}

else
{
UpdateProgressBar(progressBar);
}
}
}



Share this post


Link to post
Share on other sites
Quote:
Original post by Winegums
OK I've tried changing the code so that the invoke calls a function that updates the Button, rather than invoking the whole expensive loop again, but I still have the same problem..

*** Source Snippet Removed ***


try using a background worker.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!