473,788 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c#: help me pinpoint my unsafe thread object!

20 New Member
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:

Expand|Select|Wrap|Line Numbers
  1.         private void scheduler_event(object source, System.Timers.ElapsedEventArgs e)
  2.         {
  3.             DateTime static_time_now = DateTime.Now;
  4.             bool pre_busy = constants.busy;
  5.             constants.busy = true;
  6.  
  7.             if (constants.firstrun == true)
  8.             {
  9.                 constants.firstrun = false;
  10.                 constants.busystatus = new bool[] { false, false, false };
  11.                 constants.updatestatus = new bool[] { true, true, true };
  12.                 Thread firstrunthread = new Thread(firstrun);
  13.                 firstrunthread.Start();
  14.             }
  15.  
  16.                 if (pre_busy == false)//when program is fired up, prebusy is set to false
  17.             {
  18.                 constants.updatestatus[0] = ((constants.next_sysinfo <= static_time_now) && (constants.busystatus[0] == false));
  19.                 constants.updatestatus[1] = ((constants.next_drive <= static_time_now) && (constants.busystatus[1] == false));
  20.                 constants.updatestatus[2] = ((constants.next_eventlog <= static_time_now) && (constants.busystatus[2] == false));
  21.  
  22.                 Thread runthread = new Thread(run);
  23.                 runthread.Start();
  24.             }
  25.  
  26.         }
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
Expand|Select|Wrap|Line Numbers
  1. constants.busy = false
can this be the problem? If yes, how do I get rid of it?
Aug 11 '08 #1
3 1094
danrabydotcom
12 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. public void threadMethod()
  2. {
  3.           //Init Code - Things to happen only on the first run.
  4.           Thread.Sleep(1000);
  5.           //Now the loop
  6.           while (condition)
  7.           {
  8.                     // Get Stats
  9.                     Thread.Sleep(1000);
  10.  
  11.           }
  12. }
  13.  
Aug 12 '08 #2
matthewaveryusa
20 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. constants.busy = false;
I have:

Expand|Select|Wrap|Line Numbers
  1. lock(constants){
  2. {
  3. constants.busy = false;
  4. }
  5. }
app running for 48 hours and no problems... looking good!
Aug 12 '08 #3
danrabydotcom
12 New Member
Great!

I see what you mean now. I wasn't sure how long it would take to process your requests.
Aug 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1916
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 ?
3
3150
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...
17
1877
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?
4
4319
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...
7
3601
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)
2
3270
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
6
1403
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)
10
2588
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
5
2765
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);
0
9656
marktang
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...
0
9498
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,...
0
10366
Oralloy
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...
0
10175
jinu1996
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...
1
10112
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,...
0
6750
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();...
0
5399
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...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.