0

アニメーションをレンダリングするためにプロセスを開始するコードがあります。それはうまく機能し、プロセスとすべてを開始しますが、プロセスの実行中に、私のプログラムは応答していないと言い、進行状況の更新をまったく拒否してフリーズします。

問題を引き起こしているはずのコードは次のとおりです。

void Render()
{
    while (currentFrame <= lastFrame)
    {
        bool rendering = IsRendering("cmd");

        if (rendering == false)
        {
            StreamWriter renderBatch = new StreamWriter(batchFile);
            renderBatch.Write('"' + renderPathField.Text + '"' + " -rd " + '"' + imagePath.Text + '"' + " -s " + currentFrame + " -e " + currentFrame + " " + '"' + sceneFile.Text + '"');
            renderBatch.Close();

            var renderProcess = new Process();
            renderProcess.StartInfo = new ProcessStartInfo(batchFile);
            //renderProcess.StartInfo.Arguments = '"' + renderPathField.Text + '"' + " -rd " + '"' + imagePath.Text + '"' + " -s " + currentFrame + " -e " + currentFrame + " " + '"' + sceneFile.Text + '"';
            renderProcess.StartInfo.UseShellExecute = false;
            //renderProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            renderProcess.Start();

            if (renderProcess.HasExited == true && currentFrame < lastFrame)
            {
                ProgressBar1.Value = (currentFrame - 1) / lastFrame;
                currentFrame++; //goes onto the next frame once it is done rendering this frame
            }
        }
    }
}

これが含まれている完全なコードは次のとおりです。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using System.Net;
using System.Diagnostics;
using System.IO;

namespace Maya_Network_Render
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Globals ///////////////////////////////////////
        string prefFile = "C:\\Program Files\\Maya Render Buddy Preferences\\options.txt";
        string batchFile = "C:\\Program Files\\Maya Render Buddy Preferences\\render.bat";
        int firstFrame;
        int lastFrame;
        int currentFrame;
        // Globals //////////////////////////////////////

        private void Form1_Load(object sender, EventArgs e)
        {
            if (Directory.Exists("C:\\Program Files\\Maya Render Buddy Preferences"))
            {
                if (File.Exists(prefFile))
                {
                    StreamReader preferences = new StreamReader(prefFile);
                    string renderFilePath = preferences.ReadLine();
                    renderPathField.Text = renderFilePath;
                    preferences.Close();
                }

                else
                {
                    File.Create(prefFile);
                }

            }

            else
            {
                Directory.CreateDirectory("C:\\Program Files\\Maya Render Buddy Preferences");
                File.Create(prefFile);
            }
        }

        private void sceneBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog scenefinder = new OpenFileDialog();
            scenefinder.Title = "Browse to your Maya scene file";
            scenefinder.RestoreDirectory = true;
            if (scenefinder.ShowDialog() == DialogResult.OK)
            {
                sceneFile.Text = scenefinder.FileName;
            }
        }

        private void imageBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog imageFolderSelection = new FolderBrowserDialog();
            imageFolderSelection.ShowDialog();
            imagePath.Text = imageFolderSelection.SelectedPath;
        }

        private void renderButton_Click(object sender, EventArgs e)
        {
            string imageSavePath = imagePath.Text;
            string scene = sceneFile.Text;

            try
            {
                if (FirstFrameTextbox.Text != "" && LastFrameTextBox.Text != "") // if the textboxes are filled in then assign them to a variable
                {
                    firstFrame = Convert.ToInt32(FirstFrameTextbox.Text);
                    lastFrame = Convert.ToInt32(LastFrameTextBox.Text);

                    if (File.Exists(scene))
                    {
                        if (File.Exists(batchFile))
                        {
                            currentFrame = firstFrame;
                            progressMessage.Text = " Rendering Frame " + currentFrame + " of " + lastFrame + " from " + scene;
                            Render();
                        }

                        else
                        {
                            File.Create(batchFile); // if there is no batch file then we make one!
                            currentFrame = firstFrame;
                            progressMessage.Text = " Rendering Frame " + currentFrame + " of " + lastFrame + " from " + scene;
                            Render();
                        }
                    }

                    else // if there is not a scene file we let the user know that
                    {
                        MessageBox.Show("Please fill in image path, project path and scene file", "Cannot find file");
                        progressMessage.Text = " ERROR! SCENE FILE OR IMAGE PATH IS MISSING";
                    }
                }

                else
                {
                    MessageBox.Show("The numbers entered into the first or last frame fields are invalid", "invalid frame range");
                }
            }

            catch (Exception f)
            {
                MessageBox.Show(f.ToString() + "                                                                                                                                                                                                                                                                                                                                                                 Most commonly errors result non numerical input in the frame entry fields", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        private void ChangeRenderPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog renderfinder = new OpenFileDialog();
            renderfinder.Title = "Browse to your Render.exe file";
            renderfinder.RestoreDirectory = true;
            if (renderfinder.ShowDialog() == DialogResult.OK)
            {
                StreamWriter preferences = new StreamWriter(prefFile);
                renderPathField.Text = renderfinder.FileName;
                preferences.Write(renderPathField.Text);
                preferences.Close();
            }
        }

        public bool IsRendering(string processName)
        {
            foreach (Process renderProcess in Process.GetProcesses())
            {
                if (renderProcess.ProcessName.Contains(processName))
                {
                    return true;
                }
            }
            return false;
        }

        void Render()
        {
            while (currentFrame <= lastFrame)
            {
                bool rendering = IsRendering("cmd");

                if (rendering == false)
                {
                    StreamWriter renderBatch = new StreamWriter(batchFile);
                    renderBatch.Write('"' + renderPathField.Text + '"' + " -rd " + '"' + imagePath.Text + '"' + " -s " + currentFrame + " -e " + currentFrame + " " + '"' + sceneFile.Text + '"');
                    renderBatch.Close();

                    var renderProcess = new Process();
                    renderProcess.StartInfo = new ProcessStartInfo(batchFile);
                    //renderProcess.StartInfo.Arguments = '"' + renderPathField.Text + '"' + " -rd " + '"' + imagePath.Text + '"' + " -s " + currentFrame + " -e " + currentFrame + " " + '"' + sceneFile.Text + '"';
                    renderProcess.StartInfo.UseShellExecute = false;
                    //renderProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    renderProcess.Start();

                    if (renderProcess.HasExited == true && currentFrame < lastFrame)
                    {
                        ProgressBar1.Value = (currentFrame - 1) / lastFrame;
                        currentFrame++; //goes onto the next frame once it is done rendering this frame
                    }
                }
            }
        }

        private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            if (DialogResult.No == MessageBox.Show("If this program is not open it will not assign renders. Would you still like to close?", "You are about to stop rendering!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
            {
                e.Cancel = true;
            }

            else
            {
                foreach (Process renderProcess in Process.GetProcesses())
                {
                    if (renderProcess.ProcessName.Contains("cmd"))
                    {
                        renderProcess.Kill();
                    }
                }
            }
        }
    } // form closing brace
}
4

1 に答える 1

3

UI の更新は、メイン プロセスとは別のスレッドで行う必要があります。そうしないと、プロセス全体が完了するまで待機してから、更新された UI が表示されます。

フォーム内に多くの「処理」コードがあるため、単純な修正はありません。別のスレッドで処理を開始し、更新を UI に戻すイベントを設定する必要があります。

于 2013-10-23T19:49:19.403 に答える