Mono using statement bug with initialization - c#

I think there is a bug in c# mono implementation.
using (ITransactional transaction = new TransactionalContext ())
using (ITransactional logTransaction =
new TransactionalContext(transaction.Connection ())){
...
}
Such code doesn't call constructor of TransactionalContext and immediatelly call Dispose() on transaction variable.
Could someone confirm that?
I'm compiling this code using Mono 3.12.1 (Windows)
Some investigation:
Such code work compiled by MS compiler and fail compiled by Mono
public class DispTest : IDisposable
{
private static int t = 0;
public DispTest()
{
Console.WriteLine("CONSTRUCTOR");
this.Text = "HELLO" + t.ToString();
t += 1;
}
public DispTest(string text)
{
this.Text = text;
Console.WriteLine(text);
}
public string Text { get; set; }
public void Dispose()
{
Console.WriteLine("DISPOSING");
}
}
//in main
using (DispTest t = new DispTest())
using(DispTest t2 = new DispTest(t.Text)){
}
using (DispTest t = new DispTest()){
using(DispTest t2 = new DispTest(t.Text)){
//this code will fail too.
}
}

Related

ThreadAbortException when running nunit test with Thread.SetApartmentState(ApartmentState.STA)

I have the following classes. I am trying to test the component that I built but its throwing ThreadAbortException and I'm unable to fix the issue. Following is the code I've written
This is on codebehind of
UserPromptWPS.xaml.cs
public partial class UserPromptWPS : Window
{
public bool WPSButtonFailed { get; set; }
public bool WPSButtonPassed { get; set; }
public bool DoTest { get; set; }
private IWebDriver driver;
private WebDriverWait wait;
public UserPromptWPS(IWebDriver driver, WebDriverWait wait)
{
InitializeComponent();
this.driver = driver;
this.wait = wait;
Loaded += UserPromptWPS_Loaded;
}
private async void UserPromptWPS_Loaded(object sender, RoutedEventArgs e)
{
WPSButton wpsButton = new WPSButton();
await Task.Run(() =>
{
while(true)
{
if(wpsButton.ClickWPS(driver,wait))
{
break;
}
}
});
this.Close();
}
}
Here WPSButton is an object I'm calling. And below is the NUnit test I've written.
NUnit Test
[TestFixture]
public class WPSButtonTests
{
[Test]
public void ExecuteClickWPS_Success_ReturnsTrue()
{
var result = false;
Thread thread = new Thread(new ThreadStart(delegate
{
LoginWebGui loginWebGui = new LoginWebGui();
ClearLog clearLog = new ClearLog();
WPSButton wpsButton = new WPSButton();
LogoutWebGui logoutWebGui = new LogoutWebGui();
var webDriverSetup = new WebDriverSetup();
var driver = webDriverSetup.WebDriver;
var wait = webDriverSetup.Wait;
loginWebGui.ExecuteLogin(driver, wait);
clearLog.ExecuteClearLog(driver, wait);
UserPromptWPS userPromptWPS = new UserPromptWPS(driver, wait);
userPromptWPS.Closed += (s, e) => userPromptWPS.Dispatcher.InvokeShutdown();
userPromptWPS.ShowDialog();
result = userPromptWPS.WPSButtonPassed;
}));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
Assert.That(result, Is.True);
}
}
Here running this unit test throwing ThreadAbortException error. What I'm trying to achieve here is to run an infinite loop when 'UserPromptWPS' window is open and upon (if) condition is met then break the loop and close the window. I couldn't fix the issue of why am I getting ThreadAbortException error.
If someone could help me out please.

Can't delete a file after thread is interrupted

I'm kinda lost on this one ; i've tried everything i know for doing such operation and the error persists.
I've a FileProcessor class that creates a new thread, do some operations, etc ; however, even when manually calling Dispose() inside it and Thread.Interrupt() i can't seem to delete the files after use.
First i was doing this code using an async method on the main thread ; now i've switched to threading with this FileProcessor, just trying to delete those files after the operation.
I can delete one or two files, but when it gets to the third file it throws an System.IOEXception
I truly don't know what else can i do. Any input is appreciated.
I was using Worker.Join inside Dispose() and waiting for the thread to finish or the GC ends it - but neither of em ever happened.
Thanks
My code (reduced as possible)
Form1:
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private bool RestartTimer;
private bool ThreadRunning;
FileProcessor TIFFtoXMLProcessor;
FileProcessor CIP3toTIFFProcessor;
List<string> files;
public Form1()
{
InitializeComponent();
TIFFtoXMLProcessor = new FileProcessor();
RestartTimer = false;
}
private void BeginWork()
{
TIFFtoXMLProcessor.EnqueueFileName(#"C:\test\yourtestfile1.txt");
TIFFtoXMLProcessor.EnqueueFileName(#"C:\test\yourtestfile2.txt");
TIFFtoXMLProcessor.EnqueueFileName(#"C:\test\yourtestfile3.txt");
files = new List<string>(TIFFtoXMLProcessor.fileNamesQueue);
TIFFtoXMLProcessor.eventWaitHandle.Set();
if(TIFFtoXMLProcessor.worker.IsAlive == false)
{
foreach(var item in files)
{
System.IO.File.Delete(item);
}
}
}
}
}
The FileProcessor class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
namespace WindowsFormsApp1
{
class FileProcessor : IDisposable
{
public EventWaitHandle eventWaitHandle { get; private set; }
public Thread worker { get; private set; }
private readonly object locker = new object();
public Queue<string> fileNamesQueue { get; private set; }
public string currConversion { get; private set; }
public bool JobComplete { get; private set; }
private CancellationTokenSource cancelParallelWorker;
public string ColorSeparator { get; private set; }
private readonly TextBox tbStatus;
public string outputFolder { get; private set; }
List<string> filesgoingtorun;
//var AvailableJobsDictionary = new Dictionary<string, List<string>>();
//string nZones, string zWidth, string fzWidth, string lzWidth, string zAreaWidth, string zAreaHeight, double DPI
public FileProcessor()
{
eventWaitHandle = new AutoResetEvent(false);
fileNamesQueue = new Queue<string>();
// Create worker thread
worker = new Thread(Work)
{
IsBackground = true
};
cancelParallelWorker = new CancellationTokenSource();
worker.Start();
}
public void EnqueueFileName(string FileName)
{
// Enqueue the file name
// This statement is secured by lock to prevent other thread to mess with queue while enqueuing file name
lock (locker) fileNamesQueue.Enqueue(FileName);
// Signal worker that file name is enqueued and that it can be processed
//eventWaitHandle.Set();
}
private void Work()
{
List<string> filesToWork = new List<string>();
while (true)
{
string fileName = null;
// Dequeue the file name
lock (locker)
while (fileNamesQueue.Count > 0)
{
fileName = fileNamesQueue.Dequeue();
filesToWork.Add(fileName);
if (fileName == null) return;
}
if (fileNamesQueue.Count == 0 && filesToWork.Count > 0)
{
var tempList = new List<string>(filesToWork);
filesToWork.Clear();
ProcessJob(tempList);
}
}
}
private void ProcessJob(List<string> filesToWork)
{
try
{
JobComplete = true;
switch (currConversion)
{
case "TIF":
{
int j = 0;
foreach (var currJob in filesToWork)
{
//Series of tasks...
j++;
}
eventWaitHandle.WaitOne();
break;
}
}
JobComplete = false;
Dispose();
}
catch (Exception conversionEx)
{
cancelParallelWorker?.Cancel();
}
}
#region IDisposable Members
public void Dispose()
{
// Signal the FileProcessor to exit
EnqueueFileName(null);
// Wait for the FileProcessor's thread to finish
worker.Interrupt();
// Release any OS resources
eventWaitHandle.Close();
}
#endregion
}
}
Your code is insanely complex for what you're trying to do and it's no wonder that somewhere you've left a handle for a file open on a different thread and that's preventing your code from being able to delete the file. Without being able to replicate the issue at this end I can even begin to figure out what you should do.
But here's the approach I'm going to suggest.
You should use Microsoft's Reactive Framework (aka Rx) - NuGet System.Reactive.Windows.Forms and add using System.Reactive.Linq; - then you can do something like this:
public partial class Form1 : Form
{
private Subject<string> _enqueue = new Subject<string>();
private IDisposable _subscription = null;
public Form1()
{
InitializeComponent();
string ColorSeparator = "42";
int imageRotationNumber = 42;
IObservable<string> query =
from file in _enqueue
from ImageListSorted in Observable.Start(() => ImageBuilder(file, ColorSeparator))
from RotateCMYK in Observable.Start(() => Rotate(ImageListSorted.CMYKmages, imageRotationNumber))
select file;
_subscription = query.Subscribe(f => System.IO.File.Delete(f));
_enqueue.OnNext(#"C:\test\yourtestfile1.txt");
_enqueue.OnNext(#"C:\test\yourtestfile2.txt");
_enqueue.OnNext(#"C:\test\yourtestfile3.txt");
}
private CreateCMYKAndImpositionImageList ImageBuilder(string JobImages, string colorDelimiter)
{
return new CreateCMYKAndImpositionImageList(JobImages, colorDelimiter);
}
private RotateImages Rotate(Dictionary<string, string> imageList, int RotationNumber)
{
return new RotateImages(imageList, RotationNumber);
}
}
Now, I've only included two steps in your process, but you should be able to continue the logic through the rest of the steps.
Each step is run asynchronously and the entire thing can be cancelled anytime by calling _subscription.Dispose();.
The final .Subscribe(f => System.IO.File.Delete(f)) can only be hit once all of the steps are complete.
So as long as you avoid anything relating to threading and tasks then this should run quite cleanly.

Understanding C# threading interview question

I was recently asked a thread safety question in a c# interview. I didn't get it quite right.
I'm am trying to understand it now.
Here's the question...
Given a basic Order class...
using System;
namespace OrderThreadTest
{
public class Order
{
public TimeSpan CancelTime;
public int Id;
}
}
Given a simple schedule class stub however I have implemented it to test at home...
using System;
using System.Threading;
namespace OrderThreadTest
{
public class Scheduler : IDisposable
{
private Timer _timer;
public IDisposable Schedule(Order order, Action action)
{
var current = DateTime.Now;
var timeToGo = order.CancelTime - current.TimeOfDay;
_timer = new Timer(x => { action(); }, null, timeToGo, Timeout.InfiniteTimeSpan);
return this;
}
public void Dispose()
{
}
}
}
How do you ensure that the Cancel method in the worker class is thread safe?
My answer is the commented out pieces
using System;
namespace OrderThreadTest
{
public class Answer
{
private readonly Scheduler _scheduler;
private object _myLock = new object();
private Order _order;
public Answer(Scheduler scheduler)
{
_scheduler = scheduler;
}
public void Cancel(Order order)
{
// lock (_myLock)
// {
// _order = order;
// var result =
_scheduler.Schedule(order, () =>
{
//if (order.Equals(_order))
//{
Console.WriteLine("Canceled: " + order.Id);
order = null;
//}
});
// }
}
}
}
My first question in understanding this is how can I cause an example of a second thread setting the passed in Order and changing a earlier timer?
For example cause a thread data clash.
I have tried like this but it always seems to run as expected...
using System;
namespace OrderThreadTest
{
internal class Program
{
private static void Main(string[] args)
{
var a1 = new Answer(new Scheduler());
var o = new Order
{Id = 1, CancelTime = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second + 5)};
a1.Cancel(o);
a1.Cancel(o);
Console.ReadLine();
}
}
}
How do I repro the problem that I am meant to solve here?

Access statusbar on form from cplex callback function c#

I'm working with an C# .Net application that uses Cplex DLL's for an optimization operation, and during that operation I want to write status progress to a statusbar on the that initiated the operation.
This is the general layout of the specific form;
namespace ActResMain
{
public class FormOptimize : System.Windows.Forms.Form
{
private callCplex()
{
//...
cplex.Use(new Cplex_ContinuousCallback());
cplex.Solve()
}
public void Update_OptimizeStatusbarPanel(String strText)
{
statusBarPanel_1.Text = strText;
statusBar1.Refresh();
}
internal class Cplex_ContinuousCallback : Cplex.ContinuousCallback
{
FormOptimize formOpt = new FormOptimize();
public override void Main()
{
//From here I want to edit the statusbar at FormOptimize. I can write progress to console without any problems, but cannot reach function "Update_OptimizeStatusbarPanel".
//If I include "FormOptimize formOpt = new FormOptimize" here, i get Visual studio exception on illegal window reference.
}
}
}
}
I have also tried invoking the Update_OptimizeStatusbarPanel function like this:
internal class Cplex_ContinuousCallback : Cplex.ContinuousCallback
{
FormOptimize formOpt = new FormOptimize();
public override void Main()
{
FormCollection fc = Application.OpenForms;
var mpc = fc[1];
Type type = mpc.GetType();
MethodInfo dynMethod = type.GetMethod("Update_OptimizeStatusbarPanel");
dynMethod.Invoke(mpc, new object[] { String.Format("Running Optimization: {0} iterations ", Niterations)});
}
}
But then I get an exception from visual studio stating that an object created by one thread cannot be modified from another thread.
Maybe this is something stupid that I have missed, but help is greatly appriciated
EDIT: I edited the code as per Mohammad Dehghans suggestion,
public class FormOptimize : System.Windows.Forms.Form
{
private callCplex()
{
cplex.Use(new Cplex_ContinuousCallback(this));
cplex.Solve()
}
internal class Cplex_ContinuousCallback : Cplex.ContinuousCallback
{
FormOptimize _formOptimize;
public Cplex_ContinuousCallback(FormOptimize formOptimize)
{
this._formOptimize = formOptimize;
}
public override void Main()
{
if (Niterations % 10 == 0)
{
_formOptimize.Update_OptimizeStatusbarPanel(0, String.Format("Running Optimization: {0} iterations ", Niterations), 0);
}
}
}
public void Update_OptimizeStatusbarPanel(short panelIndex, String strText, short severity)
{
if (statusBar1.InvokeRequired)
statusBar1.Invoke(new Action<short, string, short>(Update_OptimizeStatusbarPanel), panelIndex, strText, severity);
else
{
if (panelIndex == 0)
{
//...
statusBarPanel_0.Text = strText;
}
else if (panelIndex == 1)
{
//...
statusBarPanel_1.Text = strText;
}
statusBar1.Refresh();
}
}
}
But by doing that I apparently broke something, as the application just ..stops after statusBar1.Invoke() is called the first time. If I pause the debugger it says that cplex.Solve() is executing, but then nothing more happens.
First of all, you need to pass the instance of your form to the implemented callback class, so when the Main method is called, you have access to the exact instance that is being shown on the screen.
Secondly, you need to use Invoke method to update the UI controls from anther thread (I've not worked with CPLEX so far, but I guess the callback is invoked from another thread. That's usual).
Read this for more information.
The complete code could be:
public class FormOptimize : System.Windows.Forms.Form
{
private callCplex()
{
//Misc code
cplex.Use(new Cplex_ContinuousCallback(this)); // <-- passing `this`
cplex.Solve()
//Misc code
}
public void Update_OptimizeStatusbarPanel(String strText)
{
if (statusBarPanel_1.InvokeRequired)
statusBarPanel_1.Invoke(Action<string>(Update_OptimizeStatusbarPanel), strText);
else
{
statusBarPanel_1.Text = strText;
statusBar1.Refresh();
}
}
internal class Cplex_ContinuousCallback : Cplex.ContinuousCallback
{
FormOptimize _formOptimize;
public Cplex_ContinuousCallback(FormOptimize formOptimize)
{
this._formOptimize = formOptimize;
}
public override void Main()
{
//...
_formOptimize.Update_OptimizeStatusbarPanel(String.Format("Running Optimization: {0} iterations ", Niterations));
}
}
}

BindingList not updating bound ListBox

I have a ListBox that is bound to a BindingList. The BindingList is built up when a third party application raises an event. I can see the BindingList being bound correctly... but nothing enters the ListBox. I have used the exact same logic with some of my own custom types and it usually works very well.
Form class
private Facade.ControlFacade _controlFacade;
public UavControlForm()
{
InitializeComponent();
_controlFacade = new UavController.Facade.ControlFacade();
UpdateEntityListBox();
}
private void UpdateEntityListBox()
{
lsbEntities.DataSource = _controlFacade.GetEntityTally();
lsbEntities.DisplayMember = "InstanceName";
}
Facade class
private Scenario _scenario;
public ControlFacade()
{
_scenario = new Scenario();
}
public BindingList<AgStkObject> GetEntityTally()
{
BindingList<AgStkObject> entityTally = _scenario.EntityTally;
return entityTally;
}
Scenario class
private static BindingList<IAgStkObject> _entityTally = new BindingList<AgStkObject>();
public Scenario()
{
if (UtilStk.CheckThatStkIsAvailable())
{
UtilStk.StkRoot.OnStkObjectAdded += new IAgStkObjectRootEvents_OnStkObjectAddedEventHandler(TallyScenarioObjects);
UtilStk.StkRoot.OnStkObjectDeleted += new IAgStkObjectRootEvents_OnStkObjectDeletedEventHandler(TallyScenarioObjects);
}
}
private void TallyScenarioObjects(object sender)
{
List<AgStkObject> tallyOfStkObjects = UtilStk.GetRunningTallyOfAllStkObjects();
List<string> stkObjectNames = UtilStk.GetInstanceNamesOfStkObjects(tallyOfStkObjects);
foreach (string stkObjectName in stkObjectNames)
{
if (!SearchFlightUavTallyByName(stkObjectName))
{
if (!SearchLoiterUavTallyByName(stkObjectName))
{
if (!SearchEntityTallyByName(stkObjectName))
{
int i = stkObjectNames.IndexOf(stkObjectName);
_entityTally.Add(tallyOfStkObjects[i]);
}
}
}
}
}
I can see the event fire from the third-party application - this adds an entity to _entityList as desired, but noothing is added to lsbEntities - why?
(jump right to the last example if you want to see it fixed etc)
Threads and "observer" patterns (such as the data-binding on winforms) are rarely good friends. You could try replacing your BindingList<T> usage with the ThreadedBindingList<T> code I used on a previous answer - but this combination of threads and UI is not an intentional use-case of winforms data-binding.
The listbox itself should support binding via list notification events (IBindingList / IBindingListView), as long as they arrive form the right thread. ThreadedBindingList<T> attempts to fix this by thread-switching on your behalf. Note that for this to work you must create the ThreadedBindingList<T> from the UI thread, after it has a sync-context, i.e. after it has started displaying forms.
To illustrate the point that listbox does respect list-change notifications (when dealing with a single thread):
using System;
using System.ComponentModel;
using System.Windows.Forms;
class Foo
{
public int Value { get; set; }
public Foo(int value) { Value = value; }
public override string ToString() { return Value.ToString(); }
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
using(var form = new Form())
using (var lst = new ListBox())
using (var timer = new Timer())
{
var data = new BindingList<Foo>();
form.Controls.Add(lst);
lst.DataSource = data;
timer.Interval = 1000;
int i = 0;
timer.Tick += delegate
{
data.Add(new Foo(i++));
};
lst.Dock = DockStyle.Fill;
form.Shown += delegate
{
timer.Start();
};
Application.Run(form);
}
}
}
and now with added threading / ThreadedBindingList<T> (it doesn't work with the regular BindingList<T>):
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
class Foo
{
public int Value { get; set; }
public Foo(int value) { Value = value; }
public override string ToString() { return Value.ToString(); }
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
using(var form = new Form())
using (var lst = new ListBox())
{
form.Controls.Add(lst);
lst.Dock = DockStyle.Fill;
form.Shown += delegate
{
BindingList<Foo> data = new ThreadedBindingList<Foo>();
lst.DataSource = data;
ThreadPool.QueueUserWorkItem(delegate
{
int i = 0;
while (true)
{
data.Add(new Foo(i++));
Thread.Sleep(1000);
}
});
};
Application.Run(form);
}
}
}
public class ThreadedBindingList<T> : BindingList<T>
{
private readonly SynchronizationContext ctx;
public ThreadedBindingList()
{
ctx = SynchronizationContext.Current;
}
protected override void OnAddingNew(AddingNewEventArgs e)
{
SynchronizationContext ctx = SynchronizationContext.Current;
if (ctx == null)
{
BaseAddingNew(e);
}
else
{
ctx.Send(delegate
{
BaseAddingNew(e);
}, null);
}
}
void BaseAddingNew(AddingNewEventArgs e)
{
base.OnAddingNew(e);
}
protected override void OnListChanged(ListChangedEventArgs e)
{
if (ctx == null)
{
BaseListChanged(e);
}
else
{
ctx.Send(delegate
{
BaseListChanged(e);
}, null);
}
}
void BaseListChanged(ListChangedEventArgs e)
{
base.OnListChanged(e);
}
}

Categories

Resources