等待RunOnUIThread完成并继续执行剩余的任务

时间:2017-05-17 15:50:20

标签: c# android visual-studio xamarin mono

我正在通过c#(xamarin.visual studio)开发一个Android应用程序,问题是我有一些任务要在其他线程中运行,当它应该更新布局时应该调用Activity.RunOnUIThread,它的所有内容#39; s运作良好,但线程不要等待此方法芬兰语并继续执行其余的等待。

问题是:如何等待RunOnUIThread完成,然后继续执行任务的其余命令。 ?

public void start(int threadCounter)
    {
        for (int i = 0; i < threadCounter; i++)
        {

            Thread thread1 = new Thread(new ThreadStart(RunScanTcp));
            thread1.Start();

        }

    }
    public void RunScanTcp()
    {

        int port;

        //while there are more ports to scan 
        while ((port = portList.NextPort()) != -1)
        {
            count = port;

            Thread.Sleep(1000); //lets be a good citizen to the cpu

            Console.WriteLine("Current Port Count : " + count.ToString());

            try
            {

                Connect(host, port, tcpTimeout);

            }
            catch
            {
                continue;
            }

            Activity.RunOnUiThread(() =>
            {
                mdata.Add(new data() { titulli = "Port : " + port, sekuenca = "Sequence : ", ttl = "Connection Sucessfull !", madhesia = "", koha = "Time : " });
                mAdapter.NotifyItemInserted(mdata.Count() - 1);
                if (ndaluar == false)
                {
                    mRecyclerView.ScrollToPosition(mdata.Count() - 1);
                }
            }); // in that point i want to wait this to finish and than continue below...
            Console.WriteLine("TCP Port {0} is open ", port);

        }

3 个答案:

答案 0 :(得分:2)

首先,您应该避免创建新的Threads。 在这种情况下,您必须使用ThreadPool.QueueUserWorkItem将CPU绑定操作排入队列。 然后,您可以使用ManualResetEventSlimTaskCompletionSource来同步UI thread和工作线程。

示例:

// mre is used to block and release threads manually. It is
// created in the unsignaled state.

ManualResetEventSlim mre = new ManualResetEventSlim(false);

RunOnUiThread(() =>
{            
    // Update UI here.
    // Release Manual reset event.

    mre.Set();
});

// Wait until UI operations end.
mre.Wait();

在您的具体案例中:

for (int i = 0; i < threadCounter; i++)
{
    ThreadPool.QueueUserWorkItem(RunScanTcp);
}

private void RunScanTcp(object stateInfo) 
{
    // Do CPU bound operation here.
    var a = 100;
    while (--a != 0)
    {
        // mre is used to block and release threads manually. It is
        // created in the unsignaled state.
        ManualResetEventSlim mre = new ManualResetEventSlim(false);

        Activity.RunOnUiThread(() =>
        {
            // Update UI here.

            // Release Manual reset event.
            mre.Set();
        });

        // Wait until UI operation ends.
        mre.WaitOne();
    }
}

如果您更喜欢使用TaskCompletionSource,可以使用替代方法

private async void RunScanTcp(object stateInfo)
{
    // Do CPU bound operation here.
    var a = 100;
    while (--a != 0)
    {
        // using TaskCompletionSource
        var tcs = new TaskCompletionSource<bool>();

        RunOnUiThread(() =>
        {
            // Update UI here.

            // Set result
            tcs.TrySetResult(true);
        });

        // Wait until UI operationds.
        tcs.Task.Wait();
    }
}

答案 1 :(得分:1)

您可以将Monitor.WaitMonitor.Pulse与共享myLock对象一起使用,以等待UI执行。

Activity.RunOnUiThread(() =>
{
    mdata.Add(new data() { titulli = "Port : " + port, sekuenca = "Sequence : ", ttl = "Connection Sucessfull !", madhesia = "", koha = "Time : " });
    mAdapter.NotifyItemInserted(mdata.Count() - 1);
    if (ndaluar == false)
    {
        mRecyclerView.ScrollToPosition(mdata.Count() - 1);
    }
    lock(myLock) Monitor.Pulse(myLock)
});
lock(myLock) Monitor.Wait(myLock)
Console.WriteLine("TCP Port {0} is open ", port);

答案 2 :(得分:0)

对于那些对async / await解决方案感兴趣的人,有Stephen Cleary的AsyncManualResetEvent,例如:

var mre = new AsyncManualResetEvent();
this.context.RunOnUiThread(() =>
{
    // Do awesome UI stuff
    mre.Set();
});
await mre.WaitAsync();
相关问题