我的代码中有类似的东西:
Parallel.ForEach(myList, new ParallelOptions { MaxDegreeOfParallelism = 4 }, item =>
{
Process(item);
});
Run Code Online (Sandbox Code Playgroud)
问题是我在Process()方法中做了很多事情(连接到文件共享,解析文件,保存到数据库等),我担心在这个过程中可能会出现问题,使迭代永远不会完成...... 这可能是曾经发生过?
有没有办法为方法设置超时,Process()以避免最终拥有僵尸线程?
更新:
我发现设置超时的最简单方法是在任务上添加毫秒CancellationTokenSource或调用Wait()方法.
选项1
Parallel.ForEach(myList, new ParallelOptions { MaxDegreeOfParallelism = 4 }, item =>
{
var cts = new CancellationTokenSource(2000);
Task task = Task.Factory.StartNew(() => Process(item), cts.Token);
});
Run Code Online (Sandbox Code Playgroud)
选项#2
Parallel.ForEach(myList, new ParallelOptions { MaxDegreeOfParallelism = 4 }, item =>
{
Task task = Task.Factory.StartNew(() => Process(item));
task.Wait(2000);
});
Run Code Online (Sandbox Code Playgroud)
问题是这些选项都不能取消该Process()方法.我需要检查Process()方法中的某些内容吗?
我使用以下GradientDrawable作为RelativeLayout的背景:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="@integer/my_value"
android:endColor="#0FFF"
android:startColor="#FFFF" />
</shape>
Run Code Online (Sandbox Code Playgroud)
然后在res\values文件夹中创建一个名为integers.xml的文件:
<resources>
<integer name="my_value">90</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)
另一个在res\values-land文件夹下具有相同名称的文件:
<resources>
<integer name="my_value">180</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)
因此,当我旋转设备时,梯度从运行Android 2.3.3(如预期)时从底部 - >从上到右 - >左,但是当我在Android 4.4.4上测试时,渐变角度不会改变.
我也尝试过:
<item name="my_value" format="float" type="dimen">90</integer>
Run Code Online (Sandbox Code Playgroud)
但结果相同.
有任何想法吗?
更新:
使用GradientDrawable的RelativeLayout存在于2个不同的文件中:
水库\布局,土地\ my_layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/my_drawable" >
Run Code Online (Sandbox Code Playgroud)
更新2:
只有在旋转设备时才会出现问题.如果我使用先前旋转的设备启动活动,则正确设置渐变角度.