任务并行库中的日期格式问题

发布于 2025-01-05 06:25:31 字数 656 浏览 3 评论 0原文

如果我调用此方法顺序,

object[] ab = GetSomething(myObject);

我会得到这样的日期时间格式,这是可以的

在此处输入图像描述

知道我是否使用 tpl,调用此方法

Task t1 = Task.Factory.StartNew(() => GetSomething(myObject));
Task t2 = Task.Factory.StartNew(() => GetSomeOtherthing(myObject));
Task.WaitAll(t1, t2);

我使用 AM/PM 获取此格式,这导致转换失败,说日期时间格式无效,是否有一种方法可以像顺序方法一样更改日期时间格式。

在此处输入图像描述

我如何将字符串转换为日期时间

Search.Date = Convert.ToDateTime(myObject.ToDate, CultureInfo.InvariantCulture);

if i call this method sequentiality,

object[] ab = GetSomething(myObject);

i get the date time format like this, which is ok

enter image description here

know if i use tpl, to invoke this method

Task t1 = Task.Factory.StartNew(() => GetSomething(myObject));
Task t2 = Task.Factory.StartNew(() => GetSomeOtherthing(myObject));
Task.WaitAll(t1, t2);

I get this format with AM/PM which is causing conversion to fail, saying invalid datetime format, is there a way to change the datetime formart like the sequential method.

enter image description here

How i am converting the string to datetime

Search.Date = Convert.ToDateTime(myObject.ToDate, CultureInfo.InvariantCulture);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那小子欠揍 2025-01-12 06:25:31

在字符串转换时始终显式指定区域性。

在您的情况下,线程池线程的 CurrentCulture 可能与您的预期不同。

Always explicitly specify a culture when converting from/to string.

In your case it's likely that the threadpool threads have a different CurrentCulture from what you're expecting.

单身情人 2025-01-12 06:25:31

如果您想更改线程的区域性,请创建您自己的了解应用程序区域性的任务调度程序。调度程序可以在执行任务之前调整文化。

这是一个示例调度程序...

class LocalizedTaskScheduler : TaskScheduler
{
    public CultureInfo Culture { get; set; }
    public CultureInfo UICulture { get; set; }

    #region Overrides of TaskScheduler

    protected override void QueueTask(Task task)
    {
        //Queue the task in the thread pool
        ThreadPool.UnsafeQueueUserWorkItem(_ =>
        {
            //Adjust the thread culture
            Thread.CurrentThread.CurrentCulture = this.Culture;
            Thread.CurrentThread.CurrentUICulture = this.UICulture;
            //Execute the task
            TryExecuteTask(task);
       }, null);
    }

    protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
    {
        if (taskWasPreviouslyQueued)
        {
            return false;
        }
        // Try to run the task. 
        return base.TryExecuteTask(task);
    }

    protected override IEnumerable<Task> GetScheduledTasks()
    {
        //We have no queue
        return Enumerable.Empty<Task>();
    }

    #endregion
}

If you want to change the culture of the thread, then create your own task scheduler that knows the application culture. The scheduler can adjust the culture before executing the task.

This is a sample scheduler...

class LocalizedTaskScheduler : TaskScheduler
{
    public CultureInfo Culture { get; set; }
    public CultureInfo UICulture { get; set; }

    #region Overrides of TaskScheduler

    protected override void QueueTask(Task task)
    {
        //Queue the task in the thread pool
        ThreadPool.UnsafeQueueUserWorkItem(_ =>
        {
            //Adjust the thread culture
            Thread.CurrentThread.CurrentCulture = this.Culture;
            Thread.CurrentThread.CurrentUICulture = this.UICulture;
            //Execute the task
            TryExecuteTask(task);
       }, null);
    }

    protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
    {
        if (taskWasPreviouslyQueued)
        {
            return false;
        }
        // Try to run the task. 
        return base.TryExecuteTask(task);
    }

    protected override IEnumerable<Task> GetScheduledTasks()
    {
        //We have no queue
        return Enumerable.Empty<Task>();
    }

    #endregion
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文