C# 如何为ObexListener对象设置超时?

C# 如何为ObexListener对象设置超时?,c#,C#,执行该行时,obexListener对象有问题: ObexListenerContext=obexListener.GetContext() 正在等待蓝牙发送的文件,程序在收到该文件后才会响应。但如果不发送文件,会发生什么情况?然后它无限期地等待,我没有办法停止程序 我如何实现等待时间,如果在该时间之后没有收到任何信息,那么操作将被取消? 我留下密码:谢谢 这是完整的代码: private void codigo(object sender, BluetoothWin32Authenticati

执行该行时,obexListener对象有问题:

ObexListenerContext=obexListener.GetContext()

正在等待蓝牙发送的文件,程序在收到该文件后才会响应。但如果不发送文件,会发生什么情况?然后它无限期地等待,我没有办法停止程序

我如何实现等待时间,如果在该时间之后没有收到任何信息,那么操作将被取消? 我留下密码:谢谢

这是完整的代码:

private void codigo(object sender, BluetoothWin32AuthenticationEventArgs e)
        {
            if (!obexListener.Authenticate)
            {
                switch (MessageBox.Show("Desea vincular el dispositivo: " + e.Device.DeviceName.ToString(), "Atención", MessageBoxButtons.YesNo))
                //switch (MessageBox.Show("PIN CODE" + e.NumberOrPasskeyAsString, "EMPAREJAMINETO", MessageBoxButtons.YesNo))
                {
                    case DialogResult.Yes:
                        e.Confirm = true;
                        break;
                    case DialogResult.No:
                        e.Confirm = false;
                        break;
                }
            }
            else
            {
                switch (MessageBox.Show("Desea vincular el dispositivo: " + e.Device.DeviceName.ToString(), "Atención", MessageBoxButtons.YesNo))
                //switch (MessageBox.Show("PIN CODE" + e.NumberOrPasskeyAsString, "EMPAREJAMINETO", MessageBoxButtons.YesNo))
                {
                    case DialogResult.Yes:
                        e.Confirm = true;
                        MessageBox.Show("Bienvenido Nuevamente!");
                        break;
                    case DialogResult.No:
                        e.Confirm = false;
                        break;
                }
            }
        }
装货形式:

ObexListener obexListener = new ObexListener(ObexTransport.Bluetooth);
            BluetoothWin32Authentication bluetoothWin32 = new BluetoothWin32Authentication(codigo);
            Guid guid = new Guid(BluetoothService.SerialPort.ToString());
            obexListener.Start();

            if (obexListener.IsListening)
            {
                timer2.Start();
                ObexListenerContext context = obexListener.GetContext();
                context.Request.WriteFile(archivoBluetooth);
                obexListener.Close();
            }

这里有两种扩展方法用于
ObexListener
,它们为
GetContext()
添加超时:

与:


这里的时间单位是毫秒。希望对您有所帮助。

thaks kozhevnikovhi koz,代码工作非常感谢,但是当时间结束时,从第一个方法“GetContext”返回“return task.Result;”时发送一个错误。异常显示为“TimeoutException:Se Excepdióel tiempo de espera de la operación.”然后程序崩溃。这是预期行为。您可以看到,当超时过期时,方法
GetContextAsync()
抛出
TimeoutException
。在这种情况下,当代码尝试访问
GetContext
中的
task.Result
时,将在此特定行中再次生成异常。您可以根据需要更改
GetContextAsync()
的实现。例如,超时过期时返回
null
,然后检查结果是否为null。我添加了
TimeoutException
来说明这个想法。
public static class ObexListenerExtensions
{
    public static ObexListenerContext GetContext(this ObexListener listener, int timeout)
    {
        var task = Task.Run(async () => await listener.GetContextAsync(timeout));
        return task.Result;
    }

    public static async Task<ObexListenerContext> GetContextAsync(this ObexListener obexListener, int timeout)
    {
        var task = Task.Run(() => obexListener.GetContext());

        if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
        {
            return task.Result;
        }
        else
        {
            throw new TimeoutException();
        }
    }
}
ObexListenerContext context = obexListener.GetContext();
ObexListenerContext context = obexListener.GetContext(1000);