Search Results

Search found 3 results on 1 pages for 'jackn'.

Page 1/1 | 1 

  • Why does C# thread die?

    - by JackN
    This is my 1st C# project so I may be doing something obviously improper in the code below. I am using .NET, WinForms (I think), and this is a desktop application until I get the bugs out. UpdateGui() uses Invoke((MethodInvoker)delegate to update various GUI controls based on received serial data and sends a GetStatus() command out the serial port 4 times a second. Thread Read() reads the response from serial port whenever it arrives which should be near immediate. SerialPortFixer is a SerialPort IOException Workaround in C# I found at http://zachsaw.blogspot.com/2010/07/serialport-ioexception-workaround-in-c.html. After one or both threads die I'll see something like The thread 0x1288 has exited with code 0 (0x0). in the debug code output. Why do UpdateGui() and/or Read() eventually die? public partial class UpdateStatus : Form { private readonly byte[] Command = new byte[32]; private readonly byte[] Status = new byte[32]; readonly Thread readThread; private static readonly Mutex commandMutex = new Mutex(); private static readonly Mutex statusMutex = new Mutex(); ... public UpdateStatus() { InitializeComponent(); SerialPortFixer.Execute("COM2"); if (serialPort1.IsOpen) { serialPort1.Close(); } try { serialPort1.Open(); } catch (Exception e) { labelWarning.Text = LOST_COMMUNICATIONS + e; labelStatus.Text = LOST_COMMUNICATIONS + e; labelWarning.Visible = true; } readThread = new Thread(Read); readThread.Start(); new Timer(UpdateGui, null, 0, 250); } static void ProcessStatus(byte[] status) { Status.State = (State) status[4]; Status.Speed = status[6]; // MSB Status.Speed *= 256; Status.Speed += status[5]; var Speed = Status.Speed/GEAR_RATIO; Status.Speed = (int) Speed; ... } public void Read() { while (serialPort1 != null) { try { serialPort1.Read(Status, 0, 1); if (Status[0] != StartCharacter[0]) continue; serialPort1.Read(Status, 1, 1); if (Status[1] != StartCharacter[1]) continue; serialPort1.Read(Status, 2, 1); if (Status[2] != (int)Command.GetStatus) continue; serialPort1.Read(Status, 3, 1); ... statusMutex.WaitOne(); ProcessStatus(Status); Status.update = true; statusMutex.ReleaseMutex(); } catch (Exception e) { Console.WriteLine(@"ERROR! Read() " + e); } } } public void GetStatus() { const int parameterLength = 0; // For GetStatus statusMutex.WaitOne(); Status.update = false; statusMutex.ReleaseMutex(); commandMutex.WaitOne(); if (!SendCommand(Command.GetStatus, parameterLength)) { Console.WriteLine(@"ERROR! SendCommand(GetStatus)"); } commandMutex.ReleaseMutex(); } private void UpdateGui(object x) { try { Invoke((MethodInvoker)delegate { Text = DateTime.Now.ToLongTimeString(); statusMutex.WaitOne(); if (Status.update) { if (Status.Speed > progressBarSpeed.Maximum) { Status.Speed = progressBarSpeed.Maximum; } progressBarSpeed.Value = Status.Speed; labelSpeed.Text = Status.Speed + RPM; ... } else { labelWarning.Text = LOST_COMMUNICATIONS; labelStatus.Text = LOST_COMMUNICATIONS; labelWarning.Visible = true; } statusMutex.ReleaseMutex(); GetStatus(); }); } catch (Exception e) { Console.WriteLine(@"ERROR! UpdateGui() " + e); } } }

    Read the article

  • C#: how to update winform from thread?

    - by JackN
    A C# thread (Read()) causes System.NotSupportedException when it tries to update a winform based on received content. The full error message is Read() System.NotSupportedException: An error message cannot be displayed because an optional resource assembly containing it cannot be found at Microsoft.AGL.Common.MISC.HandelAr() at System.Windows.Forms.ProgressBar._SetInfo() at System.Windows.Forms.ProgressBar.set_Value() at ...ProcessStatus() at ...Read() The Build/Target Environment is: Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE. Is the problem writing to the ProgressBar from a Thread? If so, what is the correct C#/winforms method to update a ProgressBar from a Thread? In this application the Read() Thread is continuous: it is started when the application starts and runs forever. void ProcessStatus(byte[] status) { Status.Speed = status[5]; var Speed = Status.Speed/GEAR_RATIO; Status.Speed = (int) Speed; progressBarSpeed.Value = Status.Speed; ...

    Read the article

  • C# Windows CE 5.0 error: Can't find entry point ExitWindowsEx in PInvoke DLL coredll

    - by JackN
    I need to programatically shutdown a Windows CE 5.0 tablet using Microsoft.NET SDK CompactFramework v2.0. I tried using the solution here but got the error message Can't find entry point ExitWindowsEx in PInvoke DLL coredll Is there a way to add ExitWindowsEx to my build? Do I need a different coredll? [Flags] public enum ExitFlags { Reboot = 0x02, PowerOff = 0x08 } [DllImport("coredll")] public static extern int ExitWindowsEx(ExitFlags flags, int reserved); private static void buttonShutdown_Click(object sender, EventArgs e) { ExitWindowsEx(ExitFlags.PowerOff, 0); } private static void buttonRestart_Click(object sender, EventArgs e) { ExitWindowsEx(ExitFlags.Reboot, 0); }

    Read the article

1