开发者

c# run process without freezing my App's GUI

I want to start a process (calling another progr开发者_运维知识库am), currently the external program takes time (it is normal)!

but it freezes my GUI I saw allot of examples and I'm learning, it is hard to figure it out, trying to read and learn threading, but it is not that easy (at least for me) and good simple tutorial or code sample?

cheers


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜