简体   繁体   中英

stacktrace on VS2010

private void ParallelCrashTest() 
{

    for (int x = 0;x < 100; x++)
    {
        Dowork_1(i);
     }
}

private void Dowork_1(int i)
{
 ThreadInfo threadInfo = new ThreadInfo();
 threadInfo.first = 0;
 threadInfo.last = 100;
 for (int x = 0;x < 60; x++)
    {
        Dowork_2(threadinfo);
        progressBar2.Value = x + 1;   
    }
}
private void Dowork_2(object param)
{

    ThreadInfo threadInfo = param as ThreadInfo;
    int first = threadInfo.first;
    int last = threadInfo.last;
    Parallel.For(first, last, i =>
    {

        didsomthing...

    }

    Update();

}

So it doesn't work . (CPU is working 90-100 % it means i guess still working my app) I used Update() instead of Application.DoEvent() . My application is crash or can not Update(). I don't know . I think that may be problem is Update() but i am not sure . Do you have any suggestion ? Thank you .

Crashing in what way?

My guess is that you are trying to update the GUI from another thread, such as to update the progress bar. To do this you must use Invoke .

It would be useful if you could provide a minimalistic simplified but complete example of compiling code that reproduces the error so that we don't have to guess. You could do this by copying your project into a new folder and removing unnecessary files, classes, methods and lines until it is impossible to remove any more lines while still reproducing the error.

Neither the Update() method nor the progressBar2.Value assignment are legal from a worker thread. It won't crash Visual Studio, not even VS2010, you'll get a simple IllegalOperationException to tell you that you are doing something improper.

You'll need to use the Control.Invoke() method. There's a good example in the MSDN article I linked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM