okay so I have an app that updates it's status every second to run tests and grab information from various files/places on a system. My app works great but I get a funny error from time to time: System.InvalidO perationExcepti on "object is being used" (or something similar).
every second a timer sets off an event. this event creates a thread to run the tests as to not freeze the GUI. However before the test is run, it fetches a static boolean that says whether a thread like this is already running or not. if the bool returns true then the thread is not initialized.
here is the code for the timer event: - private void scheduler_event(object source, System.Timers.ElapsedEventArgs e)
-
{
-
DateTime static_time_now = DateTime.Now;
-
bool pre_busy = constants.busy;
-
constants.busy = true;
-
-
if (constants.firstrun == true)
-
{
-
constants.firstrun = false;
-
constants.busystatus = new bool[] { false, false, false };
-
constants.updatestatus = new bool[] { true, true, true };
-
Thread firstrunthread = new Thread(firstrun);
-
firstrunthread.Start();
-
}
-
-
if (pre_busy == false)//when program is fired up, prebusy is set to false
-
{
-
constants.updatestatus[0] = ((constants.next_sysinfo <= static_time_now) && (constants.busystatus[0] == false));
-
constants.updatestatus[1] = ((constants.next_drive <= static_time_now) && (constants.busystatus[1] == false));
-
constants.updatestatus[2] = ((constants.next_eventlog <= static_time_now) && (constants.busystatus[2] == false));
-
-
Thread runthread = new Thread(run);
-
runthread.Start();
-
}
-
-
}
constants is an abstract class with static methods I use as global values.
now in my run code, once the test has finished, I set
can this be the problem? If yes, how do I get rid of it?
3 1094
I am also new to the C# world, but I am writing a similar app.
What I've done is created a thread with a loop in it. This way you do not have to worry about multiples threads colliding. -
public void threadMethod()
-
{
-
//Init Code - Things to happen only on the first run.
-
Thread.Sleep(1000);
-
//Now the loop
-
while (condition)
-
{
-
// Get Stats
-
Thread.Sleep(1000);
-
-
}
-
}
-
yes but this will freeze my Gui, especially when I call Perl scripts in my testing that take 10 minutes to complete.
I found the answer to my question:
I lock my whole constants class (this is the class with my static methods/variables) whenever I change busy.
so instead of:
I have: - lock(constants){
-
{
-
constants.busy = false;
-
}
-
}
app running for 48 hours and no problems... looking good!
Great!
I see what you mean now. I wasn't sure how long it would take to process your requests.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Elidel |
last post by:
Is the use of a COM object in a c# program considered 'unsafe' code ?
Does the method that calls the COM object need the 'unsafe' keyword ?
Is code that calls a COM object considered unmanaged?
Is 'unsafe' code the same as unmanaged code ?
|
by: Andre |
last post by:
if I have an unsafe method that takes in two arrays, performs some
operations and returns; I know that the garbage collector will not
collect objects allocated inside the unsafe method. However, if I'm
calling this method from a main() method of a class which is safe (i.e
managed), will the allocated array (which, suppose, has been allocated
inside the main() method) be collected by the garbage collector after
it's been used? Also, how do...
|
by: Bradley1234 |
last post by:
Sorry if this is obvious, but Ill ask...
Is there a new way of using pointer operations in C# ?
Ive got a Deitel book on C# that neither mentions the word "pointer" nor
"unsafe" in the index.
coming from a C/C++ world, pointers make perfect sense, and getting up to
speed on CS, did pointers go away?
|
by: CodeTyro |
last post by:
My native language being C++, I've got a few questions that a couple of hours
of searching on msdn didn't answer.
First, when using unsafe code and pointers, what is the C# equivalent to the
C++ "delete" command? I would like to keep my memoryspace clean, but I've
been unable to find a way to do it thus far.
Second, I'm using structs to store a specific set of data. Is there a C#
equivalent to the C++ STL "Vector" (or any of the...
|
by: TomHL |
last post by:
hello,
- I have a Bitmap Object named: bmp1
- I already have the numerical values of:red,blue and green colors + I have
the X and Y cordinates.
How do I set the pixel value via safe code on the bmp1 object?
(the pixel location based on the X & Y values and it's color based on the
red,blue and green values the I already have)
| |
by: Juuso Hukkanen |
last post by:
I need a list of multithreading unsafe C (C99) functions/features.
comp.programming.threads provided an initial list of C:ish functions,
with following ANSI C functions:
asctime, gmtime, localtime, ctime, tmpnam, strtok
http://www.lambdacs.com/cpt/FAQ.html#Q150
However, extra Googling hinted rand() and srand(), also being
unsuitable for multi-threading - opinions? And what is the status of
|
by: greg wellman |
last post by:
I'm trying to track down a problem in code written by someone else.
The symptom appears to be a form of thread unsafety. Let me write a
little psuedo code
bool firsttime = true;
(some more global declarations)
AnInitFunctionThatEveryThreadRuns()
{
if (firsttime)
|
by: Sergei |
last post by:
Can anyone explain why PostMessage to a Windows Form
is considered unsafe and raises InvalidOperationException?
I can get rid of that setting CheckForIllegalCrossThreadCalls
to false and everything works just fine. Still I feel somewhat
uncomfortable.
Sergei
|
by: james |
last post by:
Hey Guys,
Would anyone mind explaining why a foreach will implicitly do unsafe
casts, and if there is a way to turn this off?
Here is an example:
ulong vals = new ulong { ulong.MaxValue };
foreach (uint x in vals)
Console.WriteLine(x);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |