c# run process without freezing my App's GUI

前端 未结 3 2109
清歌不尽
清歌不尽 2020-12-20 06:03

I want to start a process (calling another program), currently the external program takes time (it is normal)!

but it freezes my GUI I saw allot of examples and I\'m

相关标签:
3条回答
  • Use this:

    declare at top:

    BackgroundWorker backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
    

    then in form_load:

              backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
                backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    

    after which:

    backgroundWorker1.RunWorkerAsync(someArg); // this calls  backgroundWorker1_DoWork(....
    
    
        // This event handler is where the actual,
        // potentially time-consuming work is done.
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
        }
    
    0 讨论(0)
  • BackgroundWorker was designed for exactly this kind of scenario.

    See MSDN.

    It provides useful methods for signalling (both ways) that don't require you to marshall calls back to the UI thread yourself.

    Or use the Task Parallel Library...

    0 讨论(0)
  • 2020-12-20 06:54

    Here is a link showing how to use an asynchronous method. http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

    You can use the asynchronous method to start the process, and it won't freeze the gui while it starts up.

    void Your_Method()
    {
       //Start process here
    }
    
    
    MethodInvoker myProcessStarter= new MethodInvoker(Your_Method);
    
    myProcessStarter.BeginInvoke(null, null);
    

    MethodInvoker Description

    0 讨论(0)
提交回复
热议问题