2012-02-14 24 views
0

私はこの方法の連続性、日付形式の問題

object[] ab = GetSomething(myObject); 

を呼び出す場合、私は

enter image description here

okです。このような日付時刻形式を取得私はTPLを使用するかどうかを知ります、この方法

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

を呼び出すために私がcausiはAM/PMでこのフォーマットを取得します変換が失敗すると、datetime形式が無効であると言って、datetime形式を逐次方式のように変更する方法があります。私は

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

サンプルスケジューラのですか? –

+0

Convert.ToDateTime(myObject.ToDate)を使用している@NeilKnight iamは文字列形式です –

+0

次の質問は、 'myObject'に' ToDate'とは何ですか?プロパティですか?どのように実装されていますか?そのデータ型は何ですか? –

答えて

1

をDATETIME文字列を変換していますどのように

enter image description here

文字列へ/から変換する場合は、必ず明示的に文化を指定します。

あなたのケースでは、スレッドプールのスレッドが期待しているものとは異なるCurrentCultureを持つ可能性があります。

+0

スレッドプールスレッドCurrentCultureを特定のスレッドプールスレッドに変更する方法はありますか? –

+3

@ AI25私はそれを避けるだろう。 'DateTime.Parse'と同様の関数にはカルチャをとるパラメータがあります。それを使用してください。 – CodesInChaos

+0

特定のCultureInfoを渡さずにDateTime.Pars/DateTime.ToStringを使用するライブラリを使用する場合はどうすればよいですか?スレッドでCurrentCultureを設定する必要があると思います。 – Jaap

1

スレッドのカルチャを変更する場合は、アプリケーションカルチャを認識する独自のタスクスケジューラを作成します。スケジューラーは、タスクを実行する前にカルチャを調整できます。

これは `Date`を設定している` GetSomething`ためのコードは何ですか...

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 
}